Pipe a tcpdump stream to OpenFrameworks program

Hi!

I’m working on an experiment with piping a tspdump stream to the standard in of my OpenFrameworks application. By doing this i ran into some problems with program execution (the application froze while watching the feed). I’ve managed to fix this by creating a new thread for watching the standard in.

This works fine until the app crashes with the error:

pointer being freed was not allocated

This is how i read the standard in:

// file.h
while(isThreadRunning())
{    
    for (string line; getline(std::cin, line);)
    {
        inputLines.push_back(line);
    }
}

As you can see i push the input to an array called inputLines.
I try to keep this array to a certain size, e.g. 10, like this:

// file.cpp in update()
if (inputLines.size() > 10) {
    inputLines.erase(inputLines.begin(), inputLines.end() - 10);
}

My guess is that it goes wrong here, something like it tries to erase a thing dat does not exist? but i’m not sure…

Next i add al the lines to one string so i can draw it.

// file.cpp in update()
for (int i = 0; i < inputLines.size(); i++)
{
    if (!inputLines.at(i).empty()) {
        text += inputLines.at(i)+" \n";
    }
}

So this runs fine but after let’s say a 30 seconds the app crashes with the error: pointer being freed was not allocated.

The full source is on github:
cpp file
h file

Many thanks!
I’m kinda stuck with this one!

you can’t access a collection like that from 2 different threads without using locks. you should lock while adding new elements, checking it’s size or removing elements. to lock use lock() and unlock()

from 0.9 there’s a new class ofThreadChannel that allows to do this without locking

Ah thanks! Works like a charm now!
Feeling kind of stupid now since this is clearly documented in the docs… :sweat_smile: