[Mono-list] InteropServices: DllNotFoundException...?
Simon Ask Ulsnes
Sun, 23 May 2004 17:37:33 +0200
Hooray for Managed Code! ;-)
Could you please give a short example of a C++ library function that
returns a string for use in a managed application? (with proper memory
Jonathan Pryor wrote:
>On Sun, 2004-05-23 at 10:56, Simon Ask Ulsnes wrote:
>>>Furthermore, if you do something like this:
>>> const char*
>>> std::string s ("this is my string");
>>> return s.c_str();
>>>You're *asking* for trouble, as the std::string destructor will free the
>>>memory used to hold the string, so the string returned by
>>>get_my_string() will be pointing to invalid memory.
>>And guess what, that's actually what I'm doing (basically) - so far, it
>>works, I haven't detected any memory leaks (haven't checked very
>>But you say using marshalling and IntPtr's is the best way to do it?
>The above situation isn't a memory leak, so it won't be detected as a
>memory leak. It's instead a "use after free". It's akin to doing this:
> char *mem = malloc (20); // allocate
> strcpy(mem, "some string"); // initialize
> free (mem); // free
> printf ("this is my string: %s\n", mem); // use?! bad.
>It's not safe to use memory after it's freed. Especially in a
>multi-threaded environment -- the memory could have been re-allocated by
>another thread and initialized with different data.
>So this isn't a memory corruption bug or a memory leak, it's just bad
> - Jon
>Mono-list maillist - Monoemail@example.com