gcnew but NO gcdelete

  • Thread starter Thread starter bern11
  • Start date Start date
B

bern11

I still have a bit of problem with this one since pointer management is
such a critical task in C/C++ programming. I've read it, I understand
it, but I gotta ask: There is no gcdelete function for objects created
with gcnew. Simple set the handle to nullptr and the system will
destroy it. Correct? If it is assigned to another object through a
second gcnew call, the first object will also be detroyed (unless
another reference has been made to it), correct?
 
bern11 said:
Simple set the handle to nullptr and the system will
destroy it. Correct?

At some point in the future, probably during an allocation, a GC will
occur. If the memory being pointed to by the pointer is no longer
referenced anywhere, then it will be reclaimed. Things are more
complicated when there is a finalizer, of course.
If it is assigned to another object through a
second gcnew call, the first object will also be detroyed (unless
another reference has been made to it), correct?

Nothing will be destroyed straight away in a deterministic fashion. If
you need deterministic cleanup then use the new C++/CLI destructor
semantics (i.e. C++/CLI destructors get renamed to Dispose(), and the
class implements IDispose), and allocate the object on the "stack" as a
local (it will really still be a heap-based object, but behind the
scenes Dispose() will be called automatically).

-- Barry
 
Thanks for the reply. I don't need deterministic cleanup. It just
feels strange to not delete. (I almost wish microsoft had a gcdelete
entry with your explaination in their help system - simply because any C
programmer new to Visual Studio will be looking for it).
 
Back
Top