Jump to content

[SOLVED] Bug: Local variables in methods are not deleted after method call


photo

Recommended Posts

Hello

 

 

Had a strange problem today with the output while using strings.

After some searching in the code, I couldn't find any logic problems but a "bug" or not so a nice behavior of the engine.

 

When declaring a local variable in a method, this variable is re-used with all information associated when the method is called a second time or a third.

 

class TestLog
{
   	void print(string _string)
   	{
           	string arg; // The old memory is used and not cleaned after the first call
           	arg = arg + _string; // This line is the problem while the old information is still in arg
           	log.error(arg + "\n");
   	}
};
TestLog logger;
int init()
{
   	logger = new TestLog();
   	return 1;
}
int update()
{
   	logger.print("Hello! ");
   	return 1;
}
int shutdown()
{
   	return 1;
}

 

 

When executing this code, the output should be:

 

23:05:21 Hello!

23:05:21 Hello!

23:05:21 Hello!

 

The local variable should be deleted and cleaned up after the method is left.

 

But the real output is:

 

 

23:05:21 Hello!

23:05:22 Hello! Hello!

23:05:22 Hello! Hello! Hello!

23:05:22 Hello! Hello! Hello! Hello!

23:05:22 Hello! Hello! Hello! Hello! Hello!

23:05:22 Hello! Hello! Hello! Hello! Hello! Hello!

23:05:22 Hello! Hello! Hello! Hello! Hello! Hello! Hello!

23:05:22 Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello!

23:05:22 Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello!

 

 

kind regards

Manuel

Link to comment
It is a Unigine script feature :). Local variables are static. See here: https://developer.un...features#scopes If you do not want to accumulate arg variable inside print function, you should clear it before usage:
 class TestLog { void print(string _string) { string arg = ""; arg = arg + _string; log.error(arg + "\n"); } }; 

 

Hello Anet

 

 

Thanks for the information. Strange never noticed this! I fixed it like you wrote it, but it is still strange for me.

For optimal memory usage, I have to delete in this case all variables declared and initialized in a method at the end.

 

 

kind regards

Manuel

Link to comment
×
×
  • Create New...