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

Author Topic: Xcode error: EXC_BAD_ACCESS (code=1, adress 0x0)  (Read 5464 times)

0 Members and 1 Guest are viewing this topic.

Kladdy

  • Newbie
  • *
  • Posts: 12
    • View Profile
Xcode error: EXC_BAD_ACCESS (code=1, adress 0x0)
« on: January 05, 2015, 06:49:30 am »
Hello guys! I'm just going to start off with some information. I am running Mac OS X 10.10, Xcode version 6.1.1, SFML 2.2. I am creating an app that lets you change some settings otherwise hard to change in a game, and I'm having a issue. I'm just going to say that I'm NOT a professional programmer, it's just a hobby that i've been doing for a while. I am not super good at it, so thats why I'm asking you guys!

I have a main.cpp file, which looks like this.
#include "main.h"
#include "langSelect.h"
#include "mainMenu.h"

using namespace std;

    int main(int argc, char** argv){
       
        mainMenu();
       
        return 0;
   
    }

It just calls the mainMenu method inside the mainMenu.h, which is just where i declare the method. In mainMenu.cpp, I'm displaying a menu with buttons and text. I declare these two variables in the beginning:
vector<sf::Text> text;
int textsNumber;

Then in the mainMenu method, i do
textsNumber = 0;

After that, i create text like this:
text.push_back(sf::Text("blahblahblah",font,30));

This can now be accessed like an array, like this:
text[0]

Now when it comes to clearing the window and drawing the text, i have a loop where it goes thought a set number (2 in this case, i only have 2 lines of text right now) of numbers in a loop. Remember that textsNumber is 0 as i set in the beginning.
while( textsNumber < 2){
                window.draw(text[textsNumber]);
                textsNumber++;
                if(textsNumber == 2){
                    textsNumber = 0;
                    break;
                }
            }

If i run this, the program launches and opens a window. But instantly, Xcode comes into focus and highlights
window.draw(text[textsNumber]);
in green and displays a text beside, that says Thread 1: EXC_BAD_ACCESS (code=1, adress 0x0) http://gyazo.com/c3ca45ae039ce6e17e9d08be008ca49a

It also does that on main.cpp: http://gyazo.com/317e1f147c9da652413473ce46fdcfb0

If i click the other tabs on the left side i see this:
http://gyazo.com/475feefd270519138c3a98a9dd424532
http://gyazo.com/73e99dcf657042ab477d1ce8c8357940
http://gyazo.com/d6edc312382865bdb11490b2d303ac9a

What could cause this problem? I have been searching all over google, only to find irrelevant information or very, VERY advanced guides on how to fix. If you want to see the whole source code, just tell me. Thanks.
« Last Edit: January 05, 2015, 06:54:20 am by Kladdy »

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Xcode error: EXC_BAD_ACCESS (code=1, adress 0x0)
« Reply #1 on: January 05, 2015, 10:14:07 am »
Your font is probably dead. Make sure the variable `font` is living long enough to be used in your drawings. It's a bit like the white texture problem.

BTW, you while loop is rather strange: the condition is never met because of the break. Instead you could write something like that which is cleaner:

for (int textsNumber = 0; textsNumber < 2; ++textsNumber){
    window.draw(text[textsNumber]);
}

Event better, use `textsNumber < text.size()`.

Debugger is a great tool. Have a look there if you want to learn how to use it.

Tips: avoid using break statement (except in switches).
SFML / OS X developer

Kladdy

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Xcode error: EXC_BAD_ACCESS (code=1, adress 0x0)
« Reply #2 on: January 05, 2015, 10:21:12 am »
I'm making the font with this:
sf::Font font;
        if (!font.loadFromFile(resourcePath() + "segoeuil.ttf")) {
            return EXIT_FAILURE;
        }

and then use it
text.push_back(sf::Text("blahblahblah",font,30));

After that i don't touch the variable at all, until I'm calling to draw the text. How can i make it stay alive longer?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Xcode error: EXC_BAD_ACCESS (code=1, adress 0x0)
« Reply #3 on: January 05, 2015, 10:24:23 am »
Learn how scopes work.

It's really important to know the basic of C++ before using SFML to avoid such issue. Have a look at a good book;)
SFML / OS X developer

Kladdy

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Xcode error: EXC_BAD_ACCESS (code=1, adress 0x0)
« Reply #4 on: January 05, 2015, 10:37:21 am »
I do know about scopes. This has worked before, and suddenly, it doesn't. I am declaring the variable before i use it and I'm declaring it in the same function.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Xcode error: EXC_BAD_ACCESS (code=1, adress 0x0)
« Reply #5 on: January 05, 2015, 10:48:43 am »
Alright. But that I couldn't have guessed with no code at hands. ;)

If you want more accurate help, please provide a minimal and complete code that reproduce the issue.
SFML / OS X developer

Kladdy

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Xcode error: EXC_BAD_ACCESS (code=1, adress 0x0)
« Reply #6 on: January 05, 2015, 01:15:00 pm »
Here, it's a ZIP file of a xcode project i made fast, the error happens when you run it.
https://www.dropbox.com/s/3gf0gctecrmptk7/CustomLoL%20kopia.zip?dl=0

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Xcode error: EXC_BAD_ACCESS (code=1, adress 0x0)
« Reply #7 on: January 05, 2015, 01:37:20 pm »
If I have to download something it's most probably not a minimal code and unfortunately I don't have that much time so I won't download it (like most users around here in fact). Please read http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368
SFML / OS X developer

Kladdy

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Xcode error: EXC_BAD_ACCESS (code=1, adress 0x0)
« Reply #8 on: January 05, 2015, 01:58:55 pm »
Oh well, I'll just use the normal way of displaying text. Thanks for the help

 

anything