Skip to content

Commit

Permalink
Fixed source of a memory leak
Browse files Browse the repository at this point in the history
More testing/analysis may be needed but this commit appears to have resolved
the obvious source of memory leakage.

Closes #1
  • Loading branch information
jjcarrier committed Dec 23, 2024
1 parent 0d8388a commit 02d590d
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions libdp100/PowerSupply.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ public bool DebugMode

// A temporary stringwriter instance for suppressing and/or selectively
// parsing stdout for data not exposed via the API.
StringWriter stdoutReceiver = new StringWriter();
StringWriter stdoutReceiver;

// A stream writer for restoring output to the console. This is used in between
// the ApiInstance commands to allow the application to interact with the console.
StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput());

// Mutex to ensure single-access to underlying API.
private static Mutex apiMutex = new Mutex();
Expand All @@ -110,6 +114,7 @@ public PowerSupply()
ApiInstance.ReceBasicInfoEvent += ReceiveActualVI;
// This API does not appear to be functional.
//ApiInstance.DevStateChanageEvent += DevStateChange;
standardOutput.AutoFlush = true;
}

private void DevStateChange(bool state)
Expand Down Expand Up @@ -477,8 +482,7 @@ private ushort ParseUInt16(string[] elems, int offset)

private void ParseSystemData(PowerSupplySystemParams system)
{
StringBuilder sb = stdoutReceiver.GetStringBuilder();
string[] messages = sb.ToString().Split('\n');
string[] messages = stdoutReceiver.GetStringBuilder().ToString().Split('\n');
int sysParamsIndex = -1;
for (int i = 0; i < messages.Length; i++)
{
Expand Down Expand Up @@ -522,8 +526,7 @@ private void ParseSystemData(PowerSupplySystemParams system)

private void ParseBasicInfoData(PowerSupplyActiveState activeState)
{
StringBuilder sb = stdoutReceiver.GetStringBuilder();
string[] messages = sb.ToString().Split('\n');
string[] messages = stdoutReceiver.GetStringBuilder().ToString().Split('\n');
int basicInfoIndex = -1;
for (int i = 0; i < messages.Length; i++)
{
Expand Down Expand Up @@ -862,7 +865,7 @@ private void NullStdOutput()
}

disableOutputLevel++;
stdoutReceiver.Flush();
stdoutReceiver = new StringWriter();
Console.SetOut(stdoutReceiver);
}

Expand All @@ -880,8 +883,7 @@ private void RestoreStdOutput()

if (disableOutputLevel == 0)
{
var standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
stdoutReceiver.Dispose();
Console.SetOut(standardOutput);
}
}
Expand Down

0 comments on commit 02d590d

Please sign in to comment.