Pointer changes for no reason.

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Musi
Posts: 106
Joined: November 25th, 2012, 1:06 am

Pointer changes for no reason.

Post by Musi » December 30th, 2012, 8:20 pm

I've written a function that creates a sprite and returns its address. If i create a pointer and use the function to make it point to this newly created sprite it seems to work. But for some reason during the windows message loop, when peekMessage(); is called, the pointer changes to some random value. I have no idea why it changes, its so strange.

Does anyone have a clue why it would change? Iv'e been banging my head against the desk for hours.

I could upload the solution if anyone wants, i just don't want anyone to laugh at my code. :D
Musi

There are 10 types of people that understand binary.
Those that do, and those that don't.

Paradox
Posts: 22
Joined: July 14th, 2012, 4:11 pm

Re: Pointer changes for no reason.

Post by Paradox » December 31st, 2012, 1:33 am

Hey Musi!
Could there be a name collision? The peekMessage function returns "A pointer to an MSG structure that receives message information".
Other than that, I think it would be helpful for you to post the solution! I'll be happy to look for you!
-Paradox

Musi
Posts: 106
Joined: November 25th, 2012, 1:06 am

Re: Pointer changes for no reason.

Post by Musi » December 31st, 2012, 3:25 am

Yessss! I found what was wrong. I was in the middle of tidying up the code and commenting it for you when i noticed it.

I made the function so it just created a normal Sprite object on the stack and returned its address, which is completely wrong because once it leaves the function's scope the computer can do what the hell it likes with that address. What i should have done was create a Sprite pointer and allocate a new one on the heap and then return the pointer, which I've now done, and it works!

Thank you for offering to help Paradox, and for inadvertently helping to solve the problem.
Musi

There are 10 types of people that understand binary.
Those that do, and those that don't.

Post Reply