The program has unexpectedly stop + segmentation fault
-
Hi everyone, I'm just starting with some Qt Game development, but I recently stumble upon a problem that I can't solve. Can you help me please?
Here is my code:collisionList += collidingItems(); //I update the list here for(qint64 i = 0; i < collisionList.size(); ++i) { if(typeid(*(collisionList[i])) == typeid(H_Bullet)) {//if ennemy touches bullet, remove both bullet and ennemy emit ((H_Bullet*)collisionList[i])->touched();
When I run my code it just randomly stops working. with no further explanation.
When I go in debug mode, I find a segmentation fault in the line with the if or in the line with the emit.
I have no idea how to fix this. My best guess is that when I update the list I somehow break something. It could also be a race condition, since I'm deleting items in another file too.
Any suggestion are welcome, it wold even be better if you could explain me how to track down root cause of segmentation fault in the future. -
Hi everyone, I'm just starting with some Qt Game development, but I recently stumble upon a problem that I can't solve. Can you help me please?
Here is my code:collisionList += collidingItems(); //I update the list here for(qint64 i = 0; i < collisionList.size(); ++i) { if(typeid(*(collisionList[i])) == typeid(H_Bullet)) {//if ennemy touches bullet, remove both bullet and ennemy emit ((H_Bullet*)collisionList[i])->touched();
When I run my code it just randomly stops working. with no further explanation.
When I go in debug mode, I find a segmentation fault in the line with the if or in the line with the emit.
I have no idea how to fix this. My best guess is that when I update the list I somehow break something. It could also be a race condition, since I'm deleting items in another file too.
Any suggestion are welcome, it wold even be better if you could explain me how to track down root cause of segmentation fault in the future.Hi and welcome to devnet forum
collisionList seems to pointers to item. Probably you have an unassigned pointer in there.
collisionList += collidingItems(); //I update the list here for(qint64 i = 0; i < collisionList.size(); ++i) { if ( collisionList[i] ) { if(typeid(*(collisionList[i])) == typeid(H_Bullet)) {//if ennemy touches bullet, remove both bullet and ennemy emit ((H_Bullet*)collisionList[i])->touched(); }
The introduced pointer check above may help. However, it assumes that all pointers are ok and you have only some occassional zero pointer in there. There are certainly other possibilities that this list is wrong.
In general you are avoiding segmentation fault when you are checking that the pointers are valid before you are using them. This is a simple zero pointer check.
-
Thaks for you quick answer. I tried your solution and indeed I have no more SIGSEGV, but SIGABRT. Do you know any better way to update the list to avoid unassigned pointers & co?
The best way to ensure that you are not entering in your problems is to ensure a list with correct pointers only.
In your code fragment the first line looks a bit fishy, because you exntend something already available.
collisionList += collidingItems(); //I update the list here
When the is completly empty, it is probably fine, but then you may use also this:
collisionList = collidingItems(); //I update the list here
When the list contains already objects and the list will be only updated with some additional elements, it might be perfectly fine, but that depends compeltely on your other logic.
For me it looks like a list which might be extended infinitely and possibly you are reacing the end of your memory.
-
The best way to ensure that you are not entering in your problems is to ensure a list with correct pointers only.
In your code fragment the first line looks a bit fishy, because you exntend something already available.
collisionList += collidingItems(); //I update the list here
When the is completly empty, it is probably fine, but then you may use also this:
collisionList = collidingItems(); //I update the list here
When the list contains already objects and the list will be only updated with some additional elements, it might be perfectly fine, but that depends compeltely on your other logic.
For me it looks like a list which might be extended infinitely and possibly you are reacing the end of your memory.
@koahnig thanks a lot. My problem is solved. I also clear the list at each iteration (since its size will never be greater than 1), that way I'm sure that all the elements are valid.
You are also right about the memory thing, I suspected that when I first ran my code with some qDebug but I couldn't spot what I was doing wrong.
Again thanks a lot.