By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,116 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,116 IT Pros & Developers. It's quick & easy.

Second class instance fails

P: n/a
If I make two instances of the class, the second one will fail:

map_tiles=new G_Tile("terrain.wst",32,32,3,0);
back_tiles=new G_Tile("backtiles.wst",32,32,2,1);

The program doesn't crash and the code seems to work, but the tiles
are not showing for the second instance. If I switch places then
back_tiles can be seen, but not map_tiles. Then I discovered that
if I put some code between those calls, then both will work. In
this case:

map_tiles=new G_Tile("terrain.wst",32,32,3,0);
ustr=new Font(8,16,"bfonts.bmp");
back_tiles=new G_Tile("backtiles.wst",32,32,2,1);

What could be the reason for that?

Jul 30 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Krice wrote:
If I make two instances of the class, the second one will fail:

map_tiles=new G_Tile("terrain.wst",32,32,3,0);
back_tiles=new G_Tile("backtiles.wst",32,32,2,1);

The program doesn't crash and the code seems to work, but the tiles
are not showing for the second instance. If I switch places then
back_tiles can be seen, but not map_tiles. Then I discovered that
if I put some code between those calls, then both will work. In
this case:

map_tiles=new G_Tile("terrain.wst",32,32,3,0);
ustr=new Font(8,16,"bfonts.bmp");
back_tiles=new G_Tile("backtiles.wst",32,32,2,1);

What could be the reason for that?


You have an error on line 42 of your code.
Jul 30 '05 #2

P: n/a
red floyd wrote:
You have an error on line 42 of your code.


You must be proud of yourself now.. My guess is that something
inside the constructor corrupts memory, since those objects are
most likely generated one after another in the memory space. But
some of the source code was taken from previous project and that
didn't had any errors. It's these silent memory errors I hate
the most, because they don't even crash the program.

Jul 31 '05 #3

P: n/a
Krice wrote:
My guess is that something inside the constructor corrupts memory


First I took SDL_Surface out from the class, but that didn't help.
Only when I removed all SDL variables from the class (as member
variables) the problem was gone. But why SDL doesn't work in a class?

Jul 31 '05 #4

P: n/a
Krice wrote:
But why SDL doesn't work in a class?


Speaking to myself, but the reason was a local variable with same
name that silently shadowed the class member variable, causing
such unpredictable behavior.

Aug 3 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.