What is the deal with NSZoneFree in (void) dealloc?

In the Apple documentation, NSObject NSZoneFree is called in the sample code () vall) dealloc:

- (void)dealloc { [companion release]; NSZoneFree(private, [self zone]) [super dealloc]; } 

You can find it in context here .

I never had an idea of ​​what I should call NSZoneFree in my subclasses of NSObject (or in some subclasses of NS_WhateverClass_) and cannot find anything convincing about this topic anywhere in the document.

All I can find about using NSZoneFree is a brief mention in the Memory Programming Guide and an explanation of the function in Basic Function Reference , but none of these documents let me know if I should worry about this in the context of the dealloc method.

Can anyone clarify when I should put the NSZoneFree call in my dealloc implementations for my own classes?

Edit: Thanks for your answers, now clearer for me :) - Dirk

+4
source share
2 answers

NSZoneFree() balances the call to NSZoneMalloc() , just as -release balances the call to -alloc or -alloc and CFRelease() balances the call to CFRetain() or CF*Create*() or, for that question, free() balances the call to malloc() or calloc() .

Given the distribution (s) of allocator (s) that the C library uses on Mac OS X, most of this is academic, but best practices say that you free up memory in memory the same way you got it, and in In the case of this class, the private instance variable was previously assigned using NSZoneMalloc() .

+2
source

According to the Foundation link :

Returns the memory to the zone from which it was allocated. The standard C function does the same, but takes the time to look for the zone to which the memory belongs.

So you basically use NSZoneFree () instead of free () when you clear the malloc'd and calloc'd memory. Usually you do not need to call it.

+1
source

All Articles