gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Corrupt data [Solved]

#173814 - Azenris - Sun May 02, 2010 4:57 pm

I have the following code before a function call

Code:
if (GetLevel()->m_tiles_per_level != 576)
{
   DEBUG_MSG(0, "before")
}


And i have at the top of the function being called

Code:
if (GetLevel()->m_tiles_per_level != 576)
{
   DEBUG_MSG(0, "after")
}


I'm getting the message "after". how is the act of calling a function corrupting my data.

Not sure how to fix it as I'm not really sure what the problem is.

TY for any help!
_________________
My Homebrew Games


Last edited by Azenris on Sun May 02, 2010 9:46 pm; edited 1 time in total

#173815 - DiscoStew - Sun May 02, 2010 5:49 pm

Code:
GetLevel()->m_tiles_per_level


Can you tell us what GetLevel is, because from the look of it, you are trying to get a member of a function. Last I remember, function's can't have members, yet how it can be compiled as such is beyond me.

Now, if GetLevel is a pointer to an object of a class and m_tiles_per_level is a method (function), then it would need to look something like this...

Code:
GetLevel->m_tiles_per_level()

_________________
DS - It's all about DiscoStew

#173817 - Cearn - Sun May 02, 2010 6:51 pm

DiscoStew wrote:
Code:
GetLevel()->m_tiles_per_level


Can you tell us what GetLevel is, because from the look of it, you are trying to get a member of a function. Last I remember, function's can't have members, yet how it can be compiled as such is beyond me.

Functions can't have members, but the structs they return might.

Code:

struct Foo { int m_bar; }    // Struct definition

static Foo s_foo;         // Global variable (static for encapsulation)

Foo *GetFoo()         // Get foo instance
{
    return &s_foo;
}

// Somewhere else
GetFoo()->m_bar= 3;         // Set member via wrapper function.


As for the actual problem, I suppose it's possible that DEBUG_MSG does more than you think it does. Or less than you think it does. Maybe it needs a flush to actually print. I've also noticed that if you call stdio functions before initializing the console, later initializations don't really take and printf is basically in sulk-mode, only obeying when it feels like it. If the "before" doesn't print when you change its condition to == 576, the problem is probably in DEBUG_MSG.

#173818 - Azenris - Sun May 02, 2010 7:27 pm

yea GetLevel returns a class with a public variable being accessed

Code:
inline CLevelManager *GetLevel(void) const {return m_pLevel;}


It does print when changed to ==

I also changed it slightly to print exactly what the variable is becoming, it changes from 576 to -535752702

The function is called 4 times and on the fifth attempt it fails as the variable becomes that garbage

The DEBUG_OUT calls a function that sets up the console and prints it, but they are only called after the variables fails at the checks. one before the call and after, the after being the only that fails as something changes it

hmmm! :(

Ill keep trying

EDIT: I just tried to print some stats on the actual entity its acting on, all my entities have dataID's and this one is returning on that doesnt exist so the whole entity is messed up. it must only show once calling that function. Guess I gotta look further back

EDIT: The problem was with my enemy spawner, it spawned the enemy "attacking" so it hit its target which was random memory. It now spawns correctly and the pTarget variable default set NULL.

oopsie! Ty for any help
_________________
My Homebrew Games