3

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

user228058
  • 465
  • 1
  • 7
  • 22
  • What you ask for is impossible to answer in a stackoverflow reply - at least to long to be complete and correct enough to not make you stumble. Read some good C++ book. – peterchen Aug 28 '10 at 00:00

6 Answers6

9

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

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
  • 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. – user228058 Aug 27 '10 at 16:23
  • One *can*, however, be added, by using a different memory allocator. Boehm-GC is one such example. – greyfade Aug 27 '10 at 16:24
  • @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 Aug 27 '10 at 16:34
  • 1
    @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. – Edward Strange Aug 27 '10 at 17:21
  • 1
    @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 Aug 27 '10 at 20:42
7

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.

What is the difference between new/delete and malloc/free?

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
3

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
  • 13,943
  • 6
  • 43
  • 59
2

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
  • 87,846
  • 14
  • 132
  • 192
2

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
  • 6,495
  • 10
  • 50
  • 92
0

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.

patrick
  • 16,091
  • 29
  • 100
  • 164