What compiler are you using ?
Protocol buffers definitely don't behave this way, so only a very
broken compiler or something peculiar in your memory management or
other setup can cause this.
(IIRC, default values in protobufs share a const instance (in this
case: the empty string) for memory reasons, so you would see the same
address for the default values, but this is of course disengaged
whenever a value is set.)
If you just create a simple protocol buffer with two string fields and
a simple main() function: do you get the same behavior ?
-h
> --
> You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
> To post to this group, send email to prot...@googlegroups.com.
> To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.
>
I think if you make a stand alone unit test for this, you won't see this behaviour.
--Chris
One idea I can think of is that you forgot to allocate the Bid
structure and your compiler sets unallocated pointers to some default
value. Or Bid is already freed (a common technique in memory
allocators is to fill the deleted value with some pattern in debug
mode to detect this early). Setting values in your case then
accidentally does not crash but modifies the data structure the
garbage pointer happens to point to.
How do you get the instance of Bid ? It is a bit hard to debug your
code without seeing a self-contained snippet that exposes the problem.
In your example, you're using b.set_.... Is 'b' a reference in this
case ? Could it be that you set values on a reference to a temporary
that already is gone ?
Try to run your program with valgrind maybe to find a broken allocation.
(Note it is very very unlikely that the protobuf C++ implementation
has such a bug that is undetected, so something else in your program
is going wrong.)
-h
Currently using protobuf under its 2.5.0 version, I am stuck with a problem I am struggling to resolve. First of all, the following .proto files represent the messages I am dealing with :
MacroMission.proto message MacroMission{ required int32 timeWithoutCom=1; required string name=2; required Phase failSafePhase=3; repeated Phase phases=4; } message Phase{ optional Survey survey=1; } message Survey{ repeated Segment trajectory=1; required string sonarPreset=4; } message Segment{ required WayPoint start=1; required WayPoint end=2; } Common.proto message WayPoint { required GeoPoint pos = 1; // Not relevant optional string label = 2 ; }The aim of the software is to fill a MacroMission message. I am working under Windows 7, with C++/Qt and minGW compiler, using the protobuf library within Qt Plugin projects (a message is shared between two distinct plugins, a « creator » and a « filler »).
My problem is : whenever I try to assign a value to a field of string type, all the other string fields are taking the same value. My first guess was that, somehow, all these fields had the same address in memory, which turned out true. Concerned about the good initialization of the messages, I double checked all the process for creating the MacroMission. It could be best described by the following lines :
Classe A – Plugin 1 ::pb::MacroMission mission; mission.set_name(rMissionName.toStdString()); mission.set_timewithoutcom(timeWithoutCom); foreach(MissionClass) { ::pb::Phase * missionPhase = mission.add_phases(); MissionClass->FillPhase(missionPhase); } MissionClass – Plugin 2 FillPhase methode (TnamedSegment is a support class for segment data access) ::pb::Survey * survey = missionPhase->mutable_survey(); for(int segmentRank = 0 ; segmentRank < surveyParams.getSegments().count() ; ++segmentRank) { TNamedSegment segment = surveyParams.getSegments()[segmentRank]; qDebug() << "Adding one segment :"; ::pb::Segment* pb_segment = survey->add_trajectory(); pb_segment->mutable_start()->set_label(QString("WP_%1_start").arg(segmentRank).toStdString()); pb_segment->mutable_start()->mutable_pos()->set_latitude(segment.startWP.latitude); pb_segment->mutable_start()->mutable_pos()->set_longitude(segment.startWP.longitude); pb_segment->mutable_end()->set_label(QString("WP_%1_end").arg(segmentRank).toStdString()); pb_segment->mutable_end()->mutable_pos()->set_latitude(segment.endWP.latitude); pb_segment->mutable_end()->mutable_pos()->set_longitude(segment.endWP.longitude); } survey->set_sonarpreset(surveyParams.getSonarPreset().toStdString());In the end, nothing comes out of the blue in this code. But, as I previously said, all the protobuf labels/names in the messages have the same address. Therefore, they will all end up with the same value, the one of the last field set (in this case, the value of SonarPreset). I have been able to solve a little tiny part of the problem by using the set_allocated_var method, but in the end, the major part of the problem remains unchanged.
After several hours of internet researches, many regenerations/rebuilds of the protobuf messages and of the project in which they are used, memory allocation double checks, and protobuf library help documentation readings, I come to you in order to know if, by any change, someone would have the beginning of a solution to my problem.
I did a small unit test program which only fill the protobuff message in a single "main.cpp" file, and everything works fine...
Thank you in advance for any answer.
PS : Please let me know if you need any further information in order to clarify the problem.