jeudi 9 septembre 2021

protobuf DynamicMessage reflection AddMessage pure virtual method called?

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?





Aucun commentaire:

Enregistrer un commentaire