My C# application uses wrapped C++ code for calculations.
C++ header:
__declspec(dllexport) void SetVolume(BYTE* data, unsigned int width);
C++/CLI wrapper:
void SetVolume(array<Byte>^ data, UInt32 width)
{
cli::pin_ptr<BYTE> pdata = &data[0];
pal->SetVolume(pdata, width);
}
C# :
public startCalc()
{
byte[] voxelArr = File.ReadAllBytes("Filtered.rec");
palw.SetVolume(voxelArr, 490);
//GC.KeepAlive(voxelArr); makes no sense
}
The C++ SetVolume
function starts asynchronous calculations. voxelArr
is not referenced from the managed side any longer and is garbage collected.
How can I prevent the garbage collection for this reference until the unmanaged code finished it's work without to declare voxelArr
as global variable? Creating a copy of array isn't an option as there is really a lot of data. Active wait inside of startCalc()
isn't good too.
pin_ptr
prevents moving it, doesn't it? – Busbyte[] voxelArr
as global var and everything worked just fine. After moving it's declaration into the function scope, c++ can't access the memory some time afterpalw.SetVolume(voxelArr, 490);
is executed. Is there some other reason other than it is garbage collected? – Bus