Skip to main content
gamedev.net
🔒 Locked

Weird Pointers Problem

Started by weasalmongler 10 replies 400+ views
Original Post
weasalmongler
weasalmongler
Hi everyone. I am currently putting AI and Bots into my current FPS game. I have a "CAgent" class which is basically a class containing all the information about 1 bot. This class contains a "CMD2" class and this class contains a texture class. I have followed the program through with a debugger and have noticed something odd. I watch the initialization and all pointers are assigned (to things like Vertices and texture data) and they are fine, they get through error checking fine and everything is fine. When I get to the rendering function, for some reason all the pointers to data have been reset to NULL so it crashes when trying to get vertex data from an array, as the array points to NULL data. I am finding this very odd because the pointers should still be set to the correct data. I am finding it even more odd because I had a single "CMD2" class outside of the "CAgent" class earlier in development to check MD2 model rendering, and it all worked fine. It only seems to be data inside of the CAgent class that gets messed up. One more thing that may be important is that the CAgents are stored in a dynamic array which is created by the CAIManager class when a world is loaded. I have no idea what is causing this. Could anybody please help or give me some pointers (lol, no pun intended) as to what may be wrong. Many Many Many thanks in advance. - Weasalmongler [edited by - weasalmongler on June 19, 2002 4:06:21 PM]
IndirectX
IndirectX
Try data breakpoints. In MSVC, hit Alt-F9 and navigate to Data. If your pointers are stored in the same memory location, you can cast the location to a pointer type, such as

(long *) 0x12345678

and once the value of your pointer is modified you''ll be broken into the debugger.
---visit #directxdev on afternet <- not just for directx, despite the name
weasalmongler
weasalmongler
Hmmmmm..... I may now need to solve this problem without the debugger, as every time I close the debugger, it causes a major problem and I am unable to run my program again without restarting the PC. Not only that but I get a permanent "Illegal operation" nasty error box that I cannot close, not even with Alt+Ctrl+delete. *sigh* This is gonna be a tricky one. Can anybody else think what this problem may be caused by?

- Weasalmongler
IndirectX
IndirectX
If you are passing arrays as parameters, make sure you''re passing them by reference or const reference and not by value.
---visit #directxdev on afternet <- not just for directx, despite the name
Drazgal
Drazgal
Well if your debugger i having problems do what I do in the same situation, Open a text file and write the value of the pointer to it before and after every function call, then you can look at the text file and see in which function your pointer is getting reset. Then go into that function abd write the value of the pointer after every line of code and then you can find exactly what the problem is. Time consuming but useful when the debugger is crashing aswell.

Ballistic Programs
weasalmongler
weasalmongler
Hmmmm..... Sorry guys but I think the problem is a little stranger than this.

Each "CAgent" currently only has 2 functions. And "init()" function and a "Render()" function. ALL pointers are PRIVATE variables of the class so CANNOT be accessed outside of the class. At the very end of the init() function.... all variables are OK, on entering the Render() function... they are all null..... this is VERY odd. Anyone got any more ideas?

- Weasalmongler

EDIT : P.S. I'm not passing any arrays at all .

[edited by - weasalmongler on June 19, 2002 5:00:08 PM]
SabreMan
SabreMan
Create the smallest possible complete program that demonstrates the problem and post the code.
weasalmongler
weasalmongler
Erm, I fear that there is far too much information to get into a simple program. As I am not sure what is causing the problem it would be difficult to try and simulate it. I was just after things that MIGHT be causing it so that I can check my program for these problems. I know it is difficult to reply when there is no code to work from but could ppl please try their best, because I am killing myself over this problem.

- Weasalmongler
SabreMan
SabreMan
You are almost certainly clobbering memory somewhere, possibly by over-running array bounds or doing something with memory that doesn''t belong to your process. Without code, it''s not possible to say any more than that.
weasalmongler
weasalmongler
OK, sorry to waste peoples time. I think I know what caused it.

I was creating agents like this:


  
m_agents = new CAgent[numAgents];


I was setting my number of Agents to 1 for the test so it created 1 agent. I was then accesing the array like this :


  m_agents[i].LoadAgentFromFile(file* agentfile);  


when it should have been:


  m_agents->LoadAgentFromFile(file* agentfile);  


as there was only one object in the array. I changed it and now it works.... It'll only take a small amount of adaption to get it working properly. Thank you everyone for your help.

- Weasalmongler

EDIT : Source code boxes

[edited by - weasalmongler on June 19, 2002 6:54:22 PM]
IndirectX
IndirectX
Use containers (std::vector) to prevent things like this from happening again.
---visit #directxdev on afternet <- not just for directx, despite the name
weasalmongler
weasalmongler
Cool, I might look into that coz arrays can be nasty.... people should probably forget that last post I made because after further considerations it was not that but something else more complicated... which that just happened to fix. As everything is fine now I thank you for all your help again and shall get back to work on my AI :D.

- Weasalmongler

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.