Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXC_ARM_DA_ALIGN crash

I'm having a EXC_ARM_DA_ALIGN crash in my application. Here's the code that Xcode flag as "malignant". On the simulator I don't have this crash, only on device, so I think it is a memory alignment problem. Is there anyone who knows how to fix this code? Thank you so much.

-(int) Save:(void*) pBuf {

int nNeedSize = sizeof(fType) + sizeof(sizeBrush) + sizeof(nBrushType) + sizeof(rcImage) + sizeof(count) + sizeof(data[0]) * count;

if (pBuf == nil)
return nNeedSize;

*(NSInteger*)pBuf = count; pBuf += sizeof(count);
*(BOOL*)pBuf = fType; pBuf += sizeof(fType);
(*(CGSize*)pBuf).width = sizeBrush.width;
(*(CGSize*)pBuf).height = sizeBrush.height;
pBuf += sizeof(sizeBrush);
*(NSInteger*)pBuf = nBrushType; pBuf += sizeof(nBrushType);
(*(CGRect*)pBuf).size.width = rcImage.size.width; 
(*(CGRect*)pBuf).size.height = rcImage.size.height; 
(*(CGRect*)pBuf).origin.x = rcImage.origin.x; 
(*(CGRect*)pBuf).origin.y = rcImage.origin.y; 
pBuf += sizeof(rcImage);

for (int i = 0; i < count; i++)
{
    (*(CGPoint*)pBuf).x = data[i].x;
    (*(CGPoint*)pBuf).y = data[i].y;
    pBuf += sizeof(data[0]);
}

return nNeedSize;}

And here's another part flagged as malignant:

int i;
int nTotalSize = 0;
for (i = 0; i < m_Data.count; i++)
{
    maskStroke* one = [m_Data objectAtIndex:i];
    nTotalSize += [one Save:NULL];
}

unsigned char* buf = (unsigned char*)malloc(nTotalSize+100);
unsigned char* cur_ptr = buf;
for (i = 0; i < m_Data.count; i++)
{
    maskStroke* one = [m_Data objectAtIndex:i];
    cur_ptr += [one Save:cur_ptr];
}
like image 454
Alfredo Galli Avatar asked Nov 04 '22 00:11

Alfredo Galli


1 Answers

You should use a struct or something sensible to serialize... if you can't do that, at least make sure your 32 bit and 16bit pointers are aligned to 16 or 32bit memory addresses.

*(int *foo)0x800002 (or 1) is probably going to end badly. Some processors have functions to un-screw unaligned memory addresses, but it is a waste of cycles and others (rightly) crash when you try to do this. If it doesn't crash, it is doing multiple memory accesses then combining them to create your unaligned address.

like image 165
sbingner Avatar answered Nov 13 '22 18:11

sbingner