openfrog
openfrog

Reputation: 40735

Do I own the object returned by AudioSessionGetProperty?

The method has "Get" in it's name and doesn't return something via return. It takes a pointer for example to a CFStringRef, like this:

CFStringRef outValue;
UInt32 propertySize = sizeof(outValue);
AudioSessionGetProperty(propertyID, &propertySize, &outValue);

The documentation says that the function "copies" the value into the the provided outValue pointer.

So this is creating an object with a +1 retain count, and I am responsible for releasing / freeing that data?

Upvotes: 1

Views: 948

Answers (1)

Tim Dean
Tim Dean

Reputation: 8292

According to the documentation for AudioSessionGetProperty, it depends on whether the property you are getting is a C type or if it is a core foundation value:

Some Core Audio property values are C types and others are Core Foundation objects:

If you call this function to retrieve a value that is a Core Foundation object, then this function—despite the use of “Get” in its name—duplicates the object. You are responsible for releasing the object, as described in “The Create Rule” in Memory Management Programming Guide for Core Foundation.

So if what you are getting is a core foundation property value, you will need to make sure to release the resulting object.

Note that using ARC will not automatically take care of this for you. From the ARC programming guide:

In many Cocoa applications, you need to use Core Foundation-style objects, whether from the Core Foundation framework itself (such as CFArrayRef or CFMutableDictionaryRef) or from frameworks that adopt Core Foundation conventions such as Core Graphics (you might use types like CGColorSpaceRef and CGGradientRef).

The compiler does not automatically manage the lifetimes of Core Foundation objects; you must call CFRetain and CFRelease (or the corresponding type-specific variants) as dictated by the Core Foundation memory management rules (see Memory Management Programming Guide for Core Foundation).

If you cast between Objective-C and Core Foundation-style objects, you need to tell the compiler about the ownership semantics of the object using either a cast (defined in objc/runtime.h) or a Core Foundation-style macro (defined in NSObject.h):

Upvotes: 3

Related Questions