Skip to content

Commit 94d5712

Browse files
committed
Implemented IDisposable and Dispose pattern on library classes that wrap unmanaged resources
You can now close an outlet by disposing of it (instead of waiting for garbage collection to close it for you)
1 parent 21065ca commit 94d5712

1 file changed

Lines changed: 83 additions & 11 deletions

File tree

LSL.cs

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,26 @@ public class StreamInfo
147147
public StreamInfo(IntPtr handle) { obj = handle; }
148148

149149
/// Destroy a previously created streaminfo object.
150-
~StreamInfo() { dll.lsl_destroy_streaminfo(obj); }
150+
~StreamInfo() { Dispose(false); }
151+
152+
/// Dispose of the object
153+
public void Dispose()
154+
{
155+
Dispose(true);
156+
GC.SuppressFinalize(this);
157+
}
158+
private bool _disposed = false;
159+
protected virtual void Dispose(bool disposing)
160+
{
161+
if (_disposed)
162+
{
163+
return;
164+
}
165+
166+
dll.lsl_destroy_streaminfo(obj);
167+
obj = IntPtr.Zero;
168+
_disposed = true;
169+
}
151170

152171
// ========================
153172
// === Core Information ===
@@ -306,11 +325,29 @@ public class StreamOutlet
306325
*/
307326
public StreamOutlet(StreamInfo info, int chunk_size = 0, int max_buffered = 360) { obj = dll.lsl_create_outlet(info.handle(), chunk_size, max_buffered); }
308327

328+
~StreamOutlet() { Dispose(false); }
329+
309330
/**
310-
* Destructor.
311-
* The stream will no longer be discoverable after destruction and all paired inlets will stop delivering data.
331+
* Dispose.
332+
* The stream will no longer be discoverable after disposal (or when disposed by the GC) and all paired inlets will stop delivering data.
312333
*/
313-
~StreamOutlet() { dll.lsl_destroy_outlet(obj); }
334+
public void Dispose()
335+
{
336+
Dispose(true);
337+
GC.SuppressFinalize(this);
338+
}
339+
private bool _disposed = false;
340+
protected virtual void Dispose(bool disposing)
341+
{
342+
if (_disposed)
343+
{
344+
return;
345+
}
346+
347+
dll.lsl_destroy_outlet(obj);
348+
obj = IntPtr.Zero;
349+
_disposed = true;
350+
}
314351

315352

316353
// ========================================
@@ -497,11 +534,29 @@ public StreamInlet(StreamInfo info, int max_buflen = 360, int max_chunklen = 0,
497534
dll.lsl_set_postprocessing(obj, postproc_flags);
498535
}
499536

537+
~StreamInlet() { Dispose(false); }
538+
500539
/**
501-
* Destructor.
502-
* The inlet will automatically disconnect if destroyed.
540+
* Dispose
541+
* The inlet will be disconnect after disposal (or when disposed by the GC)
503542
*/
504-
~StreamInlet() { dll.lsl_destroy_inlet(obj); }
543+
public void Dispose()
544+
{
545+
Dispose(true);
546+
GC.SuppressFinalize(this);
547+
}
548+
private bool _disposed = false;
549+
protected virtual void Dispose(bool disposing)
550+
{
551+
if (_disposed)
552+
{
553+
return;
554+
}
555+
556+
dll.lsl_destroy_inlet(obj);
557+
obj = IntPtr.Zero;
558+
_disposed = true;
559+
}
505560

506561
/**
507562
* Retrieve the complete information of the given stream, including the extended description.
@@ -799,10 +854,27 @@ public class ContinuousResolver
799854
public ContinuousResolver(string pred) { obj = dll.lsl_create_continuous_resolver_bypred(pred, 5.0); }
800855
public ContinuousResolver(string pred, double forget_after) { obj = dll.lsl_create_continuous_resolver_bypred(pred, forget_after); }
801856

802-
/**
803-
* Destructor.
804-
*/
805-
~ContinuousResolver() { dll.lsl_destroy_continuous_resolver(obj); }
857+
~ContinuousResolver() { Dispose(false); }
858+
859+
/// Dispose of the object
860+
public void Dispose()
861+
{
862+
Dispose(true);
863+
GC.SuppressFinalize(this);
864+
}
865+
private bool _disposed = false;
866+
protected virtual void Dispose(bool disposing)
867+
{
868+
if (_disposed)
869+
{
870+
return;
871+
}
872+
873+
dll.lsl_destroy_continuous_resolver(obj);
874+
obj = IntPtr.Zero;
875+
_disposed = true;
876+
}
877+
806878

807879
/**
808880
* Obtain the set of currently present streams on the network (i.e. resolve result).

0 commit comments

Comments
 (0)