473,324 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Bug? Marshaller doesn't free() strings returned by unmanaged functions

Hello, the documentation for string marshalling[1] says that the runtime
will free the memory of strings returned by unmanaged functions, like in:

[DllImport("...")]
extern string foo();

The TestStringAsResult example[2] also dynamically allocates the
returned string as if in preparation for this. Thing is, the .NET 1.1
runtime doesn't free it. That example will leak memory.

So, which is right, the documentation, or the runtime? I'd say the
runtime, as it'd just be amazingly stupid to code it as in the
documentation, as it fails in cases where the string is read-only, on
the stack, a COM object, from a different allocator, etc., and with
functions like strcpy, strdup, strerror, etc., so you'd be stuck
returning it as IntPtr and doing a PtrToString.. in pretty much all
cases. Anyone know which is the way it's supposed to be?

[1]
http://msdn.microsoft.com/library/de...ferssample.asp
[2]
http://msdn.microsoft.com/library/de...vokelibdll.asp
Nov 22 '05 #1
2 1470
Steven,
The TestStringAsResult example[2] also dynamically allocates the
returned string as if in preparation for this. Thing is, the .NET 1.1
runtime doesn't free it. That example will leak memory.
Really? Last time I tried it it worked fine. How do you tell it's
leaking?

So, which is right, the documentation, or the runtime? I'd say the
runtime, as it'd just be amazingly stupid to code it as in the
documentation, as it fails in cases where the string is read-only, on
the stack, a COM object, from a different allocator, etc., and with
functions like strcpy, strdup, strerror, etc., so you'd be stuck
returning it as IntPtr and doing a PtrToString.. in pretty much all
cases. Anyone know which is the way it's supposed to be?


Well the runtime assumes the memory was allocated with CoTaskMemAlloc
and therefore frees it with CoTaskMemFree. If you use any other
allocation API, you have to switch to an IntPtr return type. If the
string is stack allocated, you shouldn't return a pointer to it at
all.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 22 '05 #2
Mattias Sjögren wrote:
Steven,

The TestStringAsResult example[2] also dynamically allocates the
returned string as if in preparation for this. Thing is, the .NET 1.1
runtime doesn't free it. That example will leak memory.

Really? Last time I tried it it worked fine. How do you tell it's
leaking?


Nevermind, I found it's calling CoTaskMemFree which doesn't free (or
even corrupt, apparently) malloced memory. :P
Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: peter | last post by:
I am trying to truncate some Chinese Text returned from MySQL. If I use the substr function, then the last one or two chinese words would appear as symbols as opposed to the word it should be...
1
by: Andreas Hoetker | last post by:
Hello I'm currently developing a project, that contains one .net (VB) executable and a C++ (MFC) DLL. Now, what I want to do is to call the functions in the dll out of my managed code //managed...
6
by: zeroWePutTheSpamIntoTheBintype | last post by:
I'm having trouble using Braid (my user-defined type) objects returned from functions. I've got: Braid descend_n(int n, int no_strings) This function needs to use void left_multiply(Braid&b)....
2
by: JohnO | last post by:
I am new to CppNet How can I pass a double or float value to Unmanaged function from Managed CppNet Program. I can pass an integer with no problem but when I try it with double or float I get the...
1
by: Jacobo Rodriguez Villar | last post by:
Hello, I'm writting an application using mixed mode with C++ and C++.NET, and I have a problem, if I put a breakpoint or try to enter (with step by step) into a virtual and unmanaged method, the...
1
by: roni | last post by:
hi. i have gc class that wraps call to unamanged function. im confuse with the paramter allocation and gc when sending. i wrote small sample ,to understand if i this is ok. __gc class...
1
by: Steven Brown | last post by:
Hello, the documentation for string marshalling says that the runtime will free the memory of strings returned by unmanaged functions, like in: extern string foo(); The TestStringAsResult...
2
by: trantanm | last post by:
Hi everybody, I failed to call unmanaged functions in C++ dll from C# application. If I compile and produce the C++ dll file in .NET environment, I can call them from C#. Is the reason that the...
1
by: =?iso-8859-1?q?Horacio_Nu=F1ez_Hern=E1ndez?= | last post by:
Hi, I have been writing a wrapper with /clr:pure set, while I using the debug mode it was fine, when turned the mode to release it begins show me the same errors that when we try to use...
4
by: philipqiu | last post by:
hi I want to call unmanaged functions that are implemented in a dll. The problem is about the method return values. for example #include "stdafx.h" typedef struct toOut { int (WINAPI*...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.