C++ Question Pointers, Thruster handles... and a mistery I can't solve

fred18

Addon Developer
Addon Developer
Donator
Joined
Feb 2, 2012
Messages
1,667
Reaction score
115
Points
78
Hi guys,

I'm encountering a curious error while using pointers... I couldn't understand what was going on so I started to log here and there, and make experiments... and here is the point i manage to get to:

I have a function to which a ThrusterHandle is passed.
Code:
PSTREAM_HANDLE Multistage2015::AddExhaustStreamGrowing(THRUSTER_HANDLE  th,    const VECTOR3 &  pos,    PARTICLESTREAMSPEC *  pss = 0  , bool growing=FALSE, double growfactor_size=0, double growfactor_rate=0, bool count=TRUE) 
{
	psg[nPsg].pss=*pss;
	psg[nPsg].th=&th;
	psg[nPsg].psh[0]=AddExhaustStream(*psg[nPsg].th,pos,pss);
	if(count){	nPsg++;}

///Log to understand what's happening...
	VECTOR3 thdir;
	GetThrusterDir(*psg[nPsg].th,thdir);
	sprintf(logbuff,"nPsg:%i %.3f %.3f %.3f",nPsg,thdir.x,thdir.y,thdir.z);
	oapiWriteLog(logbuff);


	return psg[nPsg-1].psh[0];
}
in my class I have a structure called psg which is an array that contains a thrusterhandle pointer variable, and I store the address of the thruster handle in my structure, line 2 inside the function psg[nPsg].th=&th (nPsg is just a counter).

Now, to access the original thruster handle I'm supposed to use *psg[nPsg].th right? and that seems to work at the beginning.

I added the last part of code to log some lines with a test function for the thruster which will show me if the pointer works or not. Well if I leave it as it is in the code it works and i get the log of everything but...

If I change this:
Code:
GetThrusterDir(*psg[nPsg].th,thdir);
with simply this:
Code:
GetThrusterDir(*psg[0].th,thdir);

bang... I get a CTD. The log shows that the cycle happens for the first two times so I see the thdir o psg[0] components printed twice before CTD... now, why this does not happen if I leave the first line of code? it seems that the psg[0].th pointer becomes invalid at the third loop, but I really don't get this... Any help?

Thanks in advance!
:tiphat:
 
First thought: Isn't the THRUSTER_HANDLE to be handled opaque? It is maybe a pointer underneath but I still think you should handle it like a value (so don't use address operator "&" here).
2nd: you are taking the address of a passed variable (th). Most likely the parameter was passed to the function on the stack! So once the stack is "rolled back" (and reused) that address points to anything, but most likely not to the thruster handle.
 
Thank you! I think you got exactly what is going on here. My issue is that i need to keep track of my thruster because i will need to add more streams to it, but if i treat it like a value as i was doing originally i end up adding streams to a copy of the thruster that i created at the beginning, without keeping track of all the changes happened in between. I think i'll have to find another way to keep reference of my thruster! Thank you very much!
 
A handle is already a pointer (or at least a wrapper for one, I never actually bothered to check whether it wraps a pointer or whether its' just a typedef...). Storing a pointer to a handle doesn't make a lot of sense in most situations, but what breaks your code here is this:

While the memory location of the thruster (the location the handle wraps) is guaranteed to be constant, the handle itself is a temporary thing. It's on the stack somewhere. You're keeping a pointer to an object that won't be there anymore when the function that gave it to you terminates.
 
Back
Top