[Gc] Using GC in C++ via multiple inheritance

Shriramana Sharma samjnaa at gmail.com
Sat Apr 13 00:27:33 PDT 2013


On Sat, Apr 13, 2013 at 11:22 AM, Basile Starynkevitch
<basile at starynkevitch.net> wrote:
> Notice that MyClass will never be destroyed. So the GC would never call its destructor. In particular
> the destructor of LibraryClass will not be called by the GC. If you want the GC to call destructors
> declare insteead
>    class MyClass : public LibraryClass, public gc_cleanup
> You need to to so if for instance LibraryClass destructors do non-trivial things.

I understand that operator delete is only called after the destructor
has finished (so it's just a block of memory and no longer an
"object"). So if I inherit from class gc, only the memory allocation
portion will be handled, and no destructors called, and if I inherit
from gc_cleanup, both the memory allocation portion will be handled
and the destructors called -- fine that's clear now.

> If you use STL containers like std::vector, you'll need also to use the allocator
> of gc/gc_allocator.h header, e.g. in C++2011 code something like
>
> template <typename Elem_t>
> using my_vector = std::vector<Elem_t,gc_allocator<Elem_t>>;

Thanks -- I had learnt this from the Asymptote code.

--
Shriramana Sharma ஶ்ரீரமணஶர்மா श्रीरमणशर्मा



More information about the Gc mailing list