D
demofo
Does the pinning pointer safe?since the Read function will change the
ppBuffer points to unmanaged C++ heap,how does the dot net runtime knows
they will not move the allocated memory (pointed by ppBuffer)during the
garbage collection?
class NonGCClass
{
private:
int GetBufferLength()
{
return rand();
}
public:
int Read(unsigned char **ppBuffer)
{
const int bufferLength=GetBufferLength();
*ppBuffer=new unsigned char[bufferLength];
memset(*ppBuffer,0x64,bufferLength);
return bufferLength;
}
};
__gc class GCClass
{
private:
NonGCClass *m_pNonGC;
public:
GCClass()
:m_pNonGC(new NonGCClass())
{
unsigned char *pBuffer=NULL;
unsigned char __pin *pPinBuffer=pBuffer;
const int bufferLength=m_pNonGC->Read(&pBuffer);
Byte byteArray[] = new Byte[bufferLength];
Marshal::Copy((IntPtr)pBuffer,byteArray,0,bufferLength);
delete []pBuffer;
pBuffer=NULL;
pPinBuffer=NULL;
}
};
ppBuffer points to unmanaged C++ heap,how does the dot net runtime knows
they will not move the allocated memory (pointed by ppBuffer)during the
garbage collection?
class NonGCClass
{
private:
int GetBufferLength()
{
return rand();
}
public:
int Read(unsigned char **ppBuffer)
{
const int bufferLength=GetBufferLength();
*ppBuffer=new unsigned char[bufferLength];
memset(*ppBuffer,0x64,bufferLength);
return bufferLength;
}
};
__gc class GCClass
{
private:
NonGCClass *m_pNonGC;
public:
GCClass()
:m_pNonGC(new NonGCClass())
{
unsigned char *pBuffer=NULL;
unsigned char __pin *pPinBuffer=pBuffer;
const int bufferLength=m_pNonGC->Read(&pBuffer);
Byte byteArray[] = new Byte[bufferLength];
Marshal::Copy((IntPtr)pBuffer,byteArray,0,bufferLength);
delete []pBuffer;
pBuffer=NULL;
pPinBuffer=NULL;
}
};