c++ thrift serialize multiple objects to vector<string> only end with the last object result

206 Views Asked by At

The code is :

std::vector<std::string*> itemDataList;
for(int i = 0 ; i<4; i++){
    ctr::ZwItem zwItem;
    zwItem.news = newsList[i];
    zwItem.__isset.news = true;

    boost::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
    TCompactProtocol compactProtocol(buffer); 

    zwItem.write(&compactProtocol);
    std::string itemData = buffer->getBufferAsString();
    INFO << newsId << " size: " << std::to_string(itemData.size());
    itemDataList.emplace_back(&itemData);

}

std::ostringstream yy;
for(auto p : itemDataList){
    yy << std::to_string(p->size()) << ",";
}
MI << "sizeList: [" << yy.str() << "]";


and the running log shows this: enter image description here

when I print the serialized string sizes after the for loop, the 4 string pointers in itemDataList have the same size , not the expected [3033, 2581, 2938, 2458].

So what's the problem with my code?

0

There are 0 best solutions below