Demo & Tutorial - Resurrection
In the OnReclaim routine of an object that is being reclaimed by the HnxGC, you can re-reference the object and resurrect it to alive.
Note - the "m_bResurrected" flag is used to avoid repeatly resurrect the object
#include <stdio.h>
#include <string.h>
#include "hnxGC/hnxGC.h"
using namespace harnix;
class CFoo;
CLockedPtr<CFoo> g_pDying;
class CFoo {
public:
int m_nData;
bool m_bResurrected;
CMemberPtr<CFoo> m_pNext;
CFoo() {
m_nData = 777;
m_bResurrected = false;
}
~CFoo() {
printf("Destructing CFoo[%p]\n", this);
}
HNXGC_TRAVERSE(CFoo) {
HNXGC_TRAVERSE_PTR(m_pNext);
}
HNXGC_ONRECLAIM() {
if (!m_bResurrected) {
m_bResurrected = true;
g_pDying = InteriorPointer(this, _PouchPtr(this));
g_pDying->m_pNext = nullptr;
printf("OnReclaim: resurrect %p\n", this);
HNXGC_RESURRECT();
}
}
};
int main()
{
CLockedPtr<CFoo> p = gcnew CFoo;
p->m_pNext = p;
p = nullptr;
HNXGC_COLLECT();
printf("main(): %u\n", g_pDying->m_nData);
return 0;
}
output: OnReclaim: resurrect 00382148 main(): 777 Destructing CFoo[00382148]