Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Bad Access Exception  (Read 4226 times)

0 Members and 1 Guest are viewing this topic.

GoldenMaster

  • Newbie
  • *
  • Posts: 2
    • View Profile
Bad Access Exception
« on: December 22, 2013, 06:12:00 pm »
Hi there,

This is my first C++ Programm and my first Project with SFML and i want to build a 2D "Chessfield" like game, where each Fields in my GameArea has its own Sprite.

Later on the GameArea should know which Fields are visible to the player and therefore draw only these that should be drawn. Currently my GameArea consists of a 10 x 10 Field array (wich is stored one dimensional because i couldnt get a dynamic 2 dimensional array running in c++) each Field has a draw function requireing the RenderWindow object and a position Vector.

The GameArea has a Draw function on its own which should decide wich fields to call and wich position to give these Sprite.

Because its my first Project i worked with the XCode Template and use the same Ressources (the img) and only tried to apply my Strukture to is.

Now i get a EXC_BAD_ACCESS (code=EXC_I386_GPFLT) Message in my Fields draw function.

Im Working on my Mac Book Pro with Maverick and the CLANG package of SFML.

To give better understanding of my code i upload the whole Solution named GoS (Game of Stones)

i have no clue what I'm doing wrong and for these who aren't interested in downloading a XCode solution i will paste my 2 classes and their .h files in Code sections below.


my main.cpp
(click to show/hide)
[/font]

field.h
(click to show/hide)
[/font]

field.cpp
(click to show/hide)
[/font]

GameArea.h
(click to show/hide)
[/font]

GameArea.cpp
(click to show/hide)


p.s.: sry i dont know how to get spoiler tags to work so bear with the whole load of sourcecode

edit: i had to remove the img used as texture becouse of the size limitation

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Bad Access Exception
« Reply #1 on: December 22, 2013, 06:31:55 pm »
_fields = (Field *)malloc(x * y * sizeof (Field *));
You allocate space that is enough for x*y pointers to a Field, not x*y Fields. You should do sizeof(Field)..

I think... the code is pretty short but quite convoluted.. and so we come to second issue:
This code is terrible(no offence), basically everything you do is deprecated, wrong, undefined behaviour, bad practice, unsafe or something else bad. If you continue like that you won't get far.
Back to C++ gamedev with SFML in May 2023

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Bad Access Exception
« Reply #2 on: December 22, 2013, 07:25:30 pm »
You should really get a modern C++ book, this code is very error prone.

Don't ever use malloc and free in C++. Don't use new and delete unless you encapsulate the manual memory management. Use constructors to create objects, not raw memory allocation. Consider the rule of three, and avoid empty destructors.

Furthermore, the signature of main is one of the following, but yours is wrong:
int main()
int main(int, char**)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

GoldenMaster

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Bad Access Exception
« Reply #3 on: December 22, 2013, 08:10:07 pm »
Thanks for the fast reply.

@FRex: I edited the line as suggested but the error is still the same, i don't know if thats the error. Additionaly i would really like to know what you mean. I know my knowledge of C++ are limited but i would like to know what i'm doing wrong.

@Nexus: the main is created from the SFML template and XCode, i did just edit code within, also, what else should i use if not malloc?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Bad Access Exception
« Reply #4 on: December 22, 2013, 09:55:09 pm »
You should use the features that C++ provides to abstract from low-level techniques like memory management. Otherwise you are just using C with classes and not idiomatic C++.

Instead of malloc, you can use STL containers and smart pointers. These things cannot be fully explained within a few lines, you should really get a modern and good book (be careful, a lot of literature is of questionable quality) if you want to learn C++ seriously.

About the SFML template on XCode: Who wrote that? int main(int, const char**) is not portable C++ code.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Bad Access Exception
« Reply #5 on: December 22, 2013, 10:28:14 pm »
You pass pointers where references would do.
You don't null _fields or initialize _x and _y to zero so if image doesn't load then class will call free on random memory address in that pointer and 'draw' random amount of 'fields'(that don't exist) from a random memory address in draw.
GameArea also violates rule of three.
Storing sf::Texture pointer alongside sf::Sprite with same texture is not needed, sf::Sprite stores that pointer on its own and you can inspect it.
Using free and malloc is most of the time a big no-no for c++, also you don't check malloc for errors and you use malloc with wrong sizeof. Using manual memory management outside of containers or super specific code with weird constraints that can't use RAII is a no-no.
Your main has wrong 2nd argument.
Back to C++ gamedev with SFML in May 2023