Click here to Skip to main content
15,894,343 members
Home / Discussions / C#
   

C#

 
GeneralRe: A Static class Serialization problem Pin
HolyGrandFather18-Dec-05 5:27
HolyGrandFather18-Dec-05 5:27 
AnswerRe: A Static class Serialization problem Pin
Guffa18-Dec-05 22:51
Guffa18-Dec-05 22:51 
GeneralRe: A Static class Serialization problem Pin
HolyGrandFather19-Dec-05 4:50
HolyGrandFather19-Dec-05 4:50 
QuestionBrowser closing event Pin
chinky singh15-Dec-05 5:51
chinky singh15-Dec-05 5:51 
AnswerRe: Browser closing event Pin
albCode15-Dec-05 5:54
albCode15-Dec-05 5:54 
GeneralRe: Browser closing event Pin
chinky singh15-Dec-05 6:12
chinky singh15-Dec-05 6:12 
AnswerRe: Browser closing event Pin
Guffa15-Dec-05 11:54
Guffa15-Dec-05 11:54 
QuestionFree memory allocated in unmanaged DLL from C# Pin
Koushik Biswas15-Dec-05 5:50
Koushik Biswas15-Dec-05 5:50 
I will ask the question with a 2 step introduction... Please help me.

Step 1: I write an unmanaged DLL in C++. Here is the exported function:

SAMPLEDLL_API void StringAsOUT_AllocatedInDll( char ** szParam )<br />
{<br />
*szParam = new char [ 128 ];<br />
strcpy( *szParam, "Please free my memory after using me!" );<br />
}


Step 2: I write a C# managed EXE to use this DLL

class Imports<br />
{<br />
[DllImport("SampleDLL.dll")]<br />
public static extern void StringAsOUT_AllocatedInDll( ref int szParam );<br />
...<br />
}


...<br />
Imports impObj = new Imports();<br />
int iAddressOfANSIString = 0;<br />
impObj.StringAsOUT_AllocatedInDll( ref iAddressOfANSIString );<br />
string strOUTPUTFromUnmanagedDll = Marshal.PtrToStringAnsi( ( IntPtr ) iAddressOfANSIString );<br />
Console.WriteLine( "Value of strOUTPUTFromUnmanagedDll = {0}", strOUTPUTFromUnmanagedDll );<br />
...


Question: The code works. It does give console output "Value of strOUTPUTFromUnmanagedDll = Please free my memory after using me!"

But isn't there a memory leak? How do I free the memory allocated by new[] inside the unmanaged DLL? All the Marshal class helps artciles always refer to how to free an unmanaged memory block when it is allocated from .NET code. But here it is allocated by unamanaged code. If anybody can please help me with this, I will be grateful.

Thank you!
AnswerRe: Free memory allocated in unmanaged DLL from C# Pin
albCode15-Dec-05 5:56
albCode15-Dec-05 5:56 
QuestionThanks, but this is still not answered... Pin
Koushik Biswas15-Dec-05 6:04
Koushik Biswas15-Dec-05 6:04 
QuestionRe: Thanks, but this is still not answered... Pin
Koushik Biswas15-Dec-05 6:12
Koushik Biswas15-Dec-05 6:12 
AnswerRe: Thanks, but this is still not answered... Pin
albCode15-Dec-05 6:15
albCode15-Dec-05 6:15 
AnswerRe: Thanks, but this is still not answered... Pin
Gilad Kapelushnik17-Dec-05 23:26
Gilad Kapelushnik17-Dec-05 23:26 
QuestionContainer controls Pin
hasansheik15-Dec-05 5:22
hasansheik15-Dec-05 5:22 
AnswerRe: Container controls Pin
albCode15-Dec-05 5:36
albCode15-Dec-05 5:36 
GeneralRe: Container controls Pin
hasansheik15-Dec-05 6:03
hasansheik15-Dec-05 6:03 
GeneralRe: Container controls Pin
Sean Michael Murphy15-Dec-05 14:43
Sean Michael Murphy15-Dec-05 14:43 
QuestionHow to check the control is container control. Pin
hasansheik15-Dec-05 5:16
hasansheik15-Dec-05 5:16 
Questionexcel output Pin
dimx215-Dec-05 5:12
dimx215-Dec-05 5:12 
AnswerRe: excel output Pin
Curtis Schlak.15-Dec-05 11:13
Curtis Schlak.15-Dec-05 11:13 
GeneralRe: excel output Pin
dimx220-Dec-05 2:43
dimx220-Dec-05 2:43 
QuestionUsing CheckedListBox Pin
Gulfraz Khan15-Dec-05 4:47
Gulfraz Khan15-Dec-05 4:47 
QuestionHow do you use DataGridTableStyle with a strong typed collection derived from CollectionBase Pin
tcmgc15-Dec-05 4:44
tcmgc15-Dec-05 4:44 
Questionproxi authentification Pin
Camilo Sanchez15-Dec-05 4:26
Camilo Sanchez15-Dec-05 4:26 
AnswerRe: proxi authentification Pin
S. Senthil Kumar15-Dec-05 4:46
S. Senthil Kumar15-Dec-05 4:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.