Click here to Skip to main content
15,789,840 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
This question always remains in mind as I got mixed answers from Google.

And also,
How would I delete if my variable is a return type?

For example, say I have a C++/Cli function which returns string and also has reference type string parameter. I have to call this function from C#

String^ GetStrings( [Out] String^ %outputstring)
    String^ result = gcnew(/*  C++ function that returns char*  */);
    outputstring = gcnew(/*  C++ function that returns char*  */);
    return result;

Will this cause a memory leak?
How would I go deleting these string object that I created with gcnew after I return these in C#?

Thanks in advance
Updated 9-Jun-11 9:53am

No, never. This is .NET managed memory allocation, the object are destructed and memory reclaimed by the Garbage Collector (GC).

A memory leak is quite possible but not because you forgot to delete anything in a managed space. (However, a leak of unmanaged memory and resources is quite possible; clean-up is is usually placed in Dispose methods (but can be anywhere else); if you forget to dispose, there will be a leak.)

Managed memory is never reclaimed or disposed by the application. Here is how it works: when you Application Domain looses a reference to an object, the object is marked as a subject to destruction. However, it does not happen immediately; exact moment of time is determined by internal GC algorithm. By that reason, any code depending on the time and order of calling destructors is invalid. By the same reason, destructors are used quite rarely. CG mechanism is intellectual enough. For example, it handles cyclic associations: if object A references object B, object B references object C and object C references object A, all this structure is destructed anyway if currently running code cannot access all three objects.
Share this answer
CPallini 9-Jun-11 15:15pm    
My 5.
Sergey Alexandrovich Kryukov 9-Jun-11 18:23pm    
Thank you,
As SAKryukov correctly explained you must NOT explicitely delete what you allocated with gcnew. However the 'C++ function that returns char *' MIGHT require the deletion of the memory pointed by its return value. For instance, consider:
char * str;
str = new char[3];
sprintf(str, "hi");
String ^ result = gcnew (p);
delete [] str;

in the above code the delete [] str; statement is required (otherwise... memory leaks!), while you MUST NOT delete result, of course.
Share this answer
Lutosław 9-Jun-11 16:01pm    
It is worth to add that deleting 'str' will not spoil 'result', since the String's constructor copyies all characters from char*.
manumith 9-Jun-11 17:14pm    
Sure! I understand. I realize I have given a bad example and it should be done as Pallini did. Infact I am doing like that in my code. I was worried about deleting the String^ result and outputstring. So, from all your replies I see that GC will take care of the deleting managed objects even though they are defined in C++/CLI space.

Thanks much every one!
Sergey Alexandrovich Kryukov 9-Jun-11 18:25pm    
Good point, my 5. I failed to mention about what you demonstrated here -- mixed-mode C++/CLI + C++ (unmanaged) mode.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900