5

I've been trying to create a garbage collector in c++, i've designed it as a base class to all of mine, called garbageCandidate, which holds a static vector containing pointers to garbageCandidate with every garbageCollector instance pushing "this" on the vector.

Then a static function comes and delete all the pointers in the static vector.

At the very begining of the deleting function (delete on first pointer) i get an error stating delete was used on invalid pointer...

Does this have to do with static/dynamic binding? I mean : is the delete operator unable to act as expected since i call delete on a "father" class and it is in fact a child ?

Could a way of avoiding this be to create virtual destructors? (or a virtual delete function)?

or did i completely missed something?

ps:all objects used for test where created dynamically.

Kara
  • 6,115
  • 16
  • 50
  • 57
user2177591
  • 238
  • 2
  • 12

2 Answers2

0

Is there a reason you're rolling your own custom garbage collector? If all your objects are created dynamically, then why aren't you using boost's smart pointers (like boost::shared_ptr) which essentially uses RAII to give you a well tested garbage collection solution?

I ask because usually in the course of software development life cycle of a project, you end up fixing bugs in the code you wrote yourself (most of the time, at least). So is there a reason you're re-inventing the wheel?

Community
  • 1
  • 1
Carl
  • 43,122
  • 10
  • 80
  • 104
0

Rather than rolling your own GC, I think you should be using the Boehm Conservative GC or smart pointers. Neither is perfect:

In spite of this, either of Boehm GC or Smart pointers would be less implementation and maintenance effort than rolling your own solution.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Boehm GC periodically crawls the whole stack & heap to find occurrences of our malloced pointers. It will think a memory fragment is allocated, even if its pointer is only a random composition of some nonpointer data. And it crawls data, too. I think, the only reason behind BoehmGC is that there is currently no better opensource c/c++ garbage collector. It needs to be written. – peterh Dec 05 '13 at 16:51