jonnydee / nzmqt

nzmqt - A lightweight C++ Qt binding for ZeroMQ
Other
199 stars 72 forks source link

Memory Leak when sending images #32

Closed pauloloid closed 8 years ago

pauloloid commented 8 years ago

Hello,

I use the NZMQTINLINE bool ZMQSocket::sendMessage(const QByteArray& bytes, SendFlags flags_) function for sending a QByteArray of an QImage.

So I call socket_->sendMessage(getImageCapture()); where getImageCapture() is:

QByteArray Example::getImageCapture()
{
    /* newimage holds an pointer to the image */
    QBuffer buffer;    
    if(!newImage)
        return NULL;   
    newImage->save(&buffer, "PNG");
    delete newImage;
    return buffer.buffer();
}

Everytime the memory increases. After a certain time I get a stackoverflow.

How can I handle this?

machinekoder commented 8 years ago

sendMessage uses QByteArray. Try to use getImageCapture().buffer()

If the problem still persists this could mean that there actually is a memory leak in nzmqt. I have heard similar problems from a user. However, I rarely send huge amounts of data so the error may cumulate not as fast as for you.

machinekoder commented 8 years ago

Looking at ZMQMessage it definitely copies data before sending (why?) https://github.com/jonnydee/nzmqt/blob/8f71eac671eef8444c8307deeb955a0b3acb7e5a/include/nzmqt/impl.hpp#L70 @jonnydee where is this data freed?

pauloloid commented 8 years ago

Hello,

I am sorry, I just recognized, it does not have something to do with ZeroMQ or nzmqt.

The way I got my QImage (by signal and slot) was designed wrong.

Anyway, thank you for your fast response!

machinekoder commented 8 years ago

Yes, implementation looks correct. zmq frees the message_t automatically. However, you may consider constructing your own ZMQMessage objects in order to minimize overhead.