PDA

View Full Version : Riddle me this one



Crisp_N_Dry
27-04-2005, 09:13 PM
I had a few bugs pop up in my code and after a bit of tracking down I discovered the weirdest thing. I put two pieces of code either side of a problematic chunk. Both were the same thing :

ShowMessage(Ships[0].Script.Name);
I then kept moving the two lines closer together and waited until they both matched up then I would know where the value was getting corrupted. I eventually got them with just one unimportant line of code (it was my log file updater ) separating them. and still the results were different. So I removed the line completely and lo and behold the results were still very different.



ShowMessage(Ships[0].Script.Name);
ShowMessage(Ships[0].Script.Name);


This code results in two different returns. How the hell is that possible. I am not using threading so unless by virtue of me simply asking what the value held within the variable is changing it then I can't see why the result should be any different.

Sly
27-04-2005, 11:00 PM
That's an odd one. Would Script or Name perhaps be properties that use a read method to return the value? Perhaps the read method is modifying the value?

WILL
28-04-2005, 01:30 AM
What kind of array is Ships? If it's dynamic this could be a clue as to whats going on. Dynamic arrays are a royal pain to manage sometimes.

JSoftware
28-04-2005, 04:27 AM
i would guess that you haven't set the length of ships or haven't created some class or something like that..

Crisp_N_Dry
28-04-2005, 02:55 PM
You are correct about it being a dynamic array Will. Although I have used SetLength before calling all of this. What I think may be going on is that I am using SetLength to set it to a length of one index i.e SetLength(Ships,1). Then I am doing some other stuff and extending the length of the array so when I use SetLength again it wipes out what was in the array originally (Shouldn't happen according to Delphi Help but still) but the data still remains in memory so the first time I check on it it gives out the correct info (correct but not locked in place because the reference count has been zeroed when I used SetLength) and when I check it again the computer has since realised it is unused erased the previous data. Does this make any sense, Delphi says that when dynamic arrays are resized the original data stays intact but this doesn't seem to be the case.

Turbo_Pascal
29-04-2005, 04:06 PM
if you increase the size of your dinamic array then your prior data IS NOT LOST, if you decrease the size of your dinamic array then you lost those elements decreased.

if you say two consecutives showmessages are given two different result then it mean your are accesing a nonallocated element in your array, normally you will recive a "Acces violation" error when that happen...except when the element is "0" where for some reason somtimes you dont.




tp.

Crisp_N_Dry
29-04-2005, 07:50 PM
Hmm, that would explain it. Although I've since found a workaround and fixed it but knowing this I'm tempted to go back and see if I can get it to work this time. Thanks for the info, will be handy to know for future reference.