Demo & Tutorial - Mandatory Delete
You can mandatory delete a live object, but you should not access the destructed object any more. In the following sample, the object is destructed by HNXGC_DELETE(). After that, you can still assign the pointer to others, but should not access the destructed object.
#include <stdio.h>
#include <string.h>
#include "hnxGC/hnxGC.h"
using namespace harnix;
class CFoo {
public:
int m_nData;
CMemberPtr<CFoo> m_pNext;
~CFoo() {
printf("Destructing CFoo[%p]\n", this);
}
HNXGC_TRAVERSE(CFoo) {
HNXGC_TRAVERSE_PTR(m_pNext);
}
};
int main()
{
lp<CFoo> p1 = gcnew CFoo;
// mandatory delete the object
HNXGC_DELETE(p1);
// no more access to the object
lp<CFoo> p2;
p2 = p1;
// p2->m_nData = 0; // should not access destructed object
p2 = nullptr;
return 0;
}