Problem Deleting data passed via clbkGeneric

Zatnikitelman

Addon Developer
Addon Developer
Joined
Jan 13, 2008
Messages
2,302
Reaction score
6
Points
38
Location
Atlanta, GA, USA, North America
Finally moved to the Orbiter10 API and as I've started to update my code to Orbiter10, I've began using clbkGeneric to pass data between vessels rather than the KeyComm method using clbkConsumeBufferedKey. But I'm having a problem so far passing very simple data. I need to pass a double to a vessel.
First is two ways I've tried from the calling vessel:
Code:
double *temptime = new double(guidanceStartTime);
ves->clbkGeneric(PAD_FULL_LAUNCH,0,temptime);

//second method
ves->clbkGeneric(PAD_FULL_LAUNCH,0,new double(guidanceStartTime));

Regardless of which method I use, I get a CTD when I try to delete the context pointer in the target vessel:
Code:
clbkGeneric(int msgid, int prm, void *context)
double* time;
...
time = (double*)context;
T0 = *time;
delete time;
The CTD occurs when I try to delete time. I though I had gotten pretty good and doing strange and unusual things with pointers, but I guess there's still more out there.
Thanks for any help,
Zat
 

martins

Orbiter Founder
Orbiter Founder
Joined
Mar 31, 2008
Messages
2,448
Reaction score
462
Points
83
Website
orbit.medphys.ucl.ac.uk
Allocation and deallocation of memory across modules is generally not a good idea. AFAIK, for dynamic modules (dlls) this may work because they share their heap, but it will not work if one of the modules is statically linked, because it has its own heap, and an external module trying to deallocate memory in there results in an access violation.

So in your example it would be better for the caller to do the deallocation, once clbkGeneric returns. The body of clbkGeneric in the target vessel should simply copy the contents of the data package, but not keep a reference to the pointer (which becomes invalid once the function has returned).
 

Zatnikitelman

Addon Developer
Addon Developer
Joined
Jan 13, 2008
Messages
2,302
Reaction score
6
Points
38
Location
Atlanta, GA, USA, North America
Yep, that did it. I moved the delete statement back into the calling vessel and now it works. Thanks Martin!
As a side note, is there an easier way of doing a copy constructor on a struct other than doing this->x = obj.x; this->y = obj.y etc.? For the simpler data structure I'm working with, it's not too big a deal, but for something larger, it could get tedious, and I'm also not sure how efficient this method is.
 

martins

Orbiter Founder
Orbiter Founder
Joined
Mar 31, 2008
Messages
2,448
Reaction score
462
Points
83
Website
orbit.medphys.ucl.ac.uk
Well, you could explicitly define a copy constructor (which takes a reference to an object of its own type as an argument). Probably you may also need to overload the assignment operator (operator=). Having said that, for structs the default behaviour of "=" may actually be to copy the data contents, but I am not sure there.
 

dbeachy1

O-F Administrator
Administrator
Orbiter Contributor
Addon Developer
Donator
Beta Tester
Joined
Jan 14, 2008
Messages
9,217
Reaction score
1,563
Points
203
Location
VA
Website
alteaaerospace.com
Preferred Pronouns
he/him
Having said that, for structs the default behaviour of "=" may actually be to copy the data contents, but I am not sure there.

That is correct. :) The C compiler automatically generates a "copy-by-value" constructor for classes & structures if an explicit copy constructor is not defined. Of course it's only a shallow copy, but for simple objects that's usually fine.
 
Top