c# - how Marshal.FreeHGlobal() works? -


i have c# based ui uses c++ based dll. requirement pass big chunk of memory c# dll. dll write memory buffer , pass c#. have used intptr & global memory functions this. works fine.

the question is, how verify if marshal.freehglobal() has cleaned memory? using big chunk of memory, in terms of mbs. want make sure memory cleaned instantly.

if pass valid handle marshal.freehglobal, will freed. since method doesn't return value, can't sure whether cleaned up.

if have doubt whether you're passing right thing freehglobal, suggest code isn't clean should be. if want make sure, call localfree windows api function, passing handle have passed freehglobal:

[dllimport("kernel32", setlasterror=true)] static extern intptr localfree(intptr mem);  // now, free block of memory allocated marshal.allochglobal intptr rslt = localfree(memptr); if (rslt == intptr.zero) {     // success! } else {     int err = marshal.getlastwin32error();     // error. } 

i suggest, however, if this, call localalloc allocate memory rather calling marshal.allochglobal, it's possible (although unlikely) future versions of .net use other localalloc allocate unmanaged memory. if happened, code depends on localfree break.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -