开发者

Segmentation fault with CFNumberGetValue

开发者 https://www.devze.com 2023-03-22 21:54 出处:网络
I\'m totally perplexed at how this code is able to cause a segmentation fault. The code works fine on 10.5+ but seg faults on 10.4. Any ideas? The fault occurs during CFNumberGetValue.

I'm totally perplexed at how this code is able to cause a segmentation fault. The code works fine on 10.5+ but seg faults on 10.4. Any ideas? The fault occurs during CFNumberGetValue.

CFNumberRef volume_num = (CFNumberRef)CFDictionaryGetValue(dict, CFSTR("Volume"开发者_StackOverflow));
if(volume_num != NULL) {
    float volume = 1.f;
    CFNumberGetValue(volume_num, kCFNumberFloatType, &volume);
};

EDIT: the code above gets a value that was added to a dictionary by the code below. mKitManager.GetKitVolume() returns the default value of 1.0.

AddFloatToDictionary(dict, CFSTR("Volume"), mKitManager.GetKitVolume());

static void AddFloatToDictionary(CFMutableDictionaryRef dict, CFStringRef key, float value)
{
    CFNumberRef num = CFNumberCreate(kCFAllocatorDefault, kCFNumberFloatType, &value);
    CFDictionarySetValue(dict, key, num);
    CFRelease(num);
}


  • CFNumberGetValue(volume_num, kCFNumberFloatType, &volume)

It's likely that one of your variables is invalid (or points to invalid/out of range/null data).

The best way forward would be to compile with debugging enabled. Enable core dumps to be generated (look at ulimit -c )

When your application hits the SEGV, a core file will be generated. First rename it to something useful (so it doesn't get overwritten ). Then open it with gdb.

From there you can examine variables to see which isn't what it should be. You should also be able to view the source code when the core is open with gdb.

I'm not too familiar with OSX, but I'm assuming you'll have gdb/dbx or equivalent.


I tracked down the segmentation fault to this line:

mCurrentArray = CFArrayCreateMutableCopy(kCFAllocatorDefault, array_capacity, newArray);

The issue being that array_capacity was incorrect (too small) for the array being copied. Why this would seg fault at a later point in the program I'm not sure exactly. Tricky one to track down and only caused problems on 10.4.

0

精彩评论

暂无评论...
验证码 换一张
取 消