Saturday, 15 August 2015

struct - PInvoke: When is the memory allocated by the CLR marshaller freed? -



struct - PInvoke: When is the memory allocated by the CLR marshaller freed? -

pinvoke used pass next construction vb.net unmanaged c when app starts up. values passed in correctly step through code. within called c method save ptr passed in struct.

<structlayout(layoutkind.sequential)> construction teststruct <marshalas(unmanagedtype.i2)> public testdata short <marshalas(unmanagedtype.i2)> public testdata2 short <marshalas(unmanagedtype.i2)> public testdata3 short <marshalas(unmanagedtype.byvalarray, arraysubtype:=unmanagedtype.i2, sizeconst:=256)> public testdata4() short end construction //matching c struct struct teststruct { short testdata; short testdata2; short testdata3; short testdata4[256]; }; //the method in c saves ptr struct passed in (worked vb6) struct teststruct *vcstruct; void dllcallbackfunc(struct teststruct *vbstruct) { vcstruct = vbstruct; }

after save ptr passed in struct, ptr invalid function returns vb.net.

i suspect marshaller allocated memory freed after callback function returns

is there way tell marshaller not deallocate memory allocated during marshaling?

appreciate comments. thanks

the pointer p/invoke marshaler passes unmanaged code valid until unmanaged function returns. have 2 options, see it:

have calling code pass pointer remains valid long unmanaged code retains re-create of pointer. require signature alter pass intptr. , calls marshal.allochglobal , marshal.structuretoptr. have unmanaged code take re-create of construction rather re-create of address of structure.

the latter alternative more commonly chosen.

struct pinvoke unmanaged-memory

No comments:

Post a Comment