I have got a Access memeroy error when get a memory be malloced from unmage code to manage code in Vista, but it works well in XP SP2.
My manage code are
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
string name = RunInterop.GetStringFromUnmanage();
}
}
Interop code
public static class RunInterop
{
const string RunDllFileName = "Unmanage.dll";
[DllImport(RunDllFileName)]
public static extern string GetStringFromUnmanage();
}
Unmage code
extern "C"
{
__declspec(dllexport) char* __stdcall GetStringFromUnmanage()
{
char *lpchar = (char *)malloc(strlen("test) + 1);
memcpy(lpchar," test ",strlen("test "));
return lpchar;
}
}
There have a crash in the ntdll!RtlpLowFragHeapFree function in Vista, but it works well in Vista. I am sure lots of developers have meet this error, any suggestion I know it is ok if using StringBuilder to get the string from the unmanage code, but I am
0:000> kb
ChildEBP RetAddr Args to Child
001ce4d4 770a18c3 001ce814 001ce814 00db25f3 ntdll!RtlpLowFragHeapFree+0x31
001ce4e8 76677a7e 00200000 00000000 04183398 ntdll!RtlFreeHeap+0x101
001ce4fc 7653d5d6 00200000 00000000 041833a0 KERNEL32!HeapFree+0x14
001ce510 7653de31 7660e6fc 041833a0 001ce5f4 ole32!CRetailMalloc_Free+0x1c
001ce520 7a0b425b 041833a0 001ce814 7a0b7ab9 ole32!CoTaskMemFree+0x13
001ce52c 7a0b7ab9 001ce7bc 001ce804 f9131f59 mscorwks!DefaultMarshalOverrides<CSTRMarshalerBase>::ReturnCLRFromNative+0x33
001ce768 79ea35c6 00db25f0 001ce804 001ce7c0 mscorwks!RunML+0x77a
001ce7e8 79ea34ba 00245360 001ce894 00757030 mscorwks!NDirectGenericStubPostCall+0x194
001ce850 00db2647 00245360 001ce894 fca439b0 mscorwks!NDirectGenericStubReturnFromCall+0x1f
001ce87c 00d20303 014a0fb8 0148e8c4 0000004b CLRStub[StubLinkStub]@db2647
001ce8d4 7b0693eb 00000000 00000000 00000000 Manage2Un!Manage2Un.Form1.button1_Click(System.Object, System.EventArgs)+0x33 [D:\Fuhaitao\Manage2Un\Manage2Un\Form1.cs @ 23]
001ce96c 7b1065e9 00000000 00000000 00000000