locked
Does gcnew require a 'delete' RRS feed

  • Question

  •  

    System::String^ s = gcnew System::String();
    MyClass^ m = gcnew MyClass();
    
    delete s;
    delete m;

    Is it correct to use 'delete' with 'gcnew'? 'gcnew' creates a managed type so I think the delete is not necessary. Following is the link where I found the example: http://msdn.microsoft.com/en-us/library/ms235631.aspx

    // Convert the C style string to a System::String and display it.
      String ^systemstring = gcnew String(orig);
      systemstring += " (System::String)";
      Console::WriteLine("{0}", systemstring);
      delete systemstring;


    --Abhimanyu
    Click the 'Vote as Helpful' arrow if this post was helpful.
    • Edited by Abhimanyu Sirohi Wednesday, May 25, 2011 9:16 AM Added the reference url and code
    Wednesday, May 25, 2011 9:03 AM

Answers

  • On 25/05/2011 12:34, Abhimanyu Sirohi wrote:

    Can you point me to a place where this is explained, either on MSDN or external?

    You may find this blog post comparing C++/CLI and C# Finalize/Dispose techniques useful:

    http://weblogs.thinktecture.com/cnagel/2006/04/ccli-finalize-and-dispose.html

    Giovanni

    Wednesday, May 25, 2011 10:44 AM
  • OK. I think I understood what delete does to Managed types, thanks to the link by Giovanni.
     
    It seems that there is no IDisposable in C++\CLI so 'delete' comes to the rescue here. We can have our unmanaged code cleanup in the Managed Type's destructor which gets called when 'delete' is used on its object. 'delete' does nothing more than calling the destructor of a managed type but it gives us a way to cleanup the unmanaged resources deterministically.
    You can also use the C++/CLI "stack" semantics, which calls the destructor automatically as in in standard C++. Stack semantics is actually better than explicitly calling delete, because it is exception-safe (as is "using" in C#).
     
    [Note however that stack semantics cannot be used with System::String.]
     

    David Wilkinson | Visual C++ MVP
    Wednesday, May 25, 2011 11:27 AM

All replies

  • Memory for managed types is allocated by gcnew, and deallocated by the garbage collector. Memory for native C++ types is allocated and deallocated by the new and delete operators.
    Thanks and Regards Selvam http://www15.brinkster.com/selvamselvam/
    Wednesday, May 25, 2011 9:11 AM
  • No, the garbage collector will delete it.
    Thanks & Regards
    Wednesday, May 25, 2011 9:44 AM
  •  

    // Convert the C style string to a System::String and display it.
     String ^systemstring = gcnew String(orig);
     systemstring += " (System::String)";
     Console::WriteLine("{0}", systemstring);
     delete systemstring;

     

    The orig defined in heap memory. The String Constructor (Char*) constructed using heap memory reference. So, it destroy the memory using delete keyword.

     

    char *orig = "Hello, World!";
    

    You can check the with the following article.

    http://support.microsoft.com/kb/311259


    Thanks and Regards Selvam http://www15.brinkster.com/selvamselvam/
    Wednesday, May 25, 2011 10:17 AM
  • When you use delete with a managed pointer C++/CLI translate it into the Dispose() method so that  you can deterministically destroy  any native resources created.

    In you example you are allocating a String object which wont have a dispose() method, so the delete is unnecessary.

    Also, note that, normally the term "Dispose" is used in context of C#, in c++/cli it is called as destructor (~ClassName).

     

     

    • Proposed as answer by NotAName123 Tuesday, June 18, 2013 2:31 AM
    Wednesday, May 25, 2011 10:32 AM
  • Even though systemstring was constructed using a char*, it should have all managed memory and should not require a delete? Does a System::String holds unmanaged memory internally? How to determine whether we can call delete on a System::String or not?
    --Abhimanyu
    Click the 'Vote as Helpful' arrow if this post was helpful.
    Wednesday, May 25, 2011 10:32 AM
  • Can you point me to a place where this is explained, either on MSDN or external?
    --Abhimanyu
    Click the 'Vote as Helpful' arrow if this post was helpful.
    Wednesday, May 25, 2011 10:34 AM
  • On 25/05/2011 12:34, Abhimanyu Sirohi wrote:

    Can you point me to a place where this is explained, either on MSDN or external?

    You may find this blog post comparing C++/CLI and C# Finalize/Dispose techniques useful:

    http://weblogs.thinktecture.com/cnagel/2006/04/ccli-finalize-and-dispose.html

    Giovanni

    Wednesday, May 25, 2011 10:44 AM
  • I am not sure. But, the MSDN magazine disscussed about Deleting Managed Objects.

    Thanks and Regards Selvam http://www15.brinkster.com/selvamselvam/
    Wednesday, May 25, 2011 10:48 AM
  • OK. I think I understood what delete does to Managed types, thanks to the link by Giovanni.

    It seems that there is no IDisposable in C++\CLI so 'delete' comes to the rescue here. We can have our unmanaged code cleanup in the Managed Type's destructor which gets called when 'delete' is used on its object. 'delete' does nothing more than calling the destructor of a managed type but it gives us a way to cleanup the unmanaged resources deterministically.


    --Abhimanyu
    Click the 'Vote as Helpful' arrow if this post was helpful.
    Wednesday, May 25, 2011 10:57 AM
  • OK. I think I understood what delete does to Managed types, thanks to the link by Giovanni.
     
    It seems that there is no IDisposable in C++\CLI so 'delete' comes to the rescue here. We can have our unmanaged code cleanup in the Managed Type's destructor which gets called when 'delete' is used on its object. 'delete' does nothing more than calling the destructor of a managed type but it gives us a way to cleanup the unmanaged resources deterministically.
    You can also use the C++/CLI "stack" semantics, which calls the destructor automatically as in in standard C++. Stack semantics is actually better than explicitly calling delete, because it is exception-safe (as is "using" in C#).
     
    [Note however that stack semantics cannot be used with System::String.]
     

    David Wilkinson | Visual C++ MVP
    Wednesday, May 25, 2011 11:27 AM
  • +1: Thanks but why can;t we use stack semantics with System::String which is also a ref type?
    --Abhimanyu
    Click the 'Vote as Helpful' arrow if this post was helpful.
    Wednesday, May 25, 2011 12:14 PM
  • +1: Thanks but why can;t we use stack semantics with System::String which is also a ref type?
    I'm not sure that I totally understand it either, but here is a discussion of this issue:
     
    http://www.pcreview.co.uk/forums/error-c3699-cannot-use-indirection-type-system-string-t2241268.html
     

    David Wilkinson | Visual C++ MVP
    Wednesday, May 25, 2011 12:40 PM
  • When you cannot use stack semantics, consider the msclr::auto_handle helper class.

    Wednesday, May 25, 2011 1:30 PM
  • AFAIK, only value types can be allocated on stack. For ref types we should allocate on managedheap(by gcnew)
    Thursday, May 26, 2011 7:12 AM
  • AFAIK, only value types can be allocated on stack. For ref types we should allocate on managedheap(by gcnew)
    No, in C++/CLI it is possible to use stack semantics for reference types. The type is actually allocated on the heap, but to the user it looks and behaves like a stack variable.
     

    David Wilkinson | Visual C++ MVP
    Thursday, May 26, 2011 12:46 PM