I'm parsing message by runtime proto-file. I constructed "prototye 
message", and tried to get reflection infos of each fields. when it comes 
to nested message, I need to `AddMessage` to get the inside fields.

// proto-file
```
message A {
   int32  a1 = 1;
   string a2 = 2;
};
message B {
   repeated A b1 = 1;
   int64      b2 = 2;
};
```

```
Class ProtoType {
Message* msg;
BizX* biz_x;
void Init(const std::string& proto_file_path) {
    // parse from file to get msgDesc
    auto* factory = new google::protobuf::DynamicMessageFactory();
    factory->SetDelegateToGeneratedFactory(true);                          
                                       
    msg= mFactory->GetPrototype(msgDesc);         
}
void InitRefl() {
    biz_x = new BizX();
    biz_x->Init(*(msg->New()));
}
};
```

```
class BizX {
BizY* biz_y;
Reflection* refl;
Descriptor* desc;
void Init(const Message& msg) {
    refl = msg.GetReflection();
    desc = msg.GetDescriptor();

    const auto* field_desc = desc->FindFieldByName("b1");
    auto* p = msg.New();
    auto* tmp = refl->AddMessage(p, field_desc);

    biz_y = new BizY();
    biz_y->Init(msg);
} 
};
```

```
class BizY {
Reflection* refl;
Descriptor* desc;
void Init(const Message& msg) {
    refl = msg.GetReflection();
    desc = msg.GetDescriptor();

    const auto* field_desc = desc->FindFieldByName("b1");
    auto* p = msg.New();
    auto* tmp = refl->AddMessage(p, field_desc); // crash!!!:pure virtual 
method called
}
};
```

```
int main() {
    ProtoType* p = new ProtoType();
    p->Init("proto-file")
    p->InitRefl();
    return 0;
}
```

I thought `BizY::Init` should have the same result as `BizX::Init`, but it 
crashed, why?

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/protobuf/36e02587-564c-4771-be2f-7d6a4e0609cen%40googlegroups.com.

Reply via email to