Monday, 15 September 2014

c# - Prevent garbage collection for managed reference which is used in unmanaged code -



c# - Prevent garbage collection for managed reference which is used in unmanaged code -

my c# application uses wrapped c++ code 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 not referenced managed side longer , garbage collected.

how can prevent garbage collection reference until unmanaged code finished it's work without declare voxelarr global variable? creating re-create of array isn't alternative there lot of data. active wait within of startcalc() isn't too.

you can utilize gchandle.alloc(voxelarr,gchandletype.pinned) pin array manually gc won't move or clean it.

you have free handle when knew method complete, require form of callback completion.

c# garbage-collection c++-cli wrapper

No comments:

Post a Comment