Glad that you found what works for you. What baffles me is why CopyFrom() 
segfaults when given a DynamicMessage.  DynamicMessage can't be cast to 
Foo, but foo.CopyFrom() gets to be working with it. Did you find why it 
segfaults?

On Friday, January 18, 2013 3:31:48 AM UTC+8, m wrote:
>
> Thanks for your reply.
>
> My application links against all the stub proto sources of the types I 
> will be creating.  What I've found works is if I use the default(?) 
> descriptor pool (google::protobuf::DescriptorPool::generated_pool()) and 
> the default message factory 
> (google::protobuf::MessageFactory::generated_factory()).  Given the type 
> name and the content, I have a simple function that more or less does this:
>
> const gpb::Descriptor* descriptor = 
> gpb::DescriptorPool::generated_pool()->FindMessageTypeByName( 
> "my.package.Foo" );
>
>  
>
> gpb::MessageFactory* factory = gpb::MessageFactory::generated_factory();
>
>  
>
>
> const gpb::Message* prototype = factory->GetPrototype( descriptor );
>
>  
>
>
> gpb::Message* message = prototype->New();
>
>  
>
> gpb::Message* m = gpb::TextFormat::ParseFromString( "blah: \"some string\" 
> bleh: 3", message );
>
>
>
>
>
> On Wednesday, January 16, 2013 4:15:53 PM UTC-8, m wrote:
>>
>> Hi All,
>>
>> I'm seeing an incompatibility between a dynamically generated 
>> google::protobuf::Message (DynamicMessage?) created with a 
>> google::protobuf::DynamicMessageFactory and a normal 
>> google::protobuf::Message.
>>
>> What I'm running up against ultimately comes down to this: I can't 
>> dynamic_cast a DynamicMessage to a known derived type of 
>> google::protobuf::Message.  For example, let's say my proto file has the 
>> following message:
>>
>> package my.package;
>>
>> messsage Foo
>> {
>>   string blah = 1;
>>   uint32 bleh = 2;
>> }
>>
>>
>> and I successfully create a my::package::Foo with some code along the 
>> lines of:
>>
>> const google::protobuf::Descriptor* descriptor = 
>> myImporter.pool()->FindMessageTypeByName( 
>> "my.package.Foo" );
>>
>> google::protobuf::Message* message = myDynamicMessageFactory.GetPrototype( 
>> descriptor )->New();
>>
>>
>> after populating message successfully with other commands not shown 
>> here, I try to do something like:
>>
>> my::package::Foo foo;
>> foo.CopyFrom( *message );
>>
>>
>> and I get a segfault in CopyFrom() because ultimately the following 
>> doesn't work (the pointer pFoo ends up being NULL down inside 
>> CopyFrom()/MergeFrom()):
>>
>> my::package::Foo* pFoo = dynamic_cast< my::package::Foo* >( message );
>>
>>
>> It appears as though the derived type of message is not actually 
>> my::package::Foo, but instead google::protobuf::DynamicMessage (or 
>> something like that), so naturally the dynamic_cast fails.  I was sure to 
>> observe the output of message->DebugString() and message->GetTypeName() 
>> which returned all the appropriate information.
>>
>> Is there any way to get a real my::package::Foo object from a dynamic 
>> one?
>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/protobuf/-/sC2xjV3x8F0J.
To post to this group, send email to protobuf@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