Public Group

SDL, pointers, classes and crashes.

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

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)
{
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&lt;&lt;textura;
relleno=Cargar_Imagen(imagenacargar);
recorte.w=relleno-&gt;w; recorte.h=relleno-&gt;h;
recorte.x=0; recorte.y=0;
marco.w=relleno-&gt;w; marco.h=relleno-&gt;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 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 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 on other sites
Cargar_Imagen literally means Load_Image; but AFAIK, the interface to SDL isn't officially localized, so I assume this is the OP's own wrapper. :/

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.

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 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.

1. 1
Rutin
45
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 10
• 13
• Forum Statistics

• Total Topics
632984
• Total Posts
3009719
• Who's Online (See full list)

There are no registered users currently online

×