Error EXC_ARM_DA_ALIGN

I have failed EXC_ARM_DA_ALIGN in my application. Here is the code that the Xcode flag is "malignant." On the simulator, I do not have this failure, only on the device, so I think this is a memory alignment problem. Is there anyone who knows how to fix this code? Thank you very 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 is another part marked 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]; } 
+4
source share
1 answer

You should use a structure or something reasonable for serialization ... if you cannot do this, at least make sure your 32-bit and 16-bit pointers are aligned with 16 or 32-bit memory addresses.

* (int * foo) 0x800002 (or 1) will probably end badly. Some processors have functions for unscrewing unacceptable memory addresses, but this is a waste of cycles, while others (rightfully) fail when you try to do this. If it does not crash, it makes several attempts to access the memory and then combines them to create your inconsistent address.

+1
source

All Articles