On Fri, Aug 31, 2012 at 7:50 AM, Will <w...@idairco.com> wrote:

> Hi all,
>
> I was wondering if anyone has seen a memory leak originating from calls to
> MessageLite::ParseFromArray? I am using protobuf 2.4.1 on Ubuntu 12.04.
>
> I'm currently transporting OpenCV Mat objects across the network to a Qt
> based display application. Google's protocol buffers are used to define the
> message format and parse the messages.
>
> The message format is fairly simple:
>
> package aos;
>
> message MatMessage {
> required int32 width = 1;
> required int32 height = 2;
> required int32 type = 3;
> repeated bytes data = 4;
> }
>
> I also searched and did the recommended ShutdownProtobufLibrary but that
> did not clear the valgrind errors. Here is the code I used to parse the
> OpenCV Mat's:
>
> do
> {
> server.waitForNewConnection(LISTEN_TIMEOUT, &serverTimedOut);
>
> if(!serverTimedOut && server.hasPendingConnections()) // we have a new
> connection waiting...
>  {
> QTcpSocket* socket = server.nextPendingConnection(); // the client we are
> talking to currently...
>  qDebug() << "Connected to " << socket->localAddress().toString() << ":"
> << socket->localPort();
>
> // read the raw message data from the socket
> QByteArray data;
>  do
> {
> socket->waitForReadyRead(-1);
>  data.append(socket->readAll());
> socket->waitForReadyRead(-1);
>  } while(socket->bytesAvailable() > 0);
>
> socket->disconnectFromHost();
>
> // create the message
> idair::IdAirMessage message;
>  message.ParseFromArray(data.constData(), data.size()); // <-- this line
> appears to be the leak source
>
>  // create a cv::Mat from the MatMessage object...
> cv::Mat raw, segmented;
>  if(message.ByteSize() > 0)
> {
>  raw = MatMessage2Mat(message.mutable_rawimage());
> segmented = MatMessage2Mat(message.mutable_processedimage());
>
> CustomMat customOutput(raw);
> emit imageReceived(customOutput);
>  }
>
> message.release_rawimage();
>  message.release_processedimage();
>

These two lines are explicitly releasing submessages in the container
message. You need to take ownership of these and delete them, or don't call
these  two methods and just let the container IdAirMessage delete them when
it goes out of scope.

The valgrind stack trace is just reporting where they were being allocated,
which is in ParseFromArray. The leak disappears because the message is
never modified and thus the submessages never added, so there is nothing to
release later.

}
>
> } while(!isStopped());
>
> Finally, in the class destructor I call the shutdown method:
>
> ServerThread::~ServerThread() {
> google::protobuf::ShutdownProtobufLibrary();
> }
>
> I have attached the valgrind log for reference...as you can see at the
> bottom of the file I have about 40MB of imagery being leaked through
> ParseFromArray; however, if I comment out this call the leak disappears...
>
> Thanks for the help!
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/protobuf/-/k9NZxJUMXcMJ.
> To post to this group, send email to protobuf@googlegroups.com.
> To unsubscribe from this group, send email to
> protobuf+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/protobuf?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.

Reply via email to