Using malloc/free in Objective-C object
I have a class AudioManager
with a member of type AudioBufferList *
. (This is a struct declared in the CoreAudio
framework). Since AudioBufferList
is not a NSObject
, I 开发者_Python百科cannot retain it, so I have to alloc/free it (correct me if I'm wrong).
My question is, where is the 'right' place to free it? Currently I am doing it in the dealloc
method of AudioManager
. If I understand correctly, this method is invoked automatically once the release
message is sent to the instance of AudioManager
--- is that true? Is there any other recommended practice regarding using alloc/free on non-objects members of Objective-C objects?
Edit:
From Apples documentation:
Subclasses must implement their own versions of dealloc to allow the release of any additional memory consumed by the object—such as dynamically allocated storage for data or object instance variables owned by the deallocated object. After performing the class-specific deallocation, the subclass method should incorporate superclass versions of dealloc through a message to super:
Which makes things a little bit clearer - but more insights are appreciated.
Yes, you do need to malloc/free the memory. There's a great answer/example here, proper memory-management included:
iPhone: AudioBufferList init and release
As for "when", you'll want to not only free the memory when the AudioManager is released, but also if the value of your AudioBufferList is ever changed. Eg. If the pointer is initially referencing some "instance A" of the AudioBufferList struct, and you change it to point to some "instance B" of the AudioBufferList struct, then you'll want to free the memory for "instance A," otherwise it may be lost. (And the memory will be leaked.)
One important point, and maybe it's one that you knew but accidentally mistyped: dealloc
is not called when release
is sent to an object. An instance of NSObject is only deallocated when its retain count has reached zero. An object could have a retain count of 2, and after being sent [myObject release]
, its retain count would become 1. But it will not be sent the dealloc
message, as this implies that some other object still "owns" that instance, and is relying on the object to remain allocated and available.
You should not need to do malloc or free on it. You would use a struct, such as CGRect or AudioBufferList, the same way you would use an int or double. There is no issue with retain, alloc, dealloc, release, etc, on non-object types.
As for the dealloc
method of AudioManager
, it will not automatically be called when the release
message is sent, it will automatically be called when the retain count equals 0. Sending a release
message to an object decreases the retain count by 1. It could still have other things that retain it, in which case it will not yet be dealloced.
Edit
Example of how Apple uses struct types:
CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
CGRect rect;
rect.origin.x = x; rect.origin.y = y;
rect.size.width = width; rect.size.height = height;
return rect;
}
精彩评论