StringFormat - strange behaviour

 

Hello,

have a Problem with StringFormat. The function SOMETIMES writes "(null)" to resulting string and there is no reason why this happens.

Find below the code for a complete script demonstrating this:

 

 

 

string get_string(void)
{
    string xtag="AAAAAAAAAA";
    return xtag;
}
void    test(const string& msg)
{
    string a=get_string();
    string b=get_string();
    string c=get_string();
    string d=get_string();
       
    Print("a=",a);
    Print("b=",b);
    Print("c=",c);
    Print("d=",d);
    string os=StringFormat("A=%s B=%s C=%s D=%s"
            ,a
            ,b
            ,c
            ,d
            );
    
    Print(os);
}  

void OnStart()
{
    string sx="abcdef";
    test(sx);
}

 

What is the secret behind THIS strange behaviour (so far it took me 2 hours to isolate...)

 

I think i figured out that functions can't return string properly when they are locally buffered.

Please check this sample out which demonstrates this: 


string get_string2(void)
{
    return "AAAAAAAAAA"; // string constant
}
string get_string(void)
{
    string xtag="AAAAAAAAAA"; // local buf of returned value
    return xtag;
}
void    test(void)
{
    string a=get_string();
    string b=get_string();
    string os=StringFormat("a=%s b=%s",a,b);    // output incorrect
    Print(os);   

    a=get_string2();
    b=get_string2();
    os=StringFormat("a=%s b=%s",a,b);      // output correct    
    Print(os);   

}  
void OnStart()
{
    test();
}
Output
2012.10.11 21:17:50	strtest (EURUSD,M5)	a=AAAAAAAAAA b=AAAAAAAAAA
2012.10.11 21:17:50	strtest (EURUSD,M5)	a=AAAAAAAAAA b=(null)

Either stack on return or local buffer itself is corrupted. 


 

Workaround

init string a before assigned by "get_string()"

 

string get_string2(void)
{
    return "AAAAAAAAAA";
}
string get_string(void)
{
    string xtag= "AAAAAAAAAA";
    return xtag;
}
void    test(void)
{
    string a="";
    string b="";
    a=get_string();
    b=get_string();
    string os=StringFormat("a=%s b=%s",a,b);    
    Print(os);   

    a=get_string2();
    b=get_string2();
    os=StringFormat("a=%s b=%s",a,b);    
    Print(os);   

}  
void OnStart()
{
    test();
}

 

 This is only a workaround. The behaviour of the string class is not normal;nobody expects this. It should be corrected. (Thank you)

 

 

 

asdasdasd 

 

Workaround above seems also not to work in all cases.

Basically i get sporadically (null) strings and only God knows why. 

 

Does problem reproduce on the latest build?

It seems to be fixed

 
stringo:

Does problem reproduce on the latest build?

It seems to be fixed

Hello,

yes, it seems to be fixed. Just checked it out with 712 

Reason: