简体   繁体   中英

sizeof crashing on iPad 1 but not > iPad 1

Consider this code:

CGFloat largerLineSpacing = kStreamCellParagraphSpacing;

CTParagraphStyleSetting paragraphSettings[1] = {
    { kCTParagraphStyleSpecifierParagraphSpacing, sizeof(CGFloat), &largerLineSpacing }
};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(paragraphSettings, sizeof(*paragraphSettings));

This code crashes with an EXC_BAD_ACCESS when running on an iPad 1 (5.1), but not a 5.1 simulator or an iPad 3 (6.0). My C is weak - am I making a dumb mistake with sizeof?

The docs for CTParagraphStyleCreate suggest that its second argument gives the number of CTParagraphStyleSetting instances in the paragraphSettings array (1 in your case) rather than the size in bytes of the array.

If you change your code to

CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(paragraphSettings, 1);

it should work. Or, if you want to cope with adding more settings in future, you could try

int numElems = sizeof(paragraphSettings)/sizeof(paragraphSettings[0]);
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(paragraphSettings,
                                                            numElems);

static CFIndex const settingCount = 1;

CTParagraphStyleSetting paragraphSettings[settingCount] = {
    { kCTParagraphStyleSpecifierParagraphSpacing, sizeof(CGFloat), &largerLineSpacing }
};

CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(paragraphSettings, settingCount);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM