Model crashed after static_cast
-
Hello, let me represent the problem:
- I'm populating my model with Item object
class Item{ .......... void installData(Data* ptr); Data *data(); .......... }
Now suppose this:
1)AdditionalData class is inherited from Data
2)Now in the installData function i've put the initialized AdditionalData pointer;And now getting the problem:
data(const QModelIndex& index, int role)const{ Item* item=reinterpret_cast<Item*>(index.internalPointer()) Data* itemData=item.data(); AdditionalData* addtionalData=static_cast<AdditionalData*>(itemData); //IF I CALL ANY METHOD FROM addtionalData THE PROGRAM WILL CRASH otherwise everything is fine }
Any suggestions? Thanks in advance;
-
Hello, let me represent the problem:
- I'm populating my model with Item object
class Item{ .......... void installData(Data* ptr); Data *data(); .......... }
Now suppose this:
1)AdditionalData class is inherited from Data
2)Now in the installData function i've put the initialized AdditionalData pointer;And now getting the problem:
data(const QModelIndex& index, int role)const{ Item* item=reinterpret_cast<Item*>(index.internalPointer()) Data* itemData=item.data(); AdditionalData* addtionalData=static_cast<AdditionalData*>(itemData); //IF I CALL ANY METHOD FROM addtionalData THE PROGRAM WILL CRASH otherwise everything is fine }
Any suggestions? Thanks in advance;
@Ivan-Netskin Well, you should ALWAYS check the pointer after casting. And it should be reinterpret_cast, not static_cast.
If itemData is Data and not AdditionalData the cast will fail and you will get a null pointer. -
@Ivan-Netskin Well, you should ALWAYS check the pointer after casting. And it should be reinterpret_cast, not static_cast.
If itemData is Data and not AdditionalData the cast will fail and you will get a null pointer.@jsulm
yes, i know, i should. I just wrote it for simplicity. In my code i have just 1 item with initialized AdditionalData so it's for sure Item contains pointer to the AdditionalData object -
@jsulm
yes, i know, i should. I just wrote it for simplicity. In my code i have just 1 item with initialized AdditionalData so it's for sure Item contains pointer to the AdditionalData object@Ivan-Netskin Why do you use static_cast?
Did you check whether addtionalData is nullptr? -
yes i did and it is not, i can get some additional info from the pointer and after that the program will crash. Like i said, if u don't touch the pointer(not calling any of it's member functions) the program will not crash.
-
yes i did and it is not, i can get some additional info from the pointer and after that the program will crash. Like i said, if u don't touch the pointer(not calling any of it's member functions) the program will not crash.
@Ivan-Netskin
I think you should check ifadditionalData
is realy aAdditionalData*
-
@Ivan-Netskin
I think you should check ifadditionalData
is realy aAdditionalData*
Okay, the code was correct!
the line caused the crash :qDebug()<<model->data(model->index(1,0),Qt::DisplayRole);
because there is only 1 item like i said. Thanks guys anyway!
-
@Ivan-Netskin Well, you should ALWAYS check the pointer after casting. And it should be reinterpret_cast, not static_cast.
If itemData is Data and not AdditionalData the cast will fail and you will get a null pointer.@jsulm said in Model crashed after static_cast:
And it should be reinterpret_cast, not static_cast.
I think you made a typo here, it must be
dynamic_cast
.@Ivan-Netskin said in Model crashed after static_cast:
Okay, the code was correct!
With those
reinterpret_cast
s (here it's necessary, I know, but at least check forNULL
) andstatic_cast
s you're on a slippery slope ... neither is safe casting, so you may at some point in the future spend some hours debugging them.