I'm using protobuf in Qt platform. function A and B are slot functions:
function A : void MainWindow::pushButton_load_clicked();
function B : void MainWindow::listWidget_LBM_itemClicked(QListWidgetItem *item);
dic : QHash<QString, const Message*> dic;
In function A, I store the pointer to Message to a hashtable.
for (auto message_ptr : res)
{
// ...
dic[funcid] = message_ptr;
}
In function B, I get the pointer to Message and use protobuf API to get the number of fields in the Message.
// ...
if(dic.find(funcid) == dic.end())
{
qDebug() << "funcid " << funcid << " does not exist in dic" << endl;
return;
}
const Message* message_ptr = dic[funcid];
const Descriptor* desc = message_ptr->GetDescriptor();
qDebug() << "funcid is : " << funcid << "desc->field_count() : " << desc->field_count() << endl;
But when I call function B for several times, the result of descriptor->field_count() is different. the output is:
funcid is : "30001" desc->field_count() : 0
funcid is : "30007" desc->field_count() : 6881390
the correct answer is 16 and 5.
I have tried to test whether the two pointers to Descriptor of one Message in function A and function B are the same. The answer is yes.
const Message* m1,m2,m3;
const Descriptor* d1,*d2,*d3;
int cnt = 0;
int f1, f2, f3;
function A:
dic[funcid] = message_ptr;
if(cnt == 0)
{
m1 = message_ptr;
d1 = m1->GetDescriptor();
f1 = d1->field_count();
++cnt;
}
else if(cnt == 1)
{
m2 = message_ptr;
d2 = m2->GetDescriptor();
f2 = d2->field_count();
++cnt;
}
else
{
m3 = message_ptr;
d3 = m3->GetDescriptor();
f3 = d3->field_count();
}
function B:
const Message * message_ptr = dic[funcid];
if(funcid == "30007")
{
auto d = message_ptr->GetDescriptor();
if(message_ptr == m1)
qDebug() << "equals m1" << endl;
else
qDebug() << "not equals m1" << endl;
if(d == d1)
qDebug() << "equals d1" << endl;
else
qDebug() << "not equals d1" << endl;
if(d->field_count() == f1)
qDebug() << "equals f1" << endl;
else
qDebug() << "not equals f1" << endl;
}
else if(funcid == "30001")
{
auto d = message_ptr->GetDescriptor();
if(message_ptr == m2)
qDebug() << "equals m2" << endl;
else
qDebug() << "not equals m2" << endl;
if(message_ptr->GetDescriptor() == d2)
qDebug() << "equals d2" << endl;
else
qDebug() << "not equals d2" << endl;
if(d->field_count() == f2)
qDebug() << "equals f2" << endl;
else
qDebug() << "not equals f2" << endl;
}
else
{
auto d = message_ptr->GetDescriptor();
if(message_ptr == m3)
qDebug() << "equals m3" << endl;
else
qDebug() << "not equals m3" << endl;
if(message_ptr->GetDescriptor() == d3)
qDebug() << "equals d3" << endl;
else
qDebug() << "not equals d3" << endl;
if(d->field_count() == f3)
qDebug() << "equals f3" << endl;
else
qDebug() << "not equals f3" << endl;
}
the output is :
equals m1
equals d1
not equals f1
equals m2
equals d2
not equals f2
equals m3
equals d3
equals f3
Does anyone know the reason? Thanks in advance.
I'm expecting to get the correct result of descriptor->field_count() in function B.
Aucun commentaire:
Enregistrer un commentaire