Demo & Tutorial - Interior Pointer
You can use InteriorPointer() inline function to create an interior pointer, which contains the pouch address and the interior data address. You can use CLockedPtr<> or CMemberPtr<> smart pointer to hold the object alive.
In following sample, interior pointer points to the interior of object CFoo. Holding a CLockedPtr<> smart pointer to the interior address will retain the object alive. You can also create interior pointer based on an existing interior pointer.
#include <stdio.h>
#include <string.h>
#include "hnxGC/hnxGC.h"
using namespace harnix;
class CFoo {
public:
int m_nData[50];
CFoo(){
for(int i = 0; i < 50; i++) {
m_nData[i] = i;
}
}
~CFoo(){
printf("Destructing CFoo[%p]\n", this);
}
HNXGC_ENABLE(CFoo);
};
CLockedPtr<CFoo> subfunc()
{
lp<CFoo> pArray = gcnew CFoo[7];
// create interior pointer to the second element of 'pArray'
lp<CFoo> retval = InteriorPointer(&pArray[1], pArray);
return retval;
}
int main()
{
lp<CFoo> p = subfunc();
// create further interior pointer based on interior pointer 'p'
lp<int> p2 = InteriorPointer(&p[3].m_nData[17], p);
p = nullptr;
// the object is still alive
printf("%u\n", *p2);
return 0;
}
output: 17 Destructing CFoo[00382600] Destructing CFoo[00382538] Destructing CFoo[00382470] Destructing CFoo[003823A8] Destructing CFoo[003822E0] Destructing CFoo[00382218] Destructing CFoo[00382150]