Object Instantiation
Feb 20, 2009 at 9:27am UTC
I am attempting to instantiate multiple versions of the same object, my code for that simply looks like:
1 2 3 4 5 6 7 8 9 10 11
/* Global variables: */
SDL_Color _fg;
SDL_Rect _bounds;
Font* _font; //Font object.
void Game::initMenu(){
Button* b1,* b2;
b1 = new Button(_font, "ButtonOne" , _fg, _bounds, 0);
b2 = new Button(_font, "ButtonTwo" , _fg, _bounds, 0);
}
_font, _fg and _bounds are all initialized in another function, and Button is an object:
1 2 3
Button::Button(Font* f, char * txt, SDL_Color fg, SDL_Rect bounds, int justify)
: DrawableString(f, txt, fg, bounds, justify){
}
My issue is that the second time I create a new button the program seg faults. Creating one button is not a problem but creating the second is, and so far I've been unable to figure out why that is. As you can see the Button constructor doesn't actually do anything (I removed what it did trying to figure this out), and neither does Button's destructor.
I'd post more code but being that I'm not actually using any of it to create the problem it seems irrelevant - but if you need to see more of something let me know.
Thanks.
Last edited on Feb 20, 2009 at 9:27am UTC
Feb 20, 2009 at 1:20pm UTC
But Button is derived from DrawableString, so what does DrawableString's constructor and destructor do?
Feb 20, 2009 at 6:14pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
DrawableString::DrawableString(Font* f, char * txt, SDL_Color fg,
SDL_Rect bounds, int justify)
: BoundedObject(bounds){
_font = f;
_fg = fg;
_justify = justify
_txt = 0;
_txtImg = 0;
_line_number = 0;
setTxt(txt);
}
DrawableString::~DrawableString(){
if (_txt)
delete [] _txt;
if (_txtImg)
SDL_FreeSurface(_txtImg);
}
void DrawableString::setTxt(char * txt){
if (_txt)
delete [] _txt;
_txt = new char [strlen(txt)+1];
if (! _txt){
std::cerr<<"Could not allocate array for " <<txt<<"\n" ;
exit(1);
}
strcpy(_txt, txt);
render();
}
void DrawableString::render(){
int newx, newy;
if (_txtImg)
SDL_FreeSurface(_txtImg);
_txtImg = _font->renderTxt(_txt, _fg);
_src.w = _dest.w = _txtImg->w;
_src.h = _dest.h = _txtImg->h;
if (_justify == NO_JUSTIFY)
return ;
newy = _line_number* _font->getPtSize();
if (_justify == CENTER)
newx = (_bounds.w - _dest.w)/2;
if (_justify == LEFT)
newx = 0;
if (_justify == RIGHT)
newx = (_bounds.w - _dest.w);
setUpperLeftXY(newx, newy);
}
Well I seem to have fixed it, though I'm still slightly confused. What I was compiling prior to your comment in the DrawableString constructor was
1 2 3 4 5 6 7 8 9
DrawableString::DrawableString(Font* f, char * txt, SDL_Color fg,
SDL_Rect bounds, int justify)
: BoundedObject(bounds){
_font = f;
_fg = fg;
_txt = 0;
setTxt(txt);
}
which wasn't initializing _txtImg, _justify or _line_number. I see how that can result in error, but I'm not real sure why it would be acceptable for instantiating just one Button but not a second.
Thanks for the response.
Topic archived. No new replies allowed.