views:

148

answers:

6

I'm a .net programmer, without much experience with unmanaged code. I've been adding modifications and additions to an unmanaged project, and everything is functioning just fine. Can you give me some pointers as to what kind of code/objects I need to be concerned about in regard to garbage collection?

TIA

+9  A: 

None. C++ doesn't have a garbage collector.

Noah Roberts
+1 Absolutely true.
karlphillip
Sorry if i wasnt clear. I know that c++ doesnt have its own garbage collection, which requires the programmer to do his own disposing. Since I've never worked with a language without gc, I'm looking for guidance on what and when to dispose of objects that I shouldn't run into any trouble.
One *can*, however, be added, by using a different memory allocator. Boehm-GC is one such example.
greyfade
@greyfade: The Boehm-GC, last I checked, did not play well with C++ destructors. Last I checked, it was largely a C library only.
Billy ONeal
@user - OK. You know how there are objects in every language that you have to acquire and then later release? You know, like database handles, network connections, etc... Well, in a non GC language memory is just another one of those things.
Noah Roberts
@user: `I'm looking for guidance on what and when to dispose of objects that I shouldn't run into any trouble.` If you have to ask that question, you should sit down with a C++ book and learn the basics
Falmarri
+6  A: 

On C++ when you allocate memory manually using the new operator, its your job to release this memory later (when its no longer needed) using the delete operator.

http://stackoverflow.com/questions/240212/what-is-the-difference-between-new-delete-and-malloc-free

karlphillip
+2  A: 

You tagged this COM. If so, you are responsible for calling AddRef() and Release() as appropriate for any COM objects you use. They control the reference-counting features in COM, and are not related to the .NET garbage collector.

For your unmanaged objects, you are responsible for calling delete when you are done with them.

Lou Franco
+2  A: 

karlphillip give you good advice.

Moreover I want to add, that when you are using objects, best places to delete them is destructor of the class.

you must be careful, because when you delete something twice, your program will blow up.

There is a useful trick to detect whether object was just deleted.

after deleting them, you can set pointer to null

delete foo;
foo=null;

next time you can check whether it is equal to null, and in otherwise delete them. And the best thing... even if you will try delete null pointer, nothing will happens! :)

noisy
icky C stylin there
Greg Domjan
+2  A: 

If you have everything on the stack, or construct elements into containers such as vector then you won't have to worry about memory.

It is however likely you use at least some form of memory allocation (new/malloc/createobject/globalalloc/sysstring/...)

MSVC (COM) ATL provides managing 'RAII' types to help manage lifetime of objects
CComPtr<> will manage scope
CComQIPtr<> will also manage scope, but will also change to the specified type on assignment.

C++ has std::auto_ptr<> which is a bit old and is heading for deprecated boost/tr1 has a bunch of scoped/shared types for managing ptr & arrays - usage depends on if you use new or new[] so that it can call the right delete or delete[]

Greg Domjan
A: 

Figure out if the code is using smart pointers (it probably is), the smart pointers should destroy objects themselves when they go out of scope.

iterationx