Jump to content
  • Advertisement
Sign in to follow this  
Fukushuusha

Acess violation , segmentation fault [SOLVED]

This topic is 4335 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all. Thanks for reading this thread.I am literally hours above this little piece of code and I have no idea what the heck the problem is.Honestly I got past the searching patiently point and got a little bit pissed , so I am going to take a walk for a while. But before I do I would like to present it to you people here , since you might find the answer.I am making a 2D game using SDL.I am still setting the engine up ... but I am literally stuck at drawing a button in the menu.The rest of the program logic is bound to be ok , since it runs ok if I use my ScreenManager's class applySurface function to apply the button as a simple image.But when I use the draw function to draw it inside the menu I get a runtime error , just when the program starts.When I use the debugger it says : An access violation ( segmentation fault ) raised in your program Unfortunately I am not sure how to use the debugger ( DevC++) correctly so I can't find exactly where.From lots of trial and error I think it is somewhere in my menu class.It is still incomplete but I am going to paste it here and if you can see something I am missing please do say so.Thanks in advance :
#ifndef MENU_HPP
#define MENU_HPP

#include <vector>

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"

#include "ScreenManager.hpp"
#include "Button.hpp"

class Menu
{
    public:
         //a ScreenManager Object
        ScreenManager scManager;
        
        
        //a pointer to a test button 
        Button* b;
        
               
        /* default constructor */
        Menu() 
        {
          b = new Button(500 , 600 , 16 , 16 , scManager.loadImage("images/towerButton.png") );
        } ;
       
        /*draws the menu*/
        void draw(SDL_Surface* screen) ; 
        
        
        /*cleans up the buttons vector of the menu*/
        void cleanUp();
        
        private:
     
};

#endif



#include "menu.hpp"

/*draws the menu*/
void Menu::draw(SDL_Surface* screen)
{
    scManager.applySurface(0 , 568 , scManager.loadImage("images/menu.jpg") , screen);
    b->draw(screen);
}


/*cleans up the buttons vector of the menu*/
void Menu::cleanUp()
{
    delete b;
      
}


[Edited by - Fukushousha on January 31, 2007 4:47:37 PM]

Share this post


Link to post
Share on other sites
Advertisement
From looking at the code you posted, I don't see anything that really jumps out and says, "Hey...Here's your problem." What I would check to correct this problem is try to step through your code to see if you can find the line the access violation happens at, or if DevC++ has the ability to break on exceptions use that. If not, then I would probably put in some kind of diagnostic messages in certain places to see what code gets hit and what doesn't. This tends to help in cases where the debugger is lacking in features.

One other thing that I would check out first is to make sure the "SDL_Surface* screen" object you are passing in to your Draw function is valid. Or make sure your new button "b" is being properly constructed in the constructor for the Menu class. Hope this helps.

Share this post


Link to post
Share on other sites
I really don't get it.The problem is 100% with the Button* b ... but why ?

If I comment out the declaration of the pointer in the constructor .. comment out the b->draw(..) AND the delete b (from the cleanUp function ... then and only then the program runs smoothly.... why ? Why is that ? I can not understand ? -.-;

Maybe If I showed you the button class too?But it is a simple as it can get... and it inherits from the tile class ( which works ( tested it with my tilemap :p ) ...
I am at a loss -.- ;

Share this post


Link to post
Share on other sites
If you could post the code for your button class it would probably help. In the meantime, check to make sure that the towerButton.png file exists and that the scManager class can correctly load this file. Other than that, I would have to see your Button class since everything looks fine here.

Share this post


Link to post
Share on other sites
Yeah those you say are there , I made sure they were from the first moment.Even if they were not the program terminates with a return 1 ; , so I know something of this nature went wrong.
Anyway here is both the Button and the Tile class since I decided to make the button a child of the Tile class.The button class is still unfinished but .. well ... I wanted to compile it before I move on ... and here I am 6 hours later trying to see what the blazes is wrong with it.


#ifndef BUTTON_HPP
#define BUTTON_HPP

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"

#include "ScreenManager.hpp"
#include "Tile.hpp"

class Button : public Tile{
public:
/*The constructor*/
Button(int x , int y , int w , int h , SDL_Surface* img);
/*draws the button*/
void draw(SDL_Surface* screen);

private:

};

#endif

/* From here on , the .cpp part*/
#include "Button.hpp"

/*The constructor*/
Button::Button( int x , int y , int w, int h , SDL_Surface* img )
:Tile(x , y , w , h , img)
{

}

/*Draws the button at the given x and y on the screen*/
void Button::draw( SDL_Surface* screen)
{
Tile::draw(0 , 0 , screen);
}




#ifndef TILE_HPP
#define TILE_HPP



#include "ScreenManager.hpp"


class Tile
{
private:
//the tile's image
SDL_Surface* img;
//the tile's rectangle
SDL_Rect tRect;
//a Screen manager copy
ScreenManager scManager;

//the tiles dimensions
int x , y , w , h;

public :

//constructor
Tile(int x , int y ,int w , int h , SDL_Surface* img );
//draws the tile
void draw(int offsetX ,int offsetY , SDL_Surface* screen);
////////////////////////////////////////*MUTATOR METHODS*//////////////////////////////////////
//returns the image of the tiles
SDL_Surface* getImg();
//returns the screenManager
ScreenManager getScManager();
//returns the tile's rectangle
SDL_Rect& getTrect();
//mutator methods for x and y
void setX(int x);
int getX();
void setY(int y);
int getY();
/////////////////////////////////////*MUTATOR METHODS END*///////////////////////////////////////
};

#endif

/*The .cpp part from here on */

#include "Tile.hpp"


bool detectCollision( SDL_Rect &, SDL_Rect & );

/*checks for collision between 2 rectangles*/
bool detectCollision( SDL_Rect &A, SDL_Rect &B )
{
//The sides of the rectangles
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;

//Calculate the sides of rect A
leftA = A.x;
rightA = A.x + A.w;
topA = A.y;
bottomA = A.y + A.h;

//Calculate the sides of rect B
leftB = B.x;
rightB = B.x + B.w;
topB = B.y;
bottomB = B.y + B.h;

//If any of the sides from A are outside of B
if( bottomA <= topB )
{
return false;
}

if( topA >= bottomB )
{
return false;
}

if( rightA <= leftB )
{
return false;
}

if( leftA >= rightB )
{
return false;
}

//If none of the sides from A are outside B
return true;
}



Tile::Tile(int x , int y ,int w , int h , SDL_Surface* image )
{
tRect.x = x;
tRect.y = y;

this->x = x;
this->y = y;

img = image;


tRect.w = w;
tRect.h = h;
}

void Tile::draw(int offsetX ,int offsetY ,SDL_Surface* screen)
{
//draw the tile
scManager.applySurface(x-offsetX, y-offsetY , img , screen);

}
////////////////////////////////////////*MUTATOR METHODS*//////////////////////////////////////////

//gets the screen manager for the class
ScreenManager Tile::getScManager()
{
return scManager;
}

//gets the tile's rectangle
SDL_Rect& Tile::getTrect()
{
return tRect;
}

//set the x of the tile
void Tile::setX(int n)
{
x = n;
}

//gets the x of the tile
int Tile::getX()
{
return x;
}

//sets the y of the tile
void Tile::setY(int n)
{
y = n ;
}

//gets the y of the tile
int Tile::getY()
{
return y;
}

SDL_Surface* Tile::getImg()
{
return img;
}
///////////////////////////////////*MUTATOR METHODS END */////////////////////////////////////////



I know it is a little(a LOT :( ) messed up at the moment but I am still building it , so .. it is bound to be like that.Also as a note I intend to move the detectCollisions() function out of the Tile class later :p

So well ... BIG post , sorry , but if any good is to come of it and I manage to understand what is wrong with my code it will be worth it :) .Thanks in advance (again) for any help.

Share this post


Link to post
Share on other sites
Well, you were right, your Button class is pretty bare. I can't really see anything wrong code wise. And your image is there, so that's good. I would say try to only comment out the b->Draw but uncomment the construction/destruction of the Button class, but that probably won't get you very far.

So what you can do is try to change where the button is located on the screen. Try something really safe like 100, 100. If that doesn't work tr to ONLY draw the button. Don't draw the menu. Just comment out scManager.applySurface in Menu::draw. If this doesn't work then you can try to build your application completely from scratch. Remove all intermediate build files and object files. Sometimes compilers will use out of date object files and can cause a nightmare for finding a problem.

Just have a little patience. I've been down the hair pulling road quite a bit myself.

Share this post


Link to post
Share on other sites
He he yeah I had already done all those myself before , and it drew it very fine ... just drwing it as an image.

But you know what?The DevC++ was giving me some strange errors this morning .. maybe I will retry the whole thing.Anyway gotta go now , will post results when I come back and do it :)

Share this post


Link to post
Share on other sites
SOLVED AT LAST !

This idiot that posts this message here is here to post the solution to this problem that has been bugging(and I have been debugging it :p ) for more than a day.
By using the debugger from the backtrace I saw that this is the last function that is called : _libmsvcrt_a_iname() before the crash occurs.
I searched around the net and even though I did not understand exactly what this function does , I udnerstood its general purpose and then it came to me.I was actually callin the constructor of Menu before initialising SDL ... all I had to do was move the Menu menu; declaration 3 lines of code down and all ran smoothly.

I could ofcourse go on about how stupid I feel now , but suffice to say I started cackling like a maniac when I found out the solution.I wanna thank all the people who tried to help me , because from their help I did a lot of research on irrelevant things with the bug I had .. but VERY relevant for helping me become a better programmer.

And If anyone else sees this .. well ... never do anything in main before you initalize SDL. ^_^

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!