Click to See Complete Forum and Search --> : String^ memory leak


janatech
March 27th, 2009, 07:47 AM
Hi All,

I found a lot of references on net and msdn that an operation like
String^ str = gcnew String[2048];

is going to be garbage collected by the system at regular intervals by the Garbage Collector. And I don't need to specifically write any delete/release memory code.

However, in my application this doesn't seem to be happening.

My sample code,

Line1> while ( <about 2-4 times counter> )
Line2> {
Line3> ......
Line4> str = gcnew String(FuncReturningCharBuff);
Line5> ......
Line6> // delete str; if Line4 is String^ str = gcnew String(FuncReturningCharBuff);
Line7> .....
Line8> }


As my understanding goes,
Loop 1> str gets a 2048 bytes of char buffer.
Loop 2> str gets the another 2048 bytes. ( gcnew makes a new string everytime).
..... until loop 4.

I used the microsoft _ctrlDumpMemoryLeaks to find out about the leaks. So when the program completes the operation, the output is

-----------------------
Detected memory leaks!
Dumping objects ->
{478} normal block at 0x03952090, 2048 bytes long.
Data: < > 05 05 05 00 00 00 00 00 00 00 00 00 00 00 00 00
Object dump complete.
------------------------


Now I want to avoid the memory leak? Am I missing something. Should I uncomment the delete str in Line 6 above?(but again windows doesn't execute the line delete line as in debug mode, it skips). What am I doing wrong? Did I not understanding .net Garbage collection correcting as I stated above?

Kindly correct me on my thing and do give me a suggestion for the problem.
I don't want that memory leak to happen even if its 2048 bytes.

Thank you all for your valuable time.

Best Regards
Jana

Alex F
March 28th, 2009, 02:48 AM
The first thing you can do is to post your code in readable form, including FuncReturningCharBuff function. Maybe memory leak is there.
To see exact place of native memory leak, redefine new operator as described here:
http://support.microsoft.com/default.aspx/kb/140858