-
Notifications
You must be signed in to change notification settings - Fork 26
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
Overload for Make (IntPtr, ...) #7
Comments
Good observation o n the Action, will do. Also, I think I need an overload for "Make me a ustring from a buffer, but make a copy of the data, and you take care of things" |
I do not think that we need to pass a delegate, you could pass a static method, and that should work, right?
That said, I realized another idiom I want to support "Make me a ustring from a zero-terminated string" with and without deallocation, makes sense to have an Action-less version. |
And that's where you get bit by the problem. Static methods don't guarantee that. See IL_000a.
|
I'm not sure whether this is optimized at runtime in the use-case of method groups like the |
static void MyFree (IntPtr block) {}
public static ustring MyMake (IntPtr block, int size)
{
return Make (block, size, ptr => MyFree (ptr));
} |
Okay, not a big problem, if people use a lambda without captures, it's a-ok. |
Ugh. I do not like it. |
I do not see a better way to implement the native memory handling. Keeping Action is essential, it's just that the user needs to know to cache the delegate |
Another option is to have a base class that does not release, and a couple of subclasses: one that releases with Marshal.Free, and another one that releases with an Action. |
Currently, there is:
ustring Make (IntPtr block, int size, Action<ustring, IntPtr> releaseFunc = null)
The option to avoid allocating here is to make the caller cache the lambda at callsite, this can lead to lots of static delegates being declared, and possibly captures, etc. I'm not sure there is a better way to handle this though, so the users will have to deal with the delegate allocations.
It's not necessary to know the
ustring
that's being released. I don't have enough information to understand why we also pass in the ustring that's being reclaimed. Done this way, the user can still shoot themselves in the foot, since they don't know the implementation details of IntPtrUString, thus if they decide to use the string after free-ing the IntPtr, it'll lead to invalid memory accesses.Maybe trim down the Action parameters to IntPtr? That way, you can use built-in methods like
Marshal.FreeHGlobal
and use it as a method group at call-site?The text was updated successfully, but these errors were encountered: