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

Author Topic: Please Help! Access Problems..  (Read 1240 times)

0 Members and 1 Guest are viewing this topic.

Roose Bolton of the Dreadfort

  • Full Member
  • ***
  • Posts: 113
  • Full-time Procrastinator, Part-time programmer.
    • View Profile
    • Personal Portfolio/Website/Blog
Please Help! Access Problems..
« on: November 11, 2012, 12:55:10 am »
Right Hey All!

I am using a nice state manager kindly made by Exploiter.

Something very wierd has been happening.

Whenever I 'New' an object, I cannot do anything with it.. if I try and access its member functions or even delete it, I get access violations...

Example >

Entity.hpp
#pragma once

#include <SFML/Graphics.hpp>
#include <Box2D/Box2D.h>

class Entity
{
public:
        Entity(void);
        ~Entity(void);

        sf::Sprite mSprite;
};
 

Entity.cpp

#include "Entity.h"


Entity::Entity(void)
{
}


Entity::~Entity(void)
{
}
 

Playstate.hpp
#ifndef PLAYSTATE_HPP
#define PLAYSTATE_HPP

#include <SFML/Graphics.hpp>
#include <Box2D/Box2D.h>

#include "GameState.hpp"
#include "PhysicsManager.h"
#include "Entity.h"
class GameEngine;

class PlayState : public GameState
{
public:
        PlayState( GameEngine& game, bool replace = true );
        ~PlayState();
        void pause();
        void resume();

        void update();
        void draw();

private:
        PhysicsManager mPhysicsManager;
        Entity *mEntity;
};

#endif // PLAYSTATE_HPP

 

Playstate.cpp
#include <memory>
#include <iostream>

#include "PlayState.hpp"
#include "MenuState.hpp"
#include "GameEngine.hpp"

#include "sGraphicsManager.h"

PlayState::PlayState( GameEngine& game, bool replace ) : GameState( game, replace)
{
        std::cout << "PlayState Init" << std::endl;
        mPhysicsManager.GetWorld()->SetGravity( b2Vec2(0, 9.4f));
        Entity* mEntity = new Entity();
        mEntity->mSprite.setPosition(100,100);

}      

PlayState::~PlayState()
{
        delete mEntity;
        //Dtor
}

void PlayState::pause()
{
        std::cout << "PlayState Pause" << std::endl;
}

void PlayState::resume()
{
        std::cout << "PlayState Resume" << std::endl;
}

void PlayState::update()
{
        sf::Event event;

        sGraphicsManager *Graphics = sGraphicsManager::Instance();

        while( Graphics->GetWindow().pollEvent( event ) )
        {
                switch( event.type )
                {
                        case sf::Event::Closed:
                                m_game.quit();
                                break;

                        case sf::Event::KeyPressed:
                                switch( event.key.code )
                                {
                                        case sf::Keyboard::Escape:
                                                m_game.quit();
                                                break;
                                        case sf::Keyboard::M:
                                                m_next = m_game.build<MenuState>( false );
                                                break;
                                }
                        break;
                }
        }
}

void PlayState::draw()
{
        sGraphicsManager *Graphics = sGraphicsManager::Instance();
        Graphics->GetWindow().clear( sf::Color(7, 10, 29));
        Graphics->GetWindow().display();
}
 

Now as you can see.. this should work fine? But when I setposition on mEntity - get get access violation? Even if I remove that line, when I close the app, it gets access violation on the mEntity destructor when I delete it..

What the hell??
Trying so very hard to finish at-least one project.

Watch out for the RAII police, they will get you.

www.bantersaurus-games.com

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Please Help! Access Problems..
« Reply #1 on: November 11, 2012, 01:15:23 am »
Quote from: In the constructor
Entity* mEntity = new Entity();

Which creates a entity instance local to the constructor and will memory leak when the constructor is finished.

Change it to mEntity = new Entity;.
Btw I advise you to avoid manual memory management and use std::unique_ptr or std::shared_ptr, as I did for the state manager.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything