Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » [C++/SDL2] Help me find these segfaults
- SC_DStwo_Master
- Scratcher
100+ posts
[C++/SDL2] Help me find these segfaults
Here's the code:
https://github.com/Nyapp/Cell/blob/master/src/classes/text.h (Text::~Text)
https://github.com/Nyapp/Cell/blob/master/src/classes/aggressive.h (roughly at line 90)
https://github.com/Nyapp/Cell/tree/master/src (so that you can see how I use these classes)
One of the segfaults occurs when you close the game, and I think it's because of my fps counter (which is inherited from the Text class)
The other occurs when a bullet collides with the level
https://github.com/Nyapp/Cell/blob/master/src/classes/text.h (Text::~Text)
https://github.com/Nyapp/Cell/blob/master/src/classes/aggressive.h (roughly at line 90)
https://github.com/Nyapp/Cell/tree/master/src (so that you can see how I use these classes)
One of the segfaults occurs when you close the game, and I think it's because of my fps counter (which is inherited from the Text class)
The other occurs when a bullet collides with the level
- SC_DStwo_Master
- Scratcher
100+ posts
[C++/SDL2] Help me find these segfaults
I used gdb to figure out in what function exactly the segfault occurred, but I'm away from home for the next two days so I can't copy the output here. I don't want to step through the code because obviously it's a program that repeats 1000s of times a second Have you tried using tools like gdb or Valgrind? :-)
- TheMonsterOfTheDeep
- Scratcher
1000+ posts
[C++/SDL2] Help me find these segfaults
For “aggressive.h”, you might be deleting the bullet in the second line. In that case, the index is going to point to a different bullet, and if that index happens to be out of the array, you will get a segfault.
You really shouldn't render the bullet if it has been destroyed.
You really shouldn't render the bullet if it has been destroyed.
my latest extension: 2d vector math
- MegaApuTurkUltra
- Scratcher
1000+ posts
[C++/SDL2] Help me find these segfaults
If you're using C++, why aren't using STL?
My advice is try to avoid manual memory management as much as possible (smart pointers are a thing!)
My advice is try to avoid manual memory management as much as possible (smart pointers are a thing!)
$(".box-head")[0].textContent = "committing AT crimes since $whenever"
- SC_DStwo_Master
- Scratcher
100+ posts
[C++/SDL2] Help me find these segfaults
I actually fixed those two but forgot to upload the code. I added –i at the end of the bullet destruction line, and else before the next line. I think I still got a segfault For “aggressive.h”, you might be deleting the bullet in the second line. In that case, the index is going to point to a different bullet, and if that index happens to be out of the array, you will get a segfault.
You really shouldn't render the bullet if it has been destroyed.
- Jonathan50
- Scratcher
1000+ posts
[C++/SDL2] Help me find these segfaults
+1 My advice is try to avoid manual memory management as much as possible (smart pointers are a thing!)
Not yet a Knight of the Mu Calculus.
- Ethan_1
- Scratcher
100+ posts
[C++/SDL2] Help me find these segfaults
Also, there are c++ bindings of SDL2 which you may find helpful.
https://github.com/libSDL2pp/libSDL2pp
https://github.com/libSDL2pp/libSDL2pp
- SC_DStwo_Master
- Scratcher
100+ posts
[C++/SDL2] Help me find these segfaults
Nah I'd rather not relearn all of SDL2 using that binding, the C version serves me well Also, there are c++ bindings of SDL2 which you may find helpful.
https://github.com/libSDL2pp/libSDL2pp
- gtoal
- Scratcher
1000+ posts
[C++/SDL2] Help me find these segfaults
Nah I'd rather not relearn all of SDL2 using that binding, the C version serves me well
So why write in C++?
- SC_DStwo_Master
- Scratcher
100+ posts
[C++/SDL2] Help me find these segfaults
because OOPNah I'd rather not relearn all of SDL2 using that binding, the C version serves me well
So why write in C++?
C is more suited for low-level development imo
- Discussion Forums
- » Advanced Topics
- » [C++/SDL2] Help me find these segfaults