SDK Question Ctd when compiled in Debug but not in Release

JMW

Aspiring Addon Developer
Joined
Aug 5, 2008
Messages
611
Reaction score
52
Points
43
Location
Happy Wherever
Hi There Everyone. Good to be back. Was stuck abroad for 10 months !

I'm perplexed, have searched but can't find a similar case.

Code compiles with my usual warnings, but ctd after few seconds unless compiled with debug which runs fine.
I know which code is causing it but can't find out why.
Don't know where to start as can't use debug.
Can someone please point me in a direction.

Working with Windows 10 (just updated) and

Microsoft Visual Studio Express 2012 for Windows Desktop
Version 11.0.61219.00 Update 5
Microsoft .NET Framework
Version 4.8.03752

Installed Version: Desktop Express

Team Explorer for Visual Studio 2012 05695-004-0030004-02149
Microsoft Team Explorer for Visual Studio 2012

Visual Basic 2012 05695-004-0030004-02149
Microsoft Visual Basic 2012

Visual C# 2012 05695-004-0030004-02149
Microsoft Visual C# 2012

Visual C++ 2012 05695-004-0030004-02149
Microsoft Visual C++ 2012

NuGet Package Manager 2.8.60318.667
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

SQL Server Data Tools 11.1.20828.01
Microsoft SQL Server Data Tools
 

dbeachy1

O-F Administrator
Administrator
Orbiter Contributor
Addon Developer
Donator
Beta Tester
Joined
Jan 14, 2008
Messages
9,214
Reaction score
1,560
Points
203
Location
VA
Website
alteaaerospace.com
Preferred Pronouns
he/him
It sounds like you have a bug that overwrites memory in the heap. What I would do is enable the Visual Studio heap debugging for debug builds and run your code in the Visual Studio debugger to find memory overwrite bugs. Check out this post for details. :)
 
  • Like
Reactions: JMW

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,842
Reaction score
2,105
Points
203
Location
between the planets
First, your post is titled the wrong way around ;)
Second, this kind of issue is extremely annoying and difficult to find. Without seeing the code, it's pretty much impossible, so you might want to post the offending section (since you say you know which code is responsible).
 
  • Like
Reactions: JMW

JMW

Aspiring Addon Developer
Joined
Aug 5, 2008
Messages
611
Reaction score
52
Points
43
Location
Happy Wherever
First, your post is titled the wrong way around
Hey, you're right ! o_O
you might want to post the offending section (since you say you know which code is responsible).
Well I thought I did until kuddel's links when it says about the 'unrelated knock on effects'....
But here's the bit that makes the (variable delay) ctd happen (in Release :LOL:)
GetBase number is just to test.
Routine only works in either compilation if lastRange is made global?

Code:
OBJHANDLE hPlanet = oapiGetGbodyByName("Earth");
OBJHANDLE h1 = oapiGetVesselByName ("J-F-35B") ;
OBJHANDLE h2 = oapiGetBaseByIndex(hPlanet, 9);

double dt = 0.25 ;
double range = 0;
double rr = 0;

if (h1 != NULL && h2 != NULL)
{
VESSEL * v1;
    v1 = oapiGetVesselInterface(h1);

   VECTOR3 pos1 ;
     v1->GetGlobalPos (pos1) ;
     
   VECTOR3 pos2 ;
    oapiGetGlobalPos (h2, &pos2);
      
    {
     range  = dist(pos1, pos2) ;
    rr = (lastRange-range) / 0.25 ;

sprintf(oapiDebugString(),"rangeRate% 0.03f :pos1% 0.03f :pos2% 0.03f :lastRange% 0.03f :range% 0.03f", rr, pos1, pos2, lastRange, range);
    }
    lastRange = range ;
}
 

GLS

Well-known member
Orbiter Contributor
Addon Developer
Joined
Mar 22, 2008
Messages
5,870
Reaction score
2,867
Points
188
Website
github.com
OBJHANDLE hPlanet = oapiGetGbodyByName("Earth");
OBJHANDLE h1 = oapiGetVesselByName ("J-F-35B") ;
OBJHANDLE h2 = oapiGetBaseByIndex(hPlanet, 9);
It's unlikely, but if hPlanet is NULL, it probably explodes (not literally) in oapiGetBaseByIndex().



v1 = oapiGetVesselInterface(h1);

VECTOR3 pos1 ;
v1->GetGlobalPos (pos1) ;
Again, unlikely, but if v1 is NULL...



VECTOR3 pos1 ;
v1->GetGlobalPos (pos1) ;

VECTOR3 pos2 ;
oapiGetGlobalPos (h2, &pos2);

range = dist(pos1, pos2) ;
If GetGlobalPos() or oapiGetGlobalPos() don't write something (valid) into pos1 and pos2, what is range?
 
  • Like
Reactions: JMW

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,842
Reaction score
2,105
Points
203
Location
between the planets
Ouch. Lots of unverified pointers in there that may cause all kinds of havoc when they're passed to the orbiter API if they're not valid.
The major question after seeing this would be: In which api callback is that code invoked?
 

