Sign in to follow this  
Programmer16

[C++] Pointer problem

Recommended Posts

Ok, I'm not the best with pointers, so I'm having a problem figuring this out. I have a class that stores a const pointer to a D3DXVECTOR2. Then I have a class that has a position variable:
class Object
{
public:
    const D3DXVECTOR2* m_pBoundVector;
    D3DXVECTOR2 m_Position;
};

// I assign Follower like so:
g_Follower.m_pBoundVector = &g_Leader.m_Position;
This works fine and it follows perfectly, etc. except after I enter PeekMessage(). The vector appears fine, and stays correct upto the point that my app calls PeekMessage(), at which point it seems to point to random crap. All of my other pointers that I'm using are staying correct. Edit: I'm not doing anything special in my windows code, clear the WNDCLASS structure, fill it, create my window, setup Direct3D, setup DirectInput, start my message loop.)

Share this post


Link to post
Share on other sites
Maybe when you are accessing this m_pBoundVector member and it gives crap, the g_Leader.m_Position variable is already gone (if it's on stack, or you called delete on it)? Data to which pointer points must be in memory if you want to access it.

Share this post


Link to post
Share on other sites
Thanks for the reply, but nope, both variables are global, and I'm "watching" them using my compiler. The vector that is being pointed to still exists (and still has the correct values.)

Share this post


Link to post
Share on other sites
I am not exactly sure what you want to do and what PeekMessage has to do with it. If you could post more code so I can trace what is happening that would be good.

I may end up solving this pointer mystery for you :)

Share this post


Link to post
Share on other sites
Well, that'll teach me for coding an example that isn't anywhere near what I'm using. I was half way through typing a huge response, with the code and everything and I noticed the problem:

Object g_Leader;
Object g_Follower;

g_Leader.Move(0.0f, 10.0f);
g_Follower.Move(0.0f, 0.0f);
g_Follower.SetOffset(5.0f, 5.0f);
g_Follower.BindToVector(&g_Leader.GetPosition());
// Hmm, wonder why its not working...


Geez I'm dumb.

Thanks for trying to help! (Actually, I probably wouldn't have gotten it if you hadn't asked for more code lol).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this