Boost logo

Boost Users :

Subject: [Boost-users] newbie question - memory leak while deserializing an object
From: Moldovanyi Orban (contiboost_at_[hidden])
Date: 2010-02-05 17:31:21


Hi there! :)

I've recently become familiar with boost, therefore I'm quite newbie and
maybe the answer to my question is trivial.

Below there is a demonstrative code fragment from a test client application
where a server app periodically sends serialized objects to the client. The
client reads the socket, re-constructs the serialized object from the stream
using boost::archive::text_iarchive and makes some operation on it. Code is
very simple and it works fine, however the memory is leaking. I think there
is a problem with the usage of text_iarchive because if I comment the
appropriate line out, the leaking stops. Of course the
incomingTransferObject (and everything else in the progbam) are destructed
properly, they does nothing with the leak.

Here comes the code fragment:

char buffer[8192]; // class memeber, it contains data received on the socket

...

void Client::handleReadMessage(const boost::system::error_code& error,
std::size_t bytes_transferred)
{
// blah-blah

...

TransferObject* incomingTransferObject = new TransferObject();
std::istringstream archive_stream(buffer);
boost::archive::text_iarchive archive(archive_stream);
*archive >> incomingTransferObject;*

//...
// do something with the object
//...

delete incomingTransferObject;

tcpSocket->write_some(boost::asio::buffer("1")); // ack to the server

boost::asio::async_read(
*tcpSocket,
boost::asio::buffer(sizeBuffer), boost::asio::transfer_at_least(1),
boost::bind( &Client::handleReadHeader, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)
);
}

The TransferObject is a temporary class just to test serializing more
complex objects with stl containers. It looks like this:

class TransferObject
{
    friend class boost::serialization::access;

    template<typename Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & strvector;
        ar & strmap;
        ar & complexSet;
    }
    private:
        vector<string>* strvector;
        map<int,string>* strmap;
        set<HelperObject*>* complexSet;
    ...
}

class HelperObject
{
    friend class boost::serialization::access;

    template<typename Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & b;
        ar & f;
    }

    private:
        bool b;
        float f;

...

}

>From my point of view it seems that the text_iarchive makes some memory
allocations during its operation but their references are lost after the
deserialization which cause the leak, anyway I'm almost absolutely sure that
I'm using this technique badly and there is a simple solution for this
problem.

Thanks for reading this and I hope an expert can answer. :]

Laszlo



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net