n72.75

Move slow and try not to break too much.
Orbiter Contributor
Addon Developer
Tutorial Publisher
Donator
Joined
Mar 21, 2008
Messages
2,687
Reaction score
1,337
Points
128
Location
Saco, ME
Website
mwhume.space
Preferred Pronouns
he/him
Yeah, almost every CTD iive ever caused with orbiter development, is an access violation. Calling an orbiter method on a pointer that you thought wasn't null. Or bad C-style cast, because I didn't check when I copied and pasted.
 

JMW

Aspiring Addon Developer
Joined
Aug 5, 2008
Messages
611
Reaction score
52
Points
43
Location
Happy Wherever
Ouch. Lots of unverified pointers in there that may cause all kinds of havoc when they're passed to the orbiter API if they're not valid.
The major question after seeing this would be: In which api callback is that code invoked?
bool ShuttlePB::clbkDrawHUD (int mode, const HUDPAINTSPEC *hps, oapi::Sketchpad *skp)
 

GLS

Well-known member
Orbiter Contributor
Addon Developer
Joined
Mar 22, 2008
Messages
5,870
Reaction score
2,867
Points
188
Website
github.com
bool ShuttlePB::clbkDrawHUD (int mode, const HUDPAINTSPEC *hps, oapi::Sketchpad *skp)
You could/should do those calculations in a time step callback, and save the result in an internal variable, so it could then be used by that HUD callback. This way you decouple a "graphics function" (draw things in the HUD) from a "calculation function" calculate rates, etc.
This would also yield a much more accurate result for the range rate number, as you could use the timestep length that the time step callbacks send you, instead of the 0.25 you currently use.
 
  • Like
Reactions: JMW

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,842
Reaction score
2,105
Points
203
Location
between the planets
You could/should do those calculations in a time step callback, and save the result in an internal variable, so it could then be used by that HUD callback. This way you decouple a "graphics function" (draw things in the HUD) from a "calculation function" calculate rates, etc.
Only if you intend to access them multiple times in the same frame, otherwise you're better served juts pushing the math out to other functions. Avoiding any unrequired state is great for your sanity!

In any case, concerning the topic: You're not setting anything in that code, only getting, and the fact that you say the ctd happens with some delay (i.e. not immediately when your code is executed) kinda leaves only one major suspect, which is your printing to the debug string. Does the CTD still occur if you comment out just that one line? Are you absolutely positive that it can never get longer than 256 characters?
 

JMW

Aspiring Addon Developer
Joined
Aug 5, 2008
Messages
611
Reaction score
52
Points
43
Location
Happy Wherever
???:salute: Moving to void ShuttlePB::clbkPostStep (double simt, double simdt, double mjd) and "cleaning" code has cured it....
Debug print is still in there so presume that wasn't involved.
Thanks guys !

Next I need to step through BaseByIndex........

Code:
OBJHANDLE hPlanet = oapiGetGbodyByName("Earth");
OBJHANDLE h1 = oapiGetVesselByName ("J-F-35B") ;
OBJHANDLE h2 = oapiGetBaseByIndex(hPlanet, 9);

double dt = 0.25 ;
double range = 0;


if (h1 && h2 &&hPlanet )
{
VESSEL * v1;
    v1 = oapiGetVesselInterface(h1);

   VECTOR3 pos1 ;
     v1->GetGlobalPos (pos1) ;
    
   VECTOR3 pos2 ;
    oapiGetGlobalPos (h2, &pos2);
    if (pos1, &pos2 )
      {
          range  = dist(pos1, pos2) ;
    
    rr = (lastRange-range) / simdt ;

sprintf(oapiDebugString(),"rangeRate% 0.03f :pos1% 0.03f :pos2% 0.03f :lastRange% 0.03f :range% 0.03f", rr, pos1, pos2, lastRange, range);
    }
    lastRange = range ;
}
 

jedidia

shoemaker without legs
Addon Developer
Joined
Mar 19, 2008
Messages
10,842
Reaction score
2,105
Points
203
Location
between the planets
Moving to void ShuttlePB::clbkPostStep (double simt, double simdt, double mjd) and "cleaning" code has cured it....
Hmmm... Possibly not everything is done initialising when clbkDrawHud is called the first time. What is a bit surprising is that this would lead to a delayed ctd. I've had a lot of those, but usually only after setting things, because orbiter at times reacted badly at setting certain things at certain points in its "event queue".
 
  • Like
Reactions: JMW

GLS

Well-known member
Orbiter Contributor
Addon Developer
Joined
Mar 22, 2008
Messages
5,870
Reaction score
2,867
Points
188
Website
github.com
Only if you intend to access them multiple times in the same frame, otherwise you're better served juts pushing the math out to other functions. Avoiding any unrequired state is great for your sanity!
It also avoids running those calculations while the sim is paused (the HUD callback is called while paused).
 
Top