Another problem with the goddamn pointers

Started by
5 comments, last by LordAsm 17 years, 4 months ago
Alright, I have the following structure: A class named CManager which have a array of INodes, an interface class for some nodes (graphical elements) Okay, everything runs fine, but as soon my app calls GetMessage() to make the loop, my array of INodes loses their reference, so I can't call CManager->Node[0]->draw() anymore.... Anyone knows WHY this happens?
Advertisement
Use the debugger to determine at which point the contents of the array become invalid, and post the relevant lines of code here in [source] [/source] tags.
Okay.... these are the lines that erases the pointers...

Keep in mind this structure....

I have the following classes

absOGLRenderer
absSceneManager
ISceneNode
absTriangleNode : public ISceneNode
absCore

absSceneManager has an array of pointers of ISceneNode type, where I put the pointer of absTriangleNode. absSceneManager is declared in absCore, so it can call absSceneManager functions. absCore is declared in main file.

if (Core.initialize(hInstance)){        // Activate OPENGL	// message loop	MSG msg;	BOOL bRet;	while((bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)  // THIS IS THE LINE WHERE THE POINTERS GO AWAY...	{ 		if (bRet == -1)		{			// handle the error and possibly exit			break;		}		else		{			TranslateMessage(&msg); 			DispatchMessage(&msg); 		}		Core.render();	}}


Let me show you the debug watch before the marked line above:



After the while((bRet = GetMessage( &msg, NULL, 0, 0 )) != 0) line:

GetMessage will dispatch any sent messages.

So it's possible your code could be re-entered somewhere during the GetMessage, and it is changing the values. Put breakpoints where those values could be changed, and see if they get hit.

Are you sure that your pointers
are screwed after GetMessage() because make no sense.
I would suspect they go away after
DispatchMessage(&msg); because from DispatchMessage(&msg);
you get your WindowProcedure called.

It seems while you process some message WM_* window message
you delete a pointer to a node but the array still holds the
dagling pointer value.

You may place a breakpoint in INode destructor
when your execution flow is stopped befor GetMessage. Then step over
GetMessage and DispatchMessage see if 'who' is deleting the node.
When the node is deleted remove as well the array location or null it out
(unused array locations are not nulled out as I can see).
Then you can render all non-null pointers while for-ing
for all array elements.


MCO
Where is the code that creates the absTriangleNode and stores the pointer to it? If you're creating it on the stack and then storing the pointer to it, then it's being overwritten when other functions use the stack for their own local variables.
It was very simpler than I thought.

What was happening is that I created instances of absTriangleNode inside a absSceneManager method, just for testing, but when the compiler exits the method, it calls absTriangleNode destructor and lose my class.

I only changed this:

From:

absTriangleNode ss;

To:

absTriangleNode * ss;
ss = new absTriangleNode;

and the destructor stopped from being called, a noobish error I made.

Just found it strange that only when GetMessage is called, the debugger updated the watch.

Anyway, it is working now, thanks for the help ppl.

This topic is closed to new replies.

Advertisement