Static var empty - how is this possible?

 

Please see the picture.

Debugger is at line 1233 and values for cats and f are an empty string. Actually impossible.

And of course the if-statement returns false. 

When I change the code like that, that I set var f manually/explicitly, its already set before debugger reaches the line. But cats is still empty:

And when I also add cats manually, the same weird behavior is continued:


Things like that really scare me. 

 

I think you may be creating a conflict there. Given that it is a class, how would static values be controlled?

Given that there could be several objects of that class, how would you handle the local static variable. Would it be the same static variable for all objects of that class, or would each object have its local version of the static variable.

It is not sufficiently clear, so I am assuming that the compiler is ignoring the static class within the methods.

Even when one defines a static variable outside of the methods, they still need to be initialised "manually" outside of the declaration and as part of the globally scoped variable initialisations.

So, I'm assuming that this is one of those "unclear" states which needs further clarification in the code before it will work correctly.

 

According to the C++ equivalent, a static at the method level, acts as if it is a single static variable for all objects, so that if changed by one object, all other objects will see the updated version.

In MQL however, I don't know how it is being implemented. Maybe its the same, and you don't realise that another instance is causing the change.

It requires more testing!

 
Fernando Carreiro:

According to the C++ equivalent, a static at the method level, acts as if it is a single static variable for all objects, so that if changed by one object, all other objects will see the updated version.

In MQL however, I don't know how it is being implemented. Maybe its the same, and you don't realise that another instance is causing the change.

It requires more testing!

Well yes no. The problem is, in the morning, the class just worked as it should, and in the afternoon it did not anymore. I am using this kind of coding at a lot more classes, it works actually without any problems, also with static objects, not only variables.

A virtual function is stored in memory one time, one time per inheriting class, actually like any other function too. 

You should also find it weird, that the variables are set BEFORE they are initialized, but only when the vars are initialized like that (see example 2 and 3). 

 

I decided to to a small test Script with simple code and it worked just fine for me. Here is the code and the results.

class CTest
{
   protected:
      virtual void CountDown(void)
      {
         static int Counter = 5;
         static string Name = "My Name";
         PrintFormat("Counter [%s]: %d", Name, Counter-- );
         Name = StringFormat( "%s (%i)", Name, Counter );
      };
      
   public:
      void Run(void) { CountDown(); };
};

void OnStart()
{
   CTest objTest1, objTest2;
   
   objTest1.Run();
   objTest1.Run();
   objTest2.Run();
};
2021.05.08 15:46:03.638 teststatic (EURUSD,H1)  Counter [My Name]: 5
2021.05.08 15:46:03.638 teststatic (EURUSD,H1)  Counter [My Name (4)]: 4
2021.05.08 15:46:03.638 teststatic (EURUSD,H1)  Counter [My Name (4) (3)]: 3

So, it did work, and the static is common among all object instances.

In your case, maybe something else is at play!

 
Fernando Carreiro:

I decided to to a small test Script with simple code and it worked just fine for me. Here is the code and the results.

So, it did work, and the static is common among all object instances.

In your case, maybe something else is at play!

It's all u see, anyway, static variables within functions are not accessible from outside. 

 

It's a debugger issue giving wrong information (the executed line is not shown correctly). I encounter similar issues regularly since they remastered the debugger.

There are also a lot of errors in the ME Journal :

It's usual Metaquotes way to use us as beta-tester when they develop a new thing.

It's better in last beta 2903 but of course use it at your own risk.

 
Alain Verleyen:

It's a debugger issue giving wrong information (the executed line is not shown correctly). I encounter similar issues regularly since they remastered the debugger.

There are also a lot of errors in the ME Journal :

It's usual Metaquotes way to use us as beta-tester when they develop a new thing.

It's better in last beta 2903 but of course use it at your own risk.

Maybe you´ve overlooked this:

"And of course the if-statement returns false." - the variables are not initialized as they should be. 

 
Doerk Hilger:

Maybe you´ve overlooked this:

"And of course the if-statement returns false." - the variables are not initialized as they should be. 

I don't. My hypothesis is it's a debugger issue.

Is it doing it in normal mode too ? Use a print to check.

 
Doerk Hilger:

Maybe you´ve overlooked this:

"And of course the if-statement returns false." - the variables are not initialized as they should be. 

Since it seems, you are an experienced coder, let me share a File to help your debugging approaches.

The Doc is in the header, but is outdated.

It come uncommented and without support.... 

The most powerful macro is 

DBG_MSG_VAR(x)

which takes any variable of fundamental type.

Files:
lib_debug.mqh  49 kb
 
Alain Verleyen:

I don't. My hypothesis is it's a debugger issue.

Is it doing it in normal mode too ? Use a print to check.

Yes, it did. Thats how i detected it or rather why I started debugging here.

Reason: