Sunday, January 11, 2009

AIR String concatenation bug

Well the other day I was doing some file IO to tidy up some CSV files. I was trying to do this quickly and just was using a String to stored the data before writing it to file. I'm sure your thinking I should be using stream for this and I would agree with you. But at the time I didn't believe there would be an issue with the data size. Then I found my fairly simple AIR app crashing quite badly. OS X kill the app because of the following error.

Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000000000000
Crashed Thread: 0

As you can see this is far from nice and most importantly this is a operating system error not a native AIR/AS3 error. So what is causing this fatal error? Well just concanate two large strings you will get this. I created a dummy programme to show the issue.

var myStr:String = new String("1");
for ( var i:int = 0; i < 100; i++) {
myStr = myStr + myStr;

On my machine it completed to the 29th loop before failing. So it could handle 2^29 but no larger.

Can you protect your app from this with a try/catch statement? No. This is a real pain in the neck. It's not like I'm writing the app in C/C++ where you can drive your app into the wall because you the developer are managing memory. With ActionScript I would expect the Flash player/runtime to return an error and not crash since it is managing memory.

I will add this as a bug or vote for this fix if it is already entered.

No comments: