Does this leak memory?

Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 28
Reputation: edgar5 is an unknown quantity at this point 
Solved Threads: 2
edgar5 edgar5 is offline Offline
Light Poster

Does this leak memory?

 
0
  #1
Jan 1st, 2009
Does this leak memory?

  1. Dim Public strBldr As New StringBuilder("test", 5)
  2. ‘alloc a StringBuilder
  3. MsgBox(strBldr.ToString()) ‘use the StringBuilder
  4. strBldr = New StringBuilder("a", 2)
  5. ‘did the destructor of StringBuilder holding “test”
  6. 'free up its memory?

As StringBuilder does not implement a Clear() method, and I am having timing issues with the MSDN offered .Length = 0 + .Capacity = 0 solution; the following works fine if I uncomment any of the MsgBox() statements but fails to set retVal.Capacity = 2 elsewise and therefore the retVal.Append fails.

  1. Public Function WhatChar(ByVal CBox As ComboBox, ByRef retVal As StringBuilder) As Integer
  2. WhatChar = CBox.SelectedIndex
  3. retVal.Length = 0
  4. retVal.Capacity = 0
  5. retVal.Capacity = 2
  6. 'MsgBox("cb INDEX ")
  7. Select Case CBox.SelectedIndex
  8. Case 0 'single space
  9. retVal.Append(" ")
  10. Case 1 'hyphen
  11. retVal.Append("-")
  12. Case 2 'single space>hyphen<single space>
  13. retVal.Capacity = 4
  14. retVal.Append(" - ")
  15. Case 3 'open single quote
  16. retVal.Append("`")
  17. Case 4 'underscore
  18. retVal.Append("_")
  19. Case Else 'everything else
  20. retVal.Append(CBox.SelectedText)
  21. End Select
  22. 'MsgBox("retVal " + retVal.ToString())
  23. End Function

The forum at MSDN has conflicting information. Any thoughts?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Does this leak memory?

 
0
  #2
Jan 1st, 2009
Well.... I don't see when the destructor would be called.... that is, does a second new mean a call to the first destructor? I don't think so... because destruction happens when the object is set to nothing, or it goes out of scope... and neither one is happening. You are simply assigning the object variable to a new instance of an object... and the first object is not set to nothing, nor is it going out of scope... If the idea though, that when the object no longer has the variable pointing to it that it gets destructed can be tested...so I did a for loop from 0 to 1000, and basically just put in it strBldr = New StringBuilder("a", 2).. If the destructor gets called when strBldr no longer points to it, then the application shouldn't consume more memory (maybe temporarily while it's swapping stuff around, but not permanently). Then I opened up Windows Task Manager, and sorted the list by memory usage. Then I clicked the button with the for loop in it, a few times... and sure enough, the memory usage of the EXE kept growing more and more each time I did it. (I made the initial strBldr variable public, in the class scope... so it was declared at the top ) So to answer your question.... probably.
Last edited by Comatose; Jan 1st, 2009 at 11:26 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 648 | Replies: 1
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC