views:

39

answers:

1

While memory management for the foundation classes are consistent and documented, i was surprised to find (via the friendly EXC_BAD_ACCESS) that Foundation functions like e.g NSStringFromSelector() seem to return pointers to constant storage - which is at least not mentioned in the documentation for the function.

Is that behaviour documented somewhere? Are there any consistent guidelines?

+5  A: 

The rules are really just the same as those for Objective-C methods - as is illustrated by these docs for the core foundation functions.

So in your example, because the words alloc, new, create or copy are not present the object you get back is not owned by you (it will either have static storage or will be autoreleased).

Phil Nash
I read it and somehow already managed to forget it again - thanks.
Georg Fritzsche
You obviously didn't "retain" it ;-)
Phil Nash
Cocoa has its own memory management docs ( http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/ ). The rules are slightly different; CF has no autorelease, and Cocoa uses `new` where CF uses `Create`.
Peter Hosey
These memory management rules might help - http://developer.apple.com/mac/library/documentation/cocoa/conceptual/MemoryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-BAJHFBGH
JohnnySoftware
@Peter - yeah, that's why I included both new and create in my list :-) But you are right to point out the difference since this became the accepted answer
Phil Nash