Skip to content

Commit 41d0731

Browse files
committed
Fixed some memory leaks in the C# interop layer.
Also fixed some memory leaks in lsl_inlet.c.cpp, related to exceptions.
1 parent 55098eb commit 41d0731

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

LSL.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public class StreamInfo
157157
* To be useful to applications and automated processing systems using the recommended content types is preferred.
158158
* See Table of Content Types in the documentation.
159159
*/
160-
public String type() { return Marshal.PtrToStringAnsi(dll.lsl_get_type(obj)); }
160+
public string type() { return Marshal.PtrToStringAnsi(dll.lsl_get_type(obj)); }
161161

162162
/**
163163
* Number of channels of the stream.
@@ -254,7 +254,13 @@ public class StreamInfo
254254
* b) the misc elements <version>, <created_at>, <uid>, <session_id>, <v4address>, <v4data_port>, <v4service_port>, <v6address>, <v6data_port>, <v6service_port>
255255
* c) the extended description element <desc> with user-defined sub-elements.
256256
*/
257-
public string as_xml() { return Marshal.PtrToStringAnsi(dll.lsl_get_xml(obj)); }
257+
public string as_xml()
258+
{
259+
IntPtr pXml = dll.lsl_get_xml(obj);
260+
string strXml = Marshal.PtrToStringAnsi(pXml);
261+
dll.lsl_destroy_string(pXml);
262+
return strXml;
263+
}
258264

259265

260266
/**
@@ -590,8 +596,13 @@ public double pull_sample(string[] sample, double timeout) {
590596
int ec = 0;
591597
IntPtr[] tmp = new IntPtr[sample.Length];
592598
double res = dll.lsl_pull_sample_str(obj, tmp, tmp.Length, timeout, ref ec); check_error(ec);
593-
for (int k = 0; k < tmp.Length; k++)
594-
sample[k] = Marshal.PtrToStringAnsi(tmp[k]);
599+
try {
600+
for (int k = 0; k < tmp.Length; k++)
601+
sample[k] = Marshal.PtrToStringAnsi(tmp[k]);
602+
} finally {
603+
for (int k = 0; k < tmp.Length; k++)
604+
dll.lsl_destroy_string(tmp[k]);
605+
}
595606
return res;
596607
}
597608

@@ -625,10 +636,16 @@ public int pull_chunk(string[,] data_buffer, double[] timestamp_buffer, double t
625636
int ec = 0;
626637
IntPtr[,] tmp = new IntPtr[data_buffer.GetLength(0),data_buffer.GetLength(1)];
627638
uint res = dll.lsl_pull_chunk_str(obj, tmp, timestamp_buffer, (uint)tmp.Length, (uint)timestamp_buffer.Length, timeout, ref ec);
628-
for (int s = 0; s < tmp.GetLength(0); s++)
629-
for (int c = 0; c < tmp.GetLength(1); c++)
630-
data_buffer[s,c] = Marshal.PtrToStringAnsi(tmp[s,c]);
631639
check_error(ec);
640+
try {
641+
for (int s = 0; s < tmp.GetLength(0); s++)
642+
for (int c = 0; c < tmp.GetLength(1); c++)
643+
data_buffer[s,c] = Marshal.PtrToStringAnsi(tmp[s,c]);
644+
} finally {
645+
for (int s = 0; s < tmp.GetLength(0); s++)
646+
for (int c = 0; c < tmp.GetLength(1); c++)
647+
dll.lsl_destroy_string(tmp[s,c]);
648+
}
632649
return (int)res / data_buffer.GetLength(1);
633650
}
634651

0 commit comments

Comments
 (0)