Hi,

Writing or reading a std::vector<std::string> to DataOStream or from 
DataIStream will lead to corrupted memory and hard to track down bugs.
This is because the specializations of operator << and >> for std::vector do 
things like
write( &value[0], nElems * sizeof( T ) ); and
read( &value[0], nElems * sizeof( T ) );

This will bypass the constructor & copy constructor of std::string, which is 
not good.

Therefore, I propose to change the operators to do the following:

operator <<:
{
    const uint64_t nElems = value.size();
    write( &nElems, sizeof( nElems ));
    for ( int i = 0; i < nElems; i++ )
    {
        (*this) << value[i];
    }
    return *this;
}

operator >>:
{
    uint64_t nElems = 0;
    read( &nElems, sizeof( nElems ));
    value.resize( nElems );
    for ( int i = 0; i < nElems; i++ )
    {
        (*this) >> value[i];
    }
    return *this;
}

This fixes the problem.

Regards,
Thomas

Attachment: signature.asc
Description: This is a digitally signed message part.

_______________________________________________
eq-dev mailing list
[email protected]
http://www.equalizergraphics.com/cgi-bin/mailman/listinfo/eq-dev
http://www.equalizergraphics.com

Reply via email to