10

In a function in my C++ DLL, I'm returning a std::string to my c# application. It pretty much looks like this:

std::string g_DllName = "MyDLL";

extern "C" THUNDER_API const char* __stdcall GetDLLName()
{
    return g_DllName.c_str();
}

But when my C# code calls this function, I get this message in my output window:

Invalid Address specified to RtlFreeHeap( 00150000, 0012D8D8 )

The function declaration in c# looks like this:

[DllImport("MyDll", EntryPoint = "GetDLLName")]
    [return: MarshalAs(UnmanagedType.LPStr)]
    public static extern string GetDLLName();

From what I've been able to find online, sometimes this message appears when there's an inconsistency between which version of new(debug or release, etc) is being used with delete. But I'm not sure if that is what is going on in my case. So I'm not sure exactly what's causing it. Maybe the MashallAs might have something to do with it?

Any ideas?

Thanks!

1
  • Can you check what you found i.e. a) if you are using the correct (debug) libraries for debug builds etc and b) this problem occurs in both debug and release builds and c) try using /clr:pure instead of /clr for your C++ dll.
    – dirkgently
    Apr 28, 2009 at 17:50

1 Answer 1

13

I managed to find the issue. It was the way the C# definition was done. From what I can understand, using the MarshallAs(UnmanagedType.LPStr) in combination with the string return type makes it so that it'll attempt to free the string when its done. But because the string comes from the C++ DLL, and most likely a totally different memory manager, it fails. And even if it didn't fail, I don't want it to be freed anyway.

The solution I found was to change the C# declaration to this (the C++ code is unchanged):

[DllImport("MyDll", EntryPoint = "GetDLLName")]
public static extern IntPtr GetDLLName();

So this makes it so that it just returns a pointer to the string data. And then to change it to a string, pass it to Marshal.PtrToStringAnsi()

return Marshal.PtrToStringAnsi(GetDLLName());

And that gets wrapped into another function for cleanliness.

I found the solution from this page: http://discuss.fogcreek.com/dotnetquestions/default.asp?cmd=show&ixPost=1108

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.