Skip to content
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

SampleLoad leaks pinned GC handles #88

Open
smoogipoo opened this issue Feb 24, 2021 · 2 comments
Open

SampleLoad leaks pinned GC handles #88

smoogipoo opened this issue Feb 24, 2021 · 2 comments

Comments

@smoogipoo
Copy link
Contributor

BASS_ChannelSetSync is documented as accepting a channel handle of type HMUSIC, HSTREAM or HRECORD.
BASS_SampleLoad returns an HSAMPLE.

ManagedBass's overload for SampleLoad which takes a byte[] pins the handle then binds to ChannelSetSync and frees only when a Free sync callback is invoked:

public static int SampleLoad(byte[] Memory, long Offset, int Length, int MaxNoOfPlaybacks, BassFlags Flags)
{
return GCPin.CreateStreamHelper(Pointer => SampleLoad(Pointer, Offset, Length, MaxNoOfPlaybacks, Flags), Memory);
}

public static int CreateStreamHelper(Func<IntPtr, int> Function, object Memory)
{
var GCPin = GCHandle.Alloc(Memory, GCHandleType.Pinned);
var Handle = Function(GCPin.AddrOfPinnedObject());
if (Handle == 0)
GCPin.Free();
else Bass.ChannelSetSync(Handle, SyncFlags.Free, 0, (a, b, c, d) => GCPin.Free());
return Handle;
}

Bass.LastError will also be set to Errors.Handle after the call to Bass.ChannelSetSync, in confirmation of the issue. As per the above, this leaks the pinned memory handle.

@smoogipoo smoogipoo changed the title SampleLoad is very leaky SampleLoad leaks pinned GC handles Feb 24, 2021
@olitee
Copy link
Contributor

olitee commented Aug 12, 2021

Closing since PR has been merged into the codebase. Thanks @smoogipoo

@olitee olitee closed this as completed Aug 12, 2021
@smoogipoo
Copy link
Contributor Author

@olitee I believe this is still an issue. Unless I've missed something, the above merged PR is a workaround added to our own project, because I haven't had the time to address this in ManagedBass itself.

@olitee olitee reopened this Aug 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants