Sign in to follow this  

SDL, pointers, classes and crashes.

This topic is 3662 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

Hi!. I am relatively new to working with classes on C++ and I am having a problem with a "Segmentation violation" failure that has me on my knees. The thing is just like this: I have a class that is a "part". This part should be inside a list on a class that is a "whole". The "whole" is created from the main function and in the "Whole" constructor I create the "part". The thing looks like this:
int main(int argc, char * argv[])
{
	Init_SDL();
	Nivel fase(10);
        do
	{
		//Blahblah...
	}while (not Quitting());
	Shutr_SDL();
    return 0;
}

class Nivel
{
	private:
	int lado;				//No use at all.
	BloqueNivel * bloque1;

	public:
	Nivel(int dimension)			
	{
		lado=dimension;
		bloque1=NULL;
		bloque1 = new BloqueNivel(0,0,30,30, "Muro2.bmp");
	}
};

class BloqueNivel
{
	private: 
	SDL_Surface * relleno;
	SDL_Rect marco;
	SDL_Rect recorte;
	SDL_Rect caja;
	char * imagenacargar;
	
	public:
	BloqueNivel(int x, int y, int ancho, int alto, char * textura)
	{
		caja.x=x; caja.y=y; caja.w=ancho; caja.h=alto;
		imagenacargar=textura;
		cout<<textura;
		relleno=Cargar_Imagen(imagenacargar);
		recorte.w=relleno->w; recorte.h=relleno->h; 
		recorte.x=0; recorte.y=0;
		marco.w=relleno->w; marco.h=relleno->h; 
		marco.x=caja.x; marco.y=caja.y;
	}
};
Everything works until I try to acess the "relleno" pointer in the BloqueNivel class, that is created as a pointer itself... Every try of accessing it results in a crash. As an SDL_Surface it should be declared as a pointer and I am trying to access it as a pointer, am I not?. Trying to directly create a pointer to the BloqueNivel in the main function results in a crash BUT creating it as an object like in "BloqueNivel p(blahblah) does work... So, I would bet the thing crashes on the way I handle things when "pointerizing" them... As far as I know the "new" word should allocate memory for my class but, how should I acess its members that are pointer themselves?. Thanks a lot!. [Edited by - The_Marlboro_Man on December 5, 2007 2:49:38 PM]

Share this post


Link to post
Share on other sites
A few notes:

- In the Nivel constructor, the 'bloque1=NULL;' line is useless, because you're making bloque1 point to a new object in the next line anyway.
- You never delete the BloqueNivel object you new-ed in the constructor, which means your program is leaking memory. Deleting that object in the Nivel destructor would be a good idea. Basically, anything you new must be delete-ed somewhere.
- In the BloqueNivel constructor, you're using a char*. That's a C-style string, quite unsafe and uneasy to use. Use a std::string instead. If you need a char* (for example, when calling library functions that require it), you can use the c_str() function.
- I see you're using a variabele named bloque1. Are you planning to use bloque2...9 too? An array would be better if you're always going to use a set amount of bloques, but a container class such as std::vector or std::deque would be best: you can simply add objects to them when you need them, and remove them when you don't need them anymore.


As for the crash, what does the Cargar_Imagen() function do? What does it do when it can't find the specified image? Also, did you try running through your code with a debugger, to see where things went wrong?

Share this post


Link to post
Share on other sites
The most likely candidate with the information you gave is that the image loader did not work correctly. Probably because it did not locate the file. You should consider checking the return value of Cargar_Imagen (see the documentation for what values it returns).

Share this post


Link to post
Share on other sites
Zahlman, well, "Cargar_Imagen" is a function I did to do some work with SDL's "Load_BMP" to convert the surface's format... I don't really understand the need to but all tutorials I read about it state that it saves some work to SDL if the formats are different.

Regarding that, nobody knews, the function itself just returned the surface with the bitmap loaded and it did it correctly as far as I know... I mean, I already had a problem with that and tried to throw some warning errors if the image does not exist.

And that leaves only CaptainP's post to be answered. First off, thanks a lot for your notes. The code I posted is incomplete as I just made it smaller and smaller to find where the problem is and post it here: that's the reason why there's no destructor in the code but well, I have it covered :). The whole bloque1 thing is just a temporary name, actually what I have there is the "header" of a linked list but no linked lists functions are in the code I put here... That leads us to the std:strings and std:vectors... In all honesty, I never got to learn about them in my C++ studies and though I read about them (people here at GameDev pointed me to these implementations) a lot I would like to do things the "hard" way for my first serious try to do something... Is something about "Learning the ropes the hard way and then using tools that make my life easier". I know it doesn't make a lot of sense but since I am not coding for a living and I just enjoy learning this I like it that way :).

Finally about the crash itself, I rewrote the code and solved it (!!!)... Though I have looked around for something that could have changed or typos I could have made (I like to do that) I did find nothing... I should really get a debugger... I am using the g++ compiler under Ubuntu. What's an appropriate debugger for that?.

Thanks a lot.

Share this post


Link to post
Share on other sites
The quintessential debugger for linux is gdb, which can be found in ubuntu's package repository if it isn't already installed on your system. You'll want to add the '-g' option to your compile flags to have g++ add the debugging information to your executable. gdb is a commandline tool with a bit of a learning curve. DDD is a graphical interface for gdb that you might find helpful. This can also be found in the ubuntu package repositories.

Finally, along with gdb, I find valgrind to be a hugely helpful tool when developing on linux. Valgrind is a memory-leak detection tool that can aid you in pinpointing memory-related bugs.

Share this post


Link to post
Share on other sites

This topic is 3662 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this