Is there any need for free memory in unmanaged code when it is loaded with managed code

There are 2 binary files. One of them is the native / unmanaged C ++ dll, while the other is controlled by C # exe. Now what I am doing is writing a function in a C ++ dll and allocating memory inside it using malloc. I exported this function to be used by my C # module.

In C ++, I did:

char* FunctionHavingAllocatedMemory(int i){

char* p = (char*)malloc(100);

.....

//use p and do not free it.

return p;

}

In C #, I did:

[DllImport("C++.dll")]

private static extern string FunctionHavingAllocatedMemory(int i);

Now, my question is: is there a need to free memory in the C ++ module, or does the C # module automatically free it when the function returns. Why I think, since C # is a managed module, it will automatically clear the memory.

( , ++, , ++. ).

+3
3

: , FunctionHavingAllocatedMemory, .

+2

# Marshal.AllocHGlobal(), dll Marshal.FreeHGlobal() #. Marshal .

+2

GC , , .

, ++, .

0

Source: https://habr.com/ru/post/1779562/


All Articles