3 ms·
> Multi-level Cleanup Used all the time a decade or so ago when writing code that made heavy use of Apple's CoreFoundation. CF could return nil (failure) for m
by JKCalhoun 5y ago
> Multi-level Cleanup
Used all the time a decade or so ago when writing code that made heavy use of Apple's CoreFoundation. CF could return nil (failure) for many operations, and memory management at that time was left up to the programmer.
// Not real code, approximated from recollection
bool createNestedCFThing () {
CFDictionary *thing = nil;
CFArray *arrayObj = nil;
CFString *string1 = nil;
CFString *string0 = nil;
string0 = CFCreateString ("Hello");
if (string0 == nil) {
goto bail;
}
string1 = CFCreateString ("World");
if (string1 == nil) {
goto bail;
}
arrayObj = CFCreateArray ();
if (arrayObj == nil) {
goto bail;
}
if (CFArrayAddObject (arrayObj, string0) == false) {
goto bail;
}
if (CFArrayAddObject (arrayObj, string1) == false) {
goto bail;
}
thing = CFCreateDictionary ();
if (thing == nil) {
goto bail;
}
if (!CFDictionaryInsertObjectWithKey (thing, arrayObj, "somekey")) {
goto bail;
}
bail:
if (arrayObj != nil) {
CFRelease (arrayObj);
}
if (string1 != nil) {
CFRelease (string1);
}
if (string0 != nil) {
CFRelease (string0);
}
return thing;
}
- lmilcin 5y agoThis is my favorite use of goto. Not only it makes it more readable and easy to ensure the cleanup is only executed, it results in efficient binary representation which is important for some of the embedded stuff I am doing.
- mkipper 5y agoThis is the most common form of error handling used in the Linux kernel (at least the parts I look at). Although you'd normally have one error label for each allocation in reverse order, so you can jump to the cleanup step for the most recently allocated resource and pass through the other labels and clean them up as well. So in your example, it would be something like: bail_string1: CFRelease(string1); bail_string0: CFRelease(string0); bail_array: CFRelease(arrayObj); no_bail: return thing;
- vlovich123 5y agoManual memory management is bad but you can't escape it in C so `goto` is mildly useful there for that purpose. In all other languages you get better ergonomics. * In Rust you wouldn't need any of that boilerplate because lifetime is automatically managed (& Rust has Objc bindings). * If you turn on ARC & use the NS types you don't need this (they map to the same thing with no overhead for these primitives, so there's no good reason not to do this unless you are just being masochistic). * In ObjC++ you should still use ARC. But if you felt masochistic, you could do: auto string0 = unique_ptr<CFString, CFRelease>(CFCreateString("Hello")); auto string1 = unique_ptr<CFString, CFRelease>(CFCreateString("World")); auto arrayObj = unique_ptr<CFArray, CFRelease>(CFCreateMutableArray(kCFAllocatorDefault, 2, kCFTypeArrayCallBacks, kCFTypeDictionaryKeyCallBacks, )); auto thing = unique_ptr<CFDictionary, CFRelease>(CFCreateMutableDictionary(kCFAllocatorDefault, 1, kCFTypeDictionaryKeyCallBacks, kCFTypeDictionaryValueCallBacks)); if (!arrayObj || !string0 || !string1 || !thing) { return nil; } if (!CFArrayAppendValue(arrayObj.get(), string0.get()) || !CFArrayAppendValue(arrayObj.get(), string1.get())) { return nil; } if (!CFDictionaryInsertObjectWithKey(thing.get(), arrayObj.get(), "somekey")) { return nil; } return thing.release() In C++ you could also use scope guards if you didn't like the `unique_ptr` stuff (not part of the stdlib yet, but it's not hard to roll your own). The point I'm trying to make is that there are much cleaner ways of obtaining that result in whatever language you choose. If you do that, maybe you too won't be responsible for a goto fail security flaw [1]. EDIT: BTW. This advise isn't out of the blue. This was 100% a strong culture even within Apple 5 years ago. If you're concerned about performance of ARC, consider that Apple dogfoods it internally for nearly every part of the OS. The only pieces it's not used are for large historical codebases where the migration cost is a factor. Performance of ARC vs manual just isn't something anyone looks at. For the historical codebases usually one would use the ObjC++ approach instead. [1] https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/ https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-got...