4

Since updating to Windows 10 1809, my application appears to be leaking memory on a WinForms C++/CLI TextBox.Text set call.

The textbox is a log updated from a separate device thread logging Bluetooth communications. After a random period of time (sometimes 10 mins, sometimes a few hours) the GUI event handler will lock on the TextBox.Text set call and WorkingSet memory will increase linearly (~ 10MB/s) for around 30s to a minute before recovering. However the allocated commit memory is retained and then built upon the next time it fails. This builds until the application eventually crashes with a stack overflow or out of memory exception.

This is the bones of the Comms event handler:

System::Void MainForm::OnCommsMessageLog(System::Object^ sender, CustomEventArgs::LogMessageEventArgs^ plogMessageEventArgs)
{
    if (InvokeRequired)
    {
        Invoke(gcnew commsMessageDelegate(this, &MainForm::OnCommsMessageLog), sender, plogMessageEventArgs);
    }
    else
    {
        msclr::lock OnCommsMessageLogLock(mpOnCommsLogLock);

        String^ logMessage = plogMessageEventArgs->LogMessage;

        // Pipe out to file to check for log contents
        StreamWriter^ pWriter = gcnew StreamWriter(mLogFilePath + pBthDevice->Name + ".txt", true);
        pWriter->Write(logMessage);
        pWriter->Close();

        mpDeviceLog += logMessage; // Add new log line to String member variable

        if(mpDeviceLog->Length > MAX_LOG_LENGTH) // MAX_LOG_LENGTH = 100000
        {
            mpDeviceLog = mpDeviceLog->Substring(MAX_LOG_LENGTH / 5);
            logTextBox->Text = mpDeviceLog; // <-- LOCKS HERE
        }
        else
        {
            logTextBox->AppendText(logMessage);
            //logTextBox->Text += logMessage; // <-- ALTERNATIVELY LOCKS HERE IF THIS METHOD IS USED
        }
    }
}

This cannot be replicated on any non-1809 machine, which could be coincidental, but seems unlikely.

The machines in question are relatively low spec, running 2GB RAM with a Celeron 1.6GHz processor, but the application is pretty thin, and under full load typically only uses 50MB of WorkingSet memory.

The rate at which it leaks memory appears to be dependant on the contents of the text box at the time of the lockup.


Update

Appears to be an issue with TextBox.MultiLine on any Windows 1809 machine. Can be replicated by creating a small application with a MultiLine text box then running the following code:

        logTextBox.Text = "a\r\n";
        logTextBox.Text = "a\r"; // Will lock here for 30 to 60s

The issue can be rectified by adding in the following between the Text set operations:

        logTextBox.Clear();
        logTextBox.Multiline = false;
        logTextBox.Multiline = true;

Issue has also been raised on MSDN awaiting a reply.

3

0

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.