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

add ref calls to prevent segfault, also ensure handle value gets upda… #4

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ internal abstract class UdevHandle : SafeHandleZeroIsInvalid
{
protected readonly Udev m_udev;
private readonly bool m_AddedRef;



internal UdevHandle(Udev udev, IntPtr handle, bool ownsHandle)
: base(handle, ownsHandle)
Expand Down Expand Up @@ -40,11 +42,12 @@ internal class udev_context : UdevHandle
internal udev_context(Udev udev, IntPtr handle, bool ownsHandle)
: base(udev, handle, ownsHandle)
{
m_udev.context_ref(handle);
}

protected override bool ReleaseHandle()
{
m_udev.context_unref(handle);
handle = m_udev.context_unref(handle);
return true;
}

Expand All @@ -66,11 +69,12 @@ internal class udev_monitor : UdevHandle
internal udev_monitor(Udev udev, IntPtr handle, bool ownsHandle)
: base(udev, handle, ownsHandle)
{
m_udev.monitor_ref(handle);
}

protected override bool ReleaseHandle()
{
m_udev.monitor_unref(handle);
handle = m_udev.monitor_unref(handle);
return true;
}

Expand All @@ -92,11 +96,12 @@ internal class udev_enumerate : UdevHandle
internal udev_enumerate(Udev udev, IntPtr handle, bool ownsHandle)
: base(udev, handle, ownsHandle)
{
m_udev.enumerate_ref(handle);
}

protected override bool ReleaseHandle()
{
m_udev.enumerate_unref(handle);
handle = m_udev.enumerate_unref(handle);
return true;
}

Expand Down Expand Up @@ -141,11 +146,12 @@ internal class udev_device : UdevHandle
internal udev_device(Udev udev, IntPtr handle, bool ownsHandle)
: base(udev, handle, ownsHandle)
{
m_udev.device_ref(handle);
}

protected override bool ReleaseHandle()
{
m_udev.device_unref(handle);
handle = m_udev.device_unref(handle);
return true;
}

Expand Down Expand Up @@ -178,6 +184,11 @@ internal sealed class Udev : IDisposable
[UnmanagedFunctionPointer(CallingConvention.Winapi, SetLastError = true)]
private delegate IntPtr _udev_new();

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate IntPtr _udev_ref(
IntPtr handle
);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
private delegate IntPtr _udev_unref(
IntPtr handle
Expand Down Expand Up @@ -305,6 +316,11 @@ private delegate IntPtr _udev_device_get_parent_with_subsystem_devtype(
private NativeLibrary m_Library;

private _udev_new m_udev_context_new;

private _udev_ref m_udev_context_ref;
private _udev_ref m_udev_device_ref;
private _udev_ref m_udev_monitor_ref;
private _udev_ref m_udev_enumerate_ref;

private _udev_unref m_udev_context_unref;
private _udev_unref m_udev_device_unref;
Expand Down Expand Up @@ -345,6 +361,11 @@ bool TryLoadLibrary(string name)
if (
!m_Library.TryGetExport("udev_new", out m_udev_context_new)

|| !m_Library.TryGetExport("udev_ref", out m_udev_context_ref)
|| !m_Library.TryGetExport("udev_device_ref", out m_udev_device_ref)
|| !m_Library.TryGetExport("udev_monitor_ref", out m_udev_monitor_ref)
|| !m_Library.TryGetExport("udev_enumerate_ref", out m_udev_enumerate_ref)

|| !m_Library.TryGetExport("udev_unref", out m_udev_context_unref)
|| !m_Library.TryGetExport("udev_device_unref", out m_udev_device_unref)
|| !m_Library.TryGetExport("udev_monitor_unref", out m_udev_monitor_unref)
Expand Down Expand Up @@ -405,6 +426,11 @@ public void DangerousRelease()
public void Dispose()
{
m_udev_context_new = null;

m_udev_context_ref = null;
m_udev_device_ref = null;
m_udev_monitor_ref = null;
m_udev_enumerate_ref = null;

m_udev_context_unref = null;
m_udev_device_unref = null;
Expand Down Expand Up @@ -441,16 +467,28 @@ public void Dispose()
public udev_context context_new()
=> new udev_context(this, m_udev_context_new(), true);

public void context_unref(IntPtr handle)
public IntPtr context_ref(IntPtr handle)
=> m_udev_context_ref(handle);

public IntPtr device_ref(IntPtr handle)
=> m_udev_device_ref(handle);

public IntPtr monitor_ref(IntPtr handle)
=> m_udev_monitor_ref(handle);

public IntPtr enumerate_ref(IntPtr handle)
=> m_udev_enumerate_ref(handle);

public IntPtr context_unref(IntPtr handle)
=> m_udev_context_unref(handle);

public void device_unref(IntPtr handle)
public IntPtr device_unref(IntPtr handle)
=> m_udev_device_unref(handle);

public void monitor_unref(IntPtr handle)
public IntPtr monitor_unref(IntPtr handle)
=> m_udev_monitor_unref(handle);

public void enumerate_unref(IntPtr handle)
public IntPtr enumerate_unref(IntPtr handle)
=> m_udev_enumerate_unref(handle);

#region udev_monitor
Expand Down