-
Notifications
You must be signed in to change notification settings - Fork 516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
char*
members in value types marshalled as string
result in a memory leak
#1785
Comments
Can you post the generated code for the string setter? I wonder if we have a 4th option, which is to optimize the string marshaling so it does not allocate at all. Edit: Or is the issue that native destructor for the value type object is never called? If that's the case, then I think it should be |
Here's the setter. set
{
if (__char_ptr_member_OwnsNativeMemory)
Marshal.FreeHGlobal(__instance.char_ptr_member);
__char_ptr_member_OwnsNativeMemory = true;
if (value == null)
{
__instance.char_ptr_member = global::System.IntPtr.Zero;
return;
}
var __bytes0 = global::System.Text.Encoding.UTF8.GetBytes(value);
var __bytePtr0 = Marshal.AllocHGlobal(__bytes0.Length + 1);
Marshal.Copy(__bytes0, 0, __bytePtr0, __bytes0.Length);
Marshal.WriteByte(__bytePtr0 + __bytes0.Length, 0);
__instance.char_ptr_member = (__IntPtr) __bytePtr0;
} Seems like |
After some more testing, if the string is long enough the same issues presents itself with |
Ok, this helps. I was mistakenly thinking you were assigning to a native
Damn, more bugs! 😱 |
The following type
results in a memory leak when the following code is executed:
The
set
accessor allocates memory which is never freed outside of theset
accessor.At this time I see three options to solve this, listed in presumed increasing level of complexity:
char*
tostring
marshalling for value type membersIDisposable
The text was updated successfully, but these errors were encountered: