3

I'm writing a C++ destructor (I hope that's the right term; I'm new to C++) and I'm not positive on what exactly I need to garbage collect. Let's say I have 2 pointers as instance variables do I need to garbage collect them? What about if I have an object as an instance variable? Or a pointer to an object?

I'm just a little fuzzy on what exactly needs to be deleted and what is automatically cleaned up.

Thanks

justin
  • 104,054
  • 14
  • 179
  • 226
Nosrettap
  • 10,940
  • 23
  • 85
  • 140
  • 8
    Pedantic note: you do not "garbage collect" things in C++. You *delete* them. – Nicol Bolas Jan 31 '12 at 04:47
  • C++ does not have a garbage collector. Are you trying to write one, or are you just trying to delete an object in the destructor? And why aren't you using smart pointers? – Cody Gray - on strike Jan 31 '12 at 04:51
  • if you coming from other languages, C++ destructor concept means, what you dirty, you should clean it yourself. You can't be careless about it. – linuxeasy Jan 31 '12 at 04:57

7 Answers7

13

General rule of thumb is... if you called new, call delete. If you called new[], call delete[]. If you're accessing these pointers outside of the class and effectively sharing them, you'll want to be careful about the "owning" object deleteing the shared objects while they're still in use. Garbage collection isn't quite the right term. You want to destroy the object and free its memory. This is what delete/delete[] does. new/new[] allocate memory and construct an object.

In C++, there is no garbage collector. You must "manually" handle it. That's not to say that it's all tedium. You'll probably get into using smart pointers to handle some of this logic for you. See this question for more information.

Community
  • 1
  • 1
Sion Sheevok
  • 4,057
  • 2
  • 21
  • 37
  • The only answer that mentioned ownership. +1! – Ben Voigt Jan 31 '12 at 04:50
  • So if I have a pointer to an object as an instance field, and let's suppose that no one else knows about it, then I would have to delete the object but not the pointer...Is that correct? – Nosrettap Jan 31 '12 at 04:53
  • The syntax is along the lines of `delete Address`. A pointer is effectively some data storing the address in memory of an object. So, if you have `int* Number;`, you should use `delete Number;` to delete what `Number` points to. You are not deleting the pointer, per se, but what the pointer points to. The syntax merely wants an address and the pointer stores the address. – Sion Sheevok Jan 31 '12 at 04:59
3

You must delete every pointer you allocate with new. It's that simple, and it's that complicated; you must not lose track of any pointer allocated by new until you have deleted it.

Also, you need to make sure that if you use new[] to allocate a pointer, you call delete[] to deallocate it.

It's not about what pointers you happen to have in a class instance. You need to know who owns them (the owner is the one responsible for deleting them). If your object owns those pointers, then it should delete them. If it doesn't own them, then it shouldn't.

This is also why experienced C++ programmers avoid naked pointers where possible. Smart pointers allow you to express ownership types semantically in the language. That way, you don't have to keep track of who owns what; you know who owns it by the type of smart pointer being used.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
3

You must call delete on every space you created using new, delete[] on every area created using new[], and free on everything you got using malloc.

You should also close any sockets you opened, and clear up any other OS resources your class owns.

Note: this is not called garbage collection. Garbage collection is when this process happens automatically, as part of a library or language runtime, not when you do it explicitly.

Borealid
  • 95,191
  • 9
  • 106
  • 122
3

You should start by learning about "Resource Acquisition is Initialization" (RAII) and smart pointers (shared_ptr and unique_ptr). This is the idiom you have to know in C++ if you come from other languages.

Usual practice says that whatever you allocated in your constructor must be deallocated in your destructor (see other answers to know how). Event better, try to use values as much as possible and new as less as possible. C++ loves the stack, and stack allocation and deallocation are automatic and cheap (no new, no delete). Use references and const-references instead of pointers.

If you really need dynamic memory, try to use one of the smart pointers.

J.N.
  • 8,203
  • 3
  • 29
  • 39
2

Any memory you allocate using new operator needs to be freed.

You allocate memory by doing:

int * p1 = new int[5];
p2 = new int;

and you delete it by doing:

delete[] p1
delete p2; 

If you are playing with Classes, you need to do the same thing in constructors (allocate) and destructors (deallocate).

shadyabhi
  • 16,675
  • 26
  • 80
  • 131
2

In an ideal world, nothing. There are smart pointers and containers for every pattern. Sometimes, you will have to write your own, or augment existing implementations, but much of what you need exists already.

For an array, start with std::array and std::vector. For objects, read up on smart pointers and shared pointers. As a generalization: if you need to call delete, delete[] or free, you have usually headed down the wrong path.

justin
  • 104,054
  • 14
  • 179
  • 226
0

You need to garbage collect all pointers that you don't no longer need. Otherwise you would have the dangling pointer problem.

Steffan Harris
  • 9,106
  • 30
  • 75
  • 101