Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Out of bounds bug found - causes random crashes

Author
Message
Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 1st Oct 2013 08:49
I have lately run into a number of cases were my code crashes unexpectedly. In each case I have found that I have caused an array out of bounds error that wasn't reported. It just caused a random crash. Is this already a known bug? I have made some test code to illustrate this.


This works as intended. Giving me an out of bounds error.



This doesn't give me an error, instead it makes the program stop responding and then crashing after a few seconds.


Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 1st Oct 2013 13:49
I can confirm this as well. Strange.

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 1st Oct 2013 16:20
This is a known issue. The AppGameKit compiler will never catch an out of bounds error during compilation because arrays are so changeable in length (the redim command).

And, even in C++ (and not just in AppGameKit Tier 2), if your code doesn't check for bounds in arrays, it may crash if you write to areas past the end of an array. This is not specific to AppGameKit by any means.

It is a programmer error issue. You really need to make sure that you don't try to use indices outside of what has been allocated for an array.

What does everybody expect to happen if you try to add data past what you've allocated. If every compiler in the world added code in the compiled application that checked to see if a value used as an index went past the end of the space, an large percentage of applications would become very bloated. It just isn't done that way.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Oct 2013 16:43
I don't think the compiler is the problem, it's the lack of an error message before the crash, right?

You ain't seen me right?
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 1st Oct 2013 17:59 Edited at: 1st Oct 2013 18:13
Typically, if someone stores something past the end of an array, they are writing into the program memory that has either executable 'code' or is for another variable.

Both these situations may cause a crash and there is no way for the app that is executing to know that this is about to happen so there is no way for it to produce an error message that has any meaning.

While using the Visual Studio debugger, if this situation happens (and it does, even to the best of us, when your variables for indices are not set right), then it stops and says that there was a crash. It doesn't know what happens, but it can usually show the stack trace.

But, in the final executable, it simply crashes. On Windows or Mac you usually get some message saying it crashed, but no other information. On iOS and Android, no such luck. The app usually just disappears.

EDIT EDIT: I just retested the original code, exactly as entered, and got the proper out of bounds error for the first example. But the second sample didn't crash or anything and responded properly to the pointer click. This is not consistent, in either case, with my earlier test. There appears to be a randomish bit here.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 1st Oct 2013 19:26
The first example is what I expect to happen. As soon as I add another variable to the UDT it generates random crashes. IIRC DBP doesn't have this problem. I'll add this to the issues board later, if I can't find it there.

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 1st Oct 2013 20:28
did akg have something like LBound(Array) UBound(Array)?
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 1st Oct 2013 20:53
No, it doesn't.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 2nd Oct 2013 01:17
Yeah... this is the bane of my existence, and I've begun debugging random crashes by immediately suspecting an OOB UDT array.
I reported it back in Feb
https://code.google.com/p/agk/issues/detail?id=495&can=5
Unfortunately no matter how much I complain it seems TGC either isn't concerned with fixing this or they're going to wait until v2, which stinks.
Yes it is a programmer issue, but it shouldn't cause such random crashes and should at least report an error. If AppGameKit can handle keeping track of int,string,float array bounds during runtime it should be able to handle UDT array bounds as well.
This and multidimensional arrays losing data on redimensioning is a big pain.
Would be nice if core issues like this were fixed before enhancements like Ouya compatability were made, but if I'm the only person complaining about it then AppGameKit has little reason to fix it because they can just blame it on my low level programming skills (not that they actually do this , but I might if I were in their shoes, prioritizing).
So please add any of your findings to issue 495.
Thanks!

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 2nd Oct 2013 03:49
Would AppGameKit know the range of memory the program allocates and thus prevent any part of the program from accessing outside that range of addresses?

Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 2nd Oct 2013 04:51
Quote: "Yes it is a programmer issue, but it shouldn't cause such random crashes and should at least report an error. If AppGameKit can handle keeping track of int,string,float array bounds during runtime it should be able to handle UDT array bounds as well."

Programmer error is programmer error. In the grand scale of things, it's not the language maker's job to safeguard the programmer against his own sloppy code unless that's the point of the language. AppGameKit has much larger issues with it as far as I can tell, and as such I assume Paul has this issue on the very bottom of his priorities.

And do you really want another int attached to each and every single array you use in a program/game? That could really stack up on mobile if you're not careful.

Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 2nd Oct 2013 05:03
Yes, yes I do want the extra int or multiple as long as I throws a proper oob error. If it's not their job to put safeguards in place then why put any in?

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 2nd Oct 2013 05:19
Quote: "Would AppGameKit know the range of memory the program allocates and thus prevent any part of the program from accessing outside that range of addresses?"

It can be very hard for a compiled app to know if an index for an array is out of bounds in an environment where the array can be dynamically re-sized anywhere in the program. This is not AppGameKit specific, it is a common problem across compiled stuff.

Some things, like C++ std::vector and such, have built in logic in the class that always checks an index against a known value stored within the class.

