empty strings

Started by BorisZ, Sun 29/05/2005 11:23:08

Previous topic - Next topic

BorisZ

I noticed something strange while showing empty strings on label. Looks like they take data from other labels or show strange characters. It looks like it is random. Is it?

DoorKnobHandle

I never noticed that.

Do you use "StrCopy ( "", string );" to make your string empty? Then it should display emptyness...

BorisZ

The problem is that I couldn't do that without major complications (and missing MAGS deadline) because I have 7 buttons on GUI that show random letters inside string that shows combined letters. Yes, I am sure it could be done different, but 3 in the morning isn't the best time for thinking  :P

DoorKnobHandle

Well then it has to be something to do with all that extra-stuff you do with the strings, because I am 100% sure, that if you define a string with "string test" and then make it empty with "StrCopy ( "", test );" and then put it out on a GUI label, that it will show "" exactly how it is supposed to be.

Scorpiorus

QuoteI noticed something strange while showing empty strings on label. Looks like they take data from other labels or show strange characters. It looks like it is random. Is it?
Something like this may happen if there is string operating script that corrupts memory.

For instance, StrCopy("", test ); would make an illegal operation. It is allowed by the compiler but may lead to hard to track down bugs.

Do StrCopy(test, ""); instead - since the first parameter is a destination string variable, the second is a source string.

But taking a look at your script would make it easier for us to try to figure out what may be wrong.

DoorKnobHandle

Sorry, I messed up the order of the parameters for the StrCopy function. I knew something sounded strange... Anyways, I think you got the idea!

BorisZ

I am sure that Ã, memory corruption is the case here!
I have a gui with two large labels (about 200 chars each). If I open that GUI first, and then open GUI I was talking about, sometimes i get error, sometimes empty strings show that other label, and sometimes just strange characters. I fixed it so it is impossible to open other GUI before closing other first :o

So, thanks for your offer, but I was just curious (I was sure it was mamory corruption or something).

How do I label this thread solved?

Scorpiorus

Quote from: [ ... ] on Sun 29/05/2005 12:01:52
Sorry, I messed up the order of the parameters for the StrCopy function. I knew something sounded strange...
Don't worry, one can't (and actually there is no need to) remember all the parameters (and their ordering) for each function, that's what the manual is for, after all.
I just pointed it out because in that particular case [ StrCopy("", test); ] the script can be compiled with no errors but as a result you will most likely get a strange behavior for the game, like some variables will be getting reset, or some occasional run-time errors (w/o descriptive error messages) etc.

StrCopy() is kind of a dangerous function because of what I've mentioned above.

For example, the following script would cause memory corruption, although the reason why it would may not be very obvious, at a first glance:

Code: ags

function DisplaySomeText(string text) {

Ã,  Ã, Display(text);
Ã,  Ã, StrCopy(text, "a very very long text");
Ã,  Ã, Display(text);

}


Code: ags

function game_start() {

Ã,  Ã, DisplaySomeText("hello world!");

}


Seems ok, but in reality what we do here is passing a "hello world!" constant to the DisplaySomeText() function, and thereby text points to it. Therefore,Ã,  Ã, 

StrCopy(text, "a very very long text");

...is becoming an equivalent of...

StrCopy("hello world!", "a very very long text");

...which is bad since the 2nd string (being a constant as well) is longer than "hello world!" and during a copy overruns it.


But the script would work ok if we passed a variable instead:

Code: ags

function game_start() {

Ã,  Ã, string message;
Ã,  Ã, StrCopy(message, "hello world!");

Ã,  Ã, DisplaySomeText(message);

}


That's why I would strongly recommend not to modify string parameters of a function. Better declare an extra string and use it instead, the following modification of the first code is ok:

Code: ags

function DisplaySomeText(string text) {

Ã,  Ã, string buffer;
Ã,  Ã, StrCopy(buffer, text);

Ã,  Ã, Display(buffer);
Ã,  Ã, StrCopy(buffer, "a very very long text");
Ã,  Ã, Display(buffer);

}


Here StrCopy copies into a buffer variable that has enough room to store "a very very long text".


QuoteSo, thanks for your offer, but I was just curious (I was sure it was mamory corruption or something).
Heh no problem, but if it indeed is a memory corruption caused by operations with strings I'd strongly recomended recheck the scripts to see if there are any lines containing something like StrCopy("hello", variable); because it's not guaranteed that a bug will not reveal itself at a later stage of the development (or even after the final release is out).

By the way, what version of AGS are you using? If the script seems ok, I'd try running it with the latest version 2.7 to see if there is any difference.


QuoteHow do I label this thread solved?
Just edit your first post and add (SOLVED) to the title of the thread.

SMF spam blocked by CleanTalk