CCImage::getData() is not properly working on android

This code is crash at memcpy function call on android.
On iOS It’s working good without crash.

CCImage image = new CCImage;
image~~>initWithImageFile;
int len = image~~>getDataLen;
unsigned short iheight = image~~>getHeight;
unsigned short iwidth = image~~>getWidth;
CCLog;
memcpy, len
4); // <—— CRASH

image->release();

I think it’s because of returned pointer value of “image->getData()” is not correct on android.

I am using Cocos2d-x 2.0.4 and
This code is tested with android OS 2.2 and 2.3.

Please Help me!

Are values logged by CCLog(“image length width height: d d %d”, len, iwidth, iheight); correct for your image? Also make sure that your image is 32bit and not 24bit if you are multiplying len with 4.

Leszek S wrote:

Are values logged by CCLog(“image length width height: d d %d”, len, iwidth, iheight); correct for your image? Also make sure that your image is 32bit and not 24bit if you are multiplying len with 4.

Yes, all values are correct!

image width = 240
image height = 160
length = 240 * 160 = 38400 (number of pixels of the image)

color depth of the image is 32bit.

It’s hard to say what could be the problem so I can only advice you to debug it more. You did not show what is SDFInMap. Maybe SDFInMap is a wrong pointer and not the pointer returned by getData(). Did you check that? Did you check to what data point both pointers? Is getData() pointer NULL or random value?

Leszek S wrote:

It’s hard to say what could be the problem so I can only advice you to debug it more. You did not show what is SDFInMap. Maybe SDFInMap is a wrong pointer and not the pointer returned by getData(). Did you check that? Did you check to what data point both pointers? Is getData() pointer NULL or random value?

SDFInMap is just simple array.

unsigned char SDFInMap[WIDTH * HEIGHT * 4];
unsigned char SDFOutMap[WIDTH * HEIGHT * 4];

SDFInmap is not wrong pointer.

I had replaced image~~>getData to SDFOutMap.
ex) memcpy;
In this case memcpy did not crash.
therefore I conclude image~~>getData() is returning wrong pointer.

Any way this is working well on iOS platform.

I suspect CCImage::getData() has a bug on Android platform.

SeongWan Kim wrote:

I checked image~~>hasAlpha.
I have found that return value of image~~>hasAlpha() is different between iOS and Android.

iOS value is true.
Android value is false.

then I have rewrote the code like this.

CCImage image = new CCImage;
image~~>initWithImageFile;
int len = image~~>getDataLen;
unsigned short iheight = image~~>getHeight;
unsigned short iwidth = image~~>getWidth;
bool hasAlpha = image~~>hasAlpha;
int bits = image~~>getBitsPerComponent;
unsigned char
pImage = image~~>getData;
CCLog;
if {
memcpy;
}
else {
for {
SDFInMap[i \* 4 + 0] = pImage[i \* 3 + 0];
SDFInMap[i \* 4 + 1] = pImage[i \* 3 + 1];
SDFInMap[i \* 4 + 2] = pImage[i \* 3 + 2];
SDFInMap[i \* 4 + 3] = 255;
}
}
image~~>release();

Now No Crash

Thanks ‘Leszek S’
Your advice helped me to solve the problem.