
Jonas Persson wrote:
Achilleas Margaritis skrev:
Can you please elaborate on the problem of destructors? I have read some papers that say that gc and destructors don't play well together, but I did not really agree with that. An example would be appreciated.
One example is that, by default, your gc do not handle cyclic dependencies correct because destructors are run when collected.
The following example gives an access violation on windows:
---------------------------------------------- class Foo : public gc_object<Foo> { public: gc_ptr<Foo> m_next; std::string m_name;
Foo(std::string name) : m_name(name) {}
~Foo() { m_next->lastGoodbye(); }
void lastGoodbye() { std::cout << "Bye " << m_name; } }; void test_cycle() { gc_ptr<Foo> foo1 = new Foo("foo 1"); foo1->m_next = new Foo("foo 2"); foo1->m_next->m_next = foo1;
foo1 = 0; gc::collect(); } ----------------------------------------------
But gc'd objects are are not supposed to touch other gc'd objects in their destructor (if you check the readme, I explicitly say that the order of finalization is random). Is there a realistic need to call another gc'd object from a destructor?