Demo & Tutorial - Reclamation Ordering
If an object has a destructor defined, then the HnxGC system will execute the destructor when reclaiming the object. Sometime, the destructor may depend on functionality of other object(s). Application program wants these dependent(s) are alive and accessible when the destructor is executed. That's why we introduce the concept of "Reclamation Ordering".
In following sample, object CFoo and CBar are circular referenced. Destructor of CBar will access CFoo's properties, so CFoo must be destructed after CBar when both of them become unreachable garbage.
#include <stdio.h>
#include <string.h>
#include "hnxGC/hnxGC.h"
using namespace harnix;
class CBar;
class CFoo {
public:
int * m_pAge;
CMemberPtr<CBar> m_pBar;
CFoo() { m_pAge = new int(2007);}
~CFoo(){
printf("Destructing CFoo[%p]\n", this);
delete m_pAge;
}
int getAge() { return *m_pAge;}
HNXGC_TRAVERSE(CFoo){
HNXGC_TRAVERSE_PTR(m_pBar);
}
};
class CBar {
public:
CMemberPtr<CFoo> m_pFoo;
CBar() {}
CBar(CWeakPtr<CFoo> pFoo) : m_pFoo(pFoo) {}
~CBar(){
// CFoo is guaranteed to be destructed after this
printf("Destructing CBar[%p]: CFoo [%p] Age = %u\n",
this, (void*)m_pFoo, m_pFoo->getAge());
}
HNXGC_TRAVERSE(CBar){
HNXGC_TRAVERSE_PTR(m_pFoo);
}
HNXGC_ONRECLAIM() {
// declare the dependence on CFoo
HNXGC_DEPEND_DECLARE(m_pFoo);
}
};
int main()
{
CLockedPtr<CFoo> p = gcnew CFoo;
p->m_pBar = gcnew CBar(p);
return 0;
}
output: Destructing CBar[00382180]: CFoo [00382148] Age = 2007 Destructing CFoo[00382148]