CFRelease crash

Crash on CFRelease(res)

, below is the codes, please help how to fix it.
    {
        dispatch_block_t block = ^{
            //All this dancing is an attempt to work around some weird crash:
            //seems like ARC is trying to release NULL before assigning new value
            CFDictionaryRef res = SCDynamicStoreCopyMultiple(store, NULL, keyPatterns);
            if (res && store) {
                NSDictionary *nsRes = (__bridge_transfer NSDictionary *)CFPropertyListCreateDeepCopy(kCFAllocatorDefault,
                                                                                                     (CFPropertyListRef)res,
                                                                                                     kCFPropertyListImmutable);
                setDynamicStore(nsRes);
            } else {
                ZLogError("SCU:[%s]%s refreshDynamicStore failed",
                          dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL),
                          __FUNCTION__);
                setDynamicStore(NULL);
            }
            if(res) {
                CFRelease(res);
            }
        };
        if (dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL) == dispatch_queue_get_label(queue)) {
            block();
        } else {
            dispatch_async(queue, block);
        }
    }
Answered by DTS Engineer in 879823022
The crash is not on demand.

Well, that makes it harder.

The code I post is very independent

It’s not as independent as you think. It relies on a huge chunk of shared mutable state, namely the objects allocated on the heap. It’s very common for problems like this to be caused by memory corruption in a completely different subsystem. For example, if one subsystem over-releases an object then some other completely different subsystem can crash in CFRelease. Such crashes are often very hard to reproduce, just like this one.

OTOH, hard-to-reproduce crashes like this can also be caused by concurrency problems )-

I want to reiterate my previous suggest: Put your code into a small test project and run it with the standard memory debugging tools. If this is a memory management issue within your code or System Configuration framework, those tools might make the problem more reproducible and hence more debuggable.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

How reproducible is this crash? Can you reproduce it reliably in your office? Or are you chasing bug reports coming in from the field?

Also, if you put that code into a small test project and run it with the standard memory debugging tools, does it crash there?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

The crash is not on demand. The code I post is very independent, it just load and release DS, why CFRelease crash even null was checked and release DS right after copy?

The crash is not on demand.

Well, that makes it harder.

The code I post is very independent

It’s not as independent as you think. It relies on a huge chunk of shared mutable state, namely the objects allocated on the heap. It’s very common for problems like this to be caused by memory corruption in a completely different subsystem. For example, if one subsystem over-releases an object then some other completely different subsystem can crash in CFRelease. Such crashes are often very hard to reproduce, just like this one.

OTOH, hard-to-reproduce crashes like this can also be caused by concurrency problems )-

I want to reiterate my previous suggest: Put your code into a small test project and run it with the standard memory debugging tools. If this is a memory management issue within your code or System Configuration framework, those tools might make the problem more reproducible and hence more debuggable.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

CFRelease crash
 
 
Q