There are 2 binaries. One is native/unmanaged C++ dll and other is managed c# exe. Now what I am doing is writing a function in c++ dll and allocated 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 any need to free memory in c++ module or c# module will automatically free it when function will return. Why I am thinking is since c# is managed module it will automatically cleanup the memory.
(Though it is good you free memory in c++ but I have certain constrained that I can not free memory in C++. Just wanted to understand more about managed applications and the way they handle memory management).