Ah, yes, I will update that loop.

I'm still a little confused about something though. Ultimately I am
trying to create an entry, and set a field, within this nested-
repeated message. However, how can I create an entry without first
getting the reflection for the nested message? My .proto file is as
follows:

package db;
message Database {

message Team {
required string name = 1;
optional string city = 2;
}

// true if the data should be stored to the database
required bool write = 1;

 // fill this in to ask about the items in the 'roster' or
'league'
optional string query = 2;

message Player {
required string name = 1;
optional float batting_avg = 2;
}
// list of players
repeated Player roster = 3;
// list of teams
repeated Team league = 4;
}


I am trying to dynamically create a new entry in roster, and set the
name and batting_avg of the Player within that entry.

This is my first time using protocol buffers, and I feel like I must
be missing a fundamental step here =)

Thanks for the help!

-Ryan


On May 4, 12:23 pm, Jason Hsueh <jas...@google.com> wrote:
> Your msg is completely empty. There aren't any entries in the repeated field
> - FieldSize() should be returning 0. Your loop needs to terminate when i <
> size, not i <= size - accessing the 1st element of an empty repeated field
> is going to return an invalid pointer.
>
> FYI bounds checking is done in debug mode, so you'd get more informative
> assertion failures rather than segfaults.
>
>
>
> On Mon, May 3, 2010 at 10:12 PM, Ryan <droopyk...@gmail.com> wrote:
> > Hello all,
>
> > I am getting a segmentation fault when trying to use the
> > MutableRepeatedMessage() class with a nested repeated method. My
> > simple C++ code is as follows:
>
> > /* Decleration in bufferDB.h*/
> >        private:
> >                        std::string serialData;
> >                        Message* msg;
> >                        Message* nestedMsg;
> >                        const Descriptor* msgDescript;
> >                        const Reflection* msgReflect;
> >                        const FieldDescriptor* msgField;
>
> >                        const Descriptor* nestedDescript;
> >                        const Reflection* nestedReflect;
> >                        const FieldDescriptor* nestedField;
>
> > /*Definition in bufferDB.cpp */
> > msg = new db::Database;
> >        msgDescript = msg->GetDescriptor();
> >        msgReflect = msg->GetReflection();
> >        msgField = msgDescript->field(2);
>
>        //msgReflect->MutableRepeatedMessage(msg, msgField, 0)-
>
> > >DiscardUnknownFields();
>
>         if (msgField->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE)
>
>
>
> >         {
> >                printf("Field is a MESSAGE\n");
> >              if (msgField->is_repeated())
> >              {
> >                        printf("Field is Repeated\n");
> >                int size = msgReflect->FieldSize(*msg, msgField);
> >                        printf("Field size is %d\n",size);
> >                for (int i = 0; i <= size; i++)
> >                {
> >                        printf("I went inside this loop");
> >                        nestedReflect =
> > msgReflect->MutableRepeatedMessage(msg, msgField,
> > i)->GetReflection();
>
> >                }
> >              }
> >              else
> >              {
> >                        printf("Field is NOT repeated");
> >                const Message& sub_message = msgReflect->GetMessage(*msg,
> > msgField);
> >              }
> >          }
>
> > The segmentation fault occurs when I call GetReflection(). Am I doing
> > something wrong with the use of the mutable message pointer being
> > returned from MutableRepeatedMessage?
>
> > Any help anyone could give me would be great. Thanks!
>
> > -Ryan
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Protocol Buffers" group.
> > To post to this group, send email to proto...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > protobuf+unsubscr...@googlegroups.com<protobuf%2bunsubscr...@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 proto...@googlegroups.com.
> To unsubscribe from this group, send email to 
> protobuf+unsubscr...@googlegroups.com.
> For more options, visit this group 
> athttp://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 proto...@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