Quote: "Yes, yes I do want the extra int or multiple as long as I throws a proper oob error."

Do you really mean that you want to put an app on the market that might crash because of programmer error and that is okay as long as a message pops up that tells the customer that there was a specific problem? Or do you mean you want the message to tell you when you've made a programming mistake?

Hopefully, this is the kind of thing that the Tier 1 debugger in V2 will make a non-issue.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 2nd Oct 2013 05:29
I want the debugger. Tired of crying for this. No one cares so I shouldn't either. I've already had to code in extra error checks for this. Wasting even more time talking about it here is doing nothing but aggravating me and not helping anything.
Bowing out.

Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 2nd Oct 2013 08:56
This is something that should work in Tier 1. If you want to be more hard core and do it yourself, stick with Tier 2. It is currently half working and thus I expect it to work every time. There is an OOB error message, when it doesn't show it's a bug.

Thanks Naphier, I'll add my findings to that thread.

JohnnyMeek
11
Years of Service
User Offline
Joined: 23rd Apr 2013
Location: Slovenia
Posted: 2nd Oct 2013 10:06
I think the issue here is the inconsistency. If it reports an error for one instance but not another then it is confusing.

You work under the assumption that you'll get notified of an OOB array, because you do in some circumstances. It makes you assume that the crash is not an OOB.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 2nd Oct 2013 16:07
Quote: "I want the debugger."

It's coming in V2.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 2nd Oct 2013 16:11
Simply solution, don't make mistakes

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 2nd Oct 2013 17:03
Quote: "Simply solution, don't make mistakes"

Don't I wish it were that easy. But, I suppose there might be a perfect programmer out there. Until he/she has to deal with the client's unrealistic demands and then the frustration starts.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 3rd Oct 2013 19:21
Use protective programming!

For each array, have an integer value with its current maximum value. If you re-dimension, change the max variable.

Then NEVER loop to some constant - loop only to the current maximum.

In Pascal you can do:

for x := Low(MyArray) to High(MyArray) do ...

In Basic it might look like:

for x = 0 to MyArrayMax ...

That will never blow up in your face.

-- Jim - When is there going to be a release?
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 3rd Oct 2013 19:47 Edited at: 3rd Oct 2013 19:48
I agree with Jim here, you have to keep track of your array size and keep it up to date. Absolutely every time you redim your array you should be updating a single variable that you use to track the size.

Generally I initialise like this:


Then to increase the array size:


Then when I'm looping through checking for anything that needs deleting:


I only ever get an error like the one you are describing if this process is slightly screwy and only rarely because I stick to a method like this as precisely as possible every time.

You ain't seen me right?
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 3rd Oct 2013 20:22
Good code baxslash.

BTW, what happened to your badges and title?

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 4th Oct 2013 09:08
Hodgey - That last bit of code would not compile in Pascal, because you are altering the loop variable inside the loop. It's much safer to go DOWN the array when deleting things.

Also - if 20 things are marked for deletion, you are re-dimensioning the array 20 times, which is not good in performance terms. You can avoid this by having a control variable called (say) Top which is initialised to MyArraySize. Use that to move the [Top] value into the deletion point and then dec Top.

After the loop, if Top <> MyArraySize, dim it to top and put the new value into my array size.

-- Jim - When is there going to be a release?
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 4th Oct 2013 11:12
Quote: "BTW, what happened to your badges and title?"

I had enough of being a Mod and a target so I'm lowering my profile. PM me on FB if you want to know more, essentially I have too much on my plate.

You ain't seen me right?
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 4th Oct 2013 14:00
Quote: "Hodgey - That last bit of code would not compile in Pascal, because you are altering the loop variable inside the loop. It's much safer to go DOWN the array when deleting things."

I believe you mean baxslash Jim?

I'm still getting used to your normal profile bax. I hope that one day you'll be up to being a Moderator again.

Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 4th Oct 2013 14:57
You should have some AppGameKit developer badges though right Bax? - not sure why they're gone.

The mod lounge won't be the same without you.

I am the one who knocks...
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 4th Oct 2013 15:13
Quote: "After the loop, if Top <> MyArraySize, dim it to top and put the new value into my array size."

That would be faster. I will start redimming at the end as you suggested. Good idea.

Quote: "You should have some AppGameKit developer badges though right Bax? - not sure why they're gone."

I dumped them as well as my Mod badge. I felt there were too many of them (starting to look ostentatious) and the mood I was in was "all or nothing". I'd rather not discuss it any further but I will miss the mod lounge.

You ain't seen me right?
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 4th Oct 2013 19:22
Sorry Bax and Hodgey. On Corfu at the moment, and was slightly suffering from too much lubrication last night!

Never post with a hangover, and never do anything inside a loop if you can do it outside!

-- Jim - When is there going to be a release?

Login to post a reply

Server time is: 2024-05-07 17:35:10
Your offset time is: 2024-05-07 17:35:10