Here I asked a theoretical question, what is the fate of a variable in C ++ code when CoTaskMemAlloc was allocated for it?

The scheme is approximately as follows:

C ++ allocate variable memory via CoTaskMemAlloc, give the variable in C # managed code.

In the C # code, the variable is described as being returned from the outside:

[return: MarshalAs(UnmanagedType.LPWStr)] .

Further, the garbage collector in C # theoretically should free up the memory of this variable when it decides that it is no longer needed, but when it does (and whether) it is not obvious.

So the question is, how do you understand in C ++ code whether the memory assigned to this variable is free or not? Null she obviously will not be assigned, if I understand correctly ..

And as I guess it’s not safe to call CoTaskMemFree from C ++ code "maybe a second time".

Unfortunately, about the logic of the work of the CoTaskMemAlloc/CoTaskMemFree in CoTaskMemAlloc/CoTaskMemFree in different situations is written a little less than nothing ..

  • Since when has the garbage collector started collecting unmanaged memory? And so, for a note, if you connect a library and allocate memory in it, then this library should free up memory. - LLENN
  • Since Marshaling uses CoTaskMemFree to clear memory, that is a very long time :) this is the meaning of the description of the return parameter in C # - NewView
  • @LLENN, that's the point of using Com-Ole allocator to unleash the situation with memory cleaning in the same place where it was allocated. - NewView 5:19 pm
  • If you want the memory cell pointer to be reset to zero after memory is released, write a custom marshaller, I think there are no other options. - LLENN
  • one

0