HELP!

ColinP
Posts: 1000
Joined: Mon Aug 03, 2020 7:46 pm

HELP!

Post by ColinP »

While working with static (class rather than instance) variables I encountered some unexpected behaviour, so I wrote this extremely simple test rig.

I must be just not seeing something blindingly obvious here but this really confuses me...

public void Initialize()
{
// add your own code here

Log( "1. testA = " + testA );
testA++;
Log( "2. testB = " + testB );
Log( "3. testC = " + testC );
testC++;
Log( "4. testD = " + testD );
}

// Add your own variables and functions here

private static int testA = 0;
private static int testB = 0;
private int testC = 0;
private int testD = 0;

Now when run in the debugger I expect this to print out...

1. testA = 0
2. testB = 0
3. testC = 0
4. testD = 0

BUT it prints out the following..

1. testA = 1
2. testB = 0
3. testC = 0
4. testD = 0

At first I thought that Initialize() was being called twice with Log() not working the first time but then testC would be 1 rather than 0.

Can anyone explain what I'm missing please?
ColinP
Posts: 1000
Joined: Mon Aug 03, 2020 7:46 pm

Re: HELP!

Post by ColinP »

Log() only works in the debugger so I stuck a LED on the panel and changed the code to...

LED1.SetValue( testA );

Log( "1. testA = " + testA );
testA++;

Log( "2. testB = " + testB );

Log( "3. testC = " + testC );
testC++;

Log( "4. testD = " + testD );

Interestingly the LED is lit when run in the debugger and off when run without the debugger.
josephbottinger
Posts: 35
Joined: Tue Oct 29, 2019 4:03 pm

Re: HELP!

Post by josephbottinger »

This sounds like an initialization oddity in VMD. I don't have a lot of insight into VMD's internals, though. (I would, however, avoid statics unless you really need them, and I'm not sure you do here.)
ColinP
Posts: 1000
Joined: Mon Aug 03, 2020 7:46 pm

Re: HELP!

Post by ColinP »

Obviously I don't need statics here but I do in the application. This is just test code to try and figure out why on earth my more complex application code didn't work as expected.

I first encountered the problem in the application and wrote a quick test inside its Initialize()...

if( testN == 0 )
{
Log( "testN is 0" );
testN = 10;
}
else
{
Log( "testN is not 0" );
}

Log( "testN = " + testN );

...

private static int testN = 0;

Which prints out...

TestN is not 0
testN = 10

Which seems like impossible behaviour.

The application is doing some advanced stuff so I started a new bare project to get rid of the other code and concentrate on the essentials.
josephbottinger
Posts: 35
Joined: Tue Oct 29, 2019 4:03 pm

Re: HELP!

Post by josephbottinger »

I know, I wasn't trying to say "You don't need statics in your sandbox app!" because, well, it's clearly a sandbox app. I'm just saying that it looks like an initialization oddity in VMD; I don't know what else would cause that sort of thing, other than having the class load (and run) silently and then re-instantiate the instance... like, maybe there's a verification step that causes the initializations to run first.
ColinP
Posts: 1000
Joined: Mon Aug 03, 2020 7:46 pm

Re: HELP!

Post by ColinP »

I forgot to add that if I change testN to none static it works as expected and prints out...

testN is 0
testN = 10
Last edited by ColinP on Thu Aug 20, 2020 11:05 am, edited 1 time in total.
ColinP
Posts: 1000
Joined: Mon Aug 03, 2020 7:46 pm

Re: HELP!

Post by ColinP »

Many thanks for your help Joseph.

"maybe there's a verification step that causes the initializations to run first."

That's kind of what I thought and so that's the purpose of testC which would be 1 if the initialisation code somehow ran twice.

Also testN being 10 kind of rules out just some minor initialisation problem with statics being initialised incorrectly.
Last edited by ColinP on Thu Aug 20, 2020 11:06 am, edited 1 time in total.
User avatar
Captain
Posts: 103
Joined: Sat Aug 25, 2018 11:12 am

Re: HELP!

Post by Captain »

I saw similar behaviour some time ago, can't remember any more the exact situation, but I do remember it was driving my crazy. :D Clearly there's something different happening inside VMD when running with vs without debugger, and it definitely looks like the initialization code (maybe something else too?) is run twice with the debugger. Maybe someone from Cherry could shed some light on this?
arbuxMusic
Posts: 49
Joined: Sun Aug 25, 2019 10:26 am

Re: HELP!

Post by arbuxMusic »

Not particularly useful to identify the issue, but have created a sample module that reproduces the issue and logs out the behaviour.

The log file is published on GitHub in the same location as the code as I could not get the attachment attached to this post (trying to attached a .txt file results in an invalid extension error).

Code: https://github.com/arbuxMusic/voltage-m ... Initialize
Log File: https://github.com/arbuxMusic/voltage-m ... ogfile.txt
ColinP
Posts: 1000
Joined: Mon Aug 03, 2020 7:46 pm

Re: HELP!

Post by ColinP »

Thanks for the input guys.

This has been driving me crazy as I spent a whole day trying to work out why my code didn't work. I was doing some low-level stuff with memory mapped files for fast inter-module communication to overcome the limitations with poly leads and I thought it must be my code causing the problems with it being kind of "under the bonnet" but clearly there's something seriously wrong with how the VM debugger handles statics.

For now I'm leaving that project to one side and working on something else.
Post Reply

Return to “Module Designer”