Discuss Scratch

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
blob8108
Scratcher
1000+ posts

[C++/SDL2] Help me find these segfaults

Have you tried using tools like gdb or Valgrind? :-)

tosh · slowly becoming a grown-up adult and very confused about it
SC_DStwo_Master
Scratcher
100+ posts

[C++/SDL2] Help me find these segfaults

blob8108 wrote:

Have you tried using tools like gdb or Valgrind? :-)
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
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.

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!)

$(".box-head")[0].textContent = "committing AT crimes since $whenever"
SC_DStwo_Master
Scratcher
100+ posts

[C++/SDL2] Help me find these segfaults

TheMonsterOfTheDeep wrote:

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.
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
Jonathan50
Scratcher
1000+ posts

[C++/SDL2] Help me find these segfaults

MegaApuTurkUltra wrote:

My advice is try to avoid manual memory management as much as possible (smart pointers are a thing!)
+1

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
SC_DStwo_Master
Scratcher
100+ posts

[C++/SDL2] Help me find these segfaults

Ethan_1 wrote:

Also, there are c++ bindings of SDL2 which you may find helpful.
https://github.com/libSDL2pp/libSDL2pp
Nah I'd rather not relearn all of SDL2 using that binding, the C version serves me well
gtoal
Scratcher
1000+ posts

[C++/SDL2] Help me find these segfaults

SC_DStwo_Master wrote:

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

gtoal wrote:

SC_DStwo_Master wrote:

Nah I'd rather not relearn all of SDL2 using that binding, the C version serves me well

So why write in C++?
because OOP
C is more suited for low-level development imo

Powered by DjangoBB