Addressing memory leak issues while streaming serial device data to the GTK text buffer

I’m creating a Windows application that can talk to several serial devices. The Win32 API manages data sending and receiving to a device, whereas GTK3 manages the GUI. The device polling loop is conducted by a worker thread generated by g thread new, and the text view/buffer is updated by g timeout add. If it is determined that a new line needs to be inserted, the device’s output, which is ASCII text when it is streaming, is then put into a buffer. The polling cycle never ends until a disconnection function is activated, at which point the resources are cleaned up.

I’ve found a memory leak, which I think originates from the buffer update function. I no longer have a leak when this function is commented out (i.e., nothing is placed into the buffer).`gboolean
updateViewport (gpointer data) {

SerialDevice *device = (SerialDevice *) data;
device = getDeviceById (device->id);

if (device != NULL) {

if (device->dataStringBuffer[device->lineCount - 1] != NULL && device->newDataForDisplay == PENDING) {

    GtkTextMark *mark;
    GtkTextIter start, end;

    mark = gtk_text_buffer_get_insert ( device->viewPort->textViewBuffer);
    gtk_text_buffer_get_iter_at_mark ( device->viewPort->textViewBuffer, &iter, mark );
    gtk_text_buffer_get_bounds ( device->viewPort->textViewBuffer, &start, &end );
    gtk_text_buffer_get_iter_at_offset (device->viewPort->textViewBuffer, &iter, -1);

    if (device->viewPort->linesDisplayingInBuffer > lineLimit) {
        gtk_text_buffer_delete(device->viewPort->textViewBuffer, &start, &end);
        device->viewPort->linesDisplayingInBuffer = 0;
    }

    gtk_text_buffer_insert (device->viewPort->textViewBuffer, &iter, device->dataStringBuffer[device->lineCount - 1], -1);

    device->newDataForDisplay = EMPTY;
    device->viewPort->linesDisplayingInBuffer++;

    gtk_text_buffer_delete_mark(device->viewPort->textViewBuffer, mark);
}

}

return G_SOURCE_CONTINUE;
}`

I want to restrict the user’s ability to scroll over a certain number of lines for the time being. I’m using the gtk text buffer delete method to empty the buffer whenever the limit is reached. In order to release the reference to the mark I made, I am using the gtk text buffer delete mark function.

The incoming data is copied into the dataStringBuffer, an array of strings, and then inserted into the text view buffer after being read into the incomingBuffer. When a device is created, memory is allocated for the dataStringBuffer and removed when the device is disconnected.

 if (bitsRead > 1) {
    // Reset lineCount to overwrite the lines that have already been displayed
    if (device->lineCount == DATA_STRING_BUFFER_SIZE - 1) {
        device->lineCount = 0;
    }

    g_strlcpy(device->dataStringBuffer[device->lineCount], device->incomingBuffer, INCOMING_BUFFER_SIZE);
    device->lineCount++;
    device->newDataForDisplay = PENDING;

    PurgeComm(device->hSerialComm, PURGE_RXCLEAR);
    memset(device->incomingBuffer, 0, INCOMING_BUFFER_SIZE);
}

When only apk device is streaming to a buffer, memory usage is minimal; however, this is not the usual. The impact of the leak is increased because there will normally be 6 to 8 devices streaming simultaneously, each on its own worker thread. Although I’m not familiar with the workings of the GTK library, my suspicion is that references are not being released when I execute gtk text buffer delete to remove the lines from the buffer.

Any suggestions, criticisms, and feedback are welcome.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.