I’ve found problems using the CCBReader when deploying my game to an iPhone 4.
There are several potential lines in CCBReader where the game will crash due to memory misalignment.
in CCBReader::readHeader()
int magicBytes = *((int*)(this->mBytes + this->mCurrentByte));
should be
int magicBytes; unsigned char* pData = ( this->mBytes + this->mCurrentByte ); memcpy( &magicBytes, pData, sizeof( int ) );
in CCBReader::readFloat() there is something really dangerous
The ARM processor which is inside all iPhones, iPads, etc. Doesn’t like memory
misalignment! so be careful doing castings with memory which could be misalignment
after the casting!
Hi guys,
>
I’ve found problems using the CCBReader when deploying my game to an iPhone 4.
There are several potential lines in CCBReader where the game will crash due to memory misalignment.
>
in CCBReader::readHeader()
> int magicBytes = *((int*)(this->mBytes + this->mCurrentByte));
>
should be
> int magicBytes; unsigned char* pData = ( this->mBytes + this->mCurrentByte ); memcpy( &magicBytes, pData, sizeof( int ) );
>
in CCBReader::readFloat() there is something really dangerous
> float * pF = (float*)(this->mBytes + this->mCurrentByte); float f = 0; memcpy(&f, pF, sizeof(float));
>
should be
> float f; unsigned char* pData = ( this->mBytes + this->mCurrentByte ); memcpy( &f, pData, sizeof( float ) );
>
The ARM processor which is inside all iPhones, iPads, etc. Doesn’t like memory
misalignment! so be careful doing castings with memory which could be misalignment
after the casting!
>
You can find more info about this issue here:
> http://stackoverflow.com/questions/3243146/why-does-this-exc-bad-access-happen-with-long-long-and-not-with-int http://stackoverflow.com/questions/7788216/exc-bad-access-and-char-pointer-to-float-pointer-cast
Dear all:
According to what Óscar said,after modify ,the CocosBuilder test case can run ,but when enter the CocosBuilderTestCase ,
When I modify the CCBReader was in 2.0.4 version of cocos2d-x. I don’t know If there are more changes since that version.
So I have plenty of cocosbuilder scenes working in my current game ( I’m using cocosbuilder 2.1 ) an everything it’s working ok.
As well the bugfix is for iOS devices… but I think It should work in other devices as well.
The only thing the code does it’s just rely on memcpy to do the memory alignament.
Hi all,
I merged a Pull Request(https://github.com/cocos2d/cocos2d-x/pull/1967) which to fix for unaligned memory access crash in CCBReader::readFloat().
Could you test it? I have tested it on my iOS device(touch3). It works ok now. Let me know whether you guys also make it works.
Thanks.
I think I fixed the bug around 2 months ago! and I post the solution here but nobody cares about it…
So could tell me a better way to warn about new bugs?
Fantastic news James!
>
I think I fixed the bug around 2 months ago! and I post the solution here but nobody cares about it…
So could tell me a better way to warn about new bugs?
>
Thanks!
>
Cheers!
Hi Óscar,
Great finding bro …I was facing this issue when I read ccbi in android game (APP_OPTIM := release in application.mk ) was going to crash . But now it is solved .
Thanks again.