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

Author Topic: Debug EXC_BAD_ACCESS?  (Read 1224 times)

0 Members and 1 Guest are viewing this topic.

Knockback

  • Newbie
  • *
  • Posts: 5
  • The Guild - CEO
    • AOL Instant Messenger - skylearj
    • View Profile
Debug EXC_BAD_ACCESS?
« on: April 28, 2013, 08:47:52 pm »
This might be an Xcode 4-only issue, though I figured one or two people here have come across it.

After following RevTorA's tutorial for a C++ Tile Engine, I came across this nasty bug when trying to set a position to an element. It seems that for us Mac devs, EXC_BAD-ACCESS points out a memory issue. The only problem is that Xcode isn't giving me a way to fix it, as would usually be the case.

The app would build and then run almost perfectly, except that this issue messes up rendering pretty badly. I was wondering if anyone here could help me out by pointing out how to fix it.

Tile.h
#ifndef Tile_h
#define Tile_h

#include <iostream>
#include <SFML/Graphics.hpp>

class Tile
{
    private:
        sf::Sprite baseSprite; // The trouble child.
       
   
    public:
        Tile(sf::Texture& texture);
        ~Tile();
   
        void Draw(int x, int y, sf::RenderWindow* renderWindow);
       
};

#endif
 

Tile.cpp
#include "Tile.h"

Tile::Tile(sf::Texture& texture)
{
    baseSprite.setTexture(texture);
}

Tile::~Tile()
{
   
}

void Tile::Draw(int x, int y, sf::RenderWindow* renderWindow)
{
    baseSprite.setPosition(x, y); // This is where the EXC_BAD_ACCESS occurs.
    renderWindow->draw(baseSprite);
}
 

Those were the Tile files, which are the origin of the issue. These are the two compadres that reference Tile at some point.

Engine.h
#ifndef Engine_h
#define Engine_h

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

#include "TextureManager.h"
#include "Tile.h"

class Engine
{
    private:
        sf::RenderWindow* window;
   
        bool Initialize();
        void MainLoop();
        void ProcessInput();
        void RenderFrame();
        void Update();
       
        TextureManager* textureManager;
        void LoadImages();
   
        Tile* testTile;
   
   
    public:
        Engine();
        ~Engine();
   
        void Go();
   
        sf::Texture BackgroundTexture;
        sf::Sprite Background;
};

#endif
 

Engine.cpp
#include <iostream>

#include "Engine.h"

Engine::Engine()
{
   
}

Engine::~Engine()
{
    delete window;
    delete textureManager;
}


bool Engine::Initialize()
{
    textureManager = new TextureManager();
    window = new sf::RenderWindow(sf::VideoMode(800, 612, 32), "BattleGame - V2");
   
    if(!window)
        return false;
    else if(!BackgroundTexture.loadFromFile("BattleGame.app/Contents/Resources/BG.png"))
            return false;
         else
            Background.setTexture(BackgroundTexture);
            return true;
}


void Engine::ProcessInput()
{
    sf::Event Event;
   
    while(window->pollEvent(Event))
    {
        switch(Event.type)
        {
            case sf::Event::Closed:
                window->close();
                break;
        }
    }
}


void Engine::MainLoop()
{
    while(window->isOpen())
    {
        ProcessInput();
        Update();
        RenderFrame();
    }
}


void Engine::Update()
{
   
}


void Engine::LoadImages()
{
    sf::Texture sprite;
    sprite.loadFromFile("BattleGame.app/Contents/Resources/ConsoleGUI.png");
   
    textureManager->AddTexture(sprite);
   
    testTile = new Tile(textureManager->GetTexture(0));
}


void Engine::RenderFrame()
{
    window->clear();
    window->draw(Background);
    testTile->Draw(100, 0, window);
    window->display();
}


void Engine::Go()
{
    if(!Initialize())
        throw "The engine failed to initialize.";
    else
        MainLoop();
}
 

Anyone?