<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>No pain no gain &#187; Cocoa</title>
	<atom:link href="http://www.dikant.de/category/cocoa/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dikant.de</link>
	<description>Personal blog of Peter Dikant</description>
	<lastBuildDate>Sun, 23 May 2010 20:57:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>NSToolbar basics</title>
		<link>http://www.dikant.de/2007/09/05/nstoolbar-basics/</link>
		<comments>http://www.dikant.de/2007/09/05/nstoolbar-basics/#comments</comments>
		<pubDate>Wed, 05 Sep 2007 14:00:41 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[NSToolbar]]></category>

		<guid isPermaLink="false">http://www.dikant.de/2007/09/05/nstoolbar-basics/</guid>
		<description><![CDATA[Building a Cocoa based user interface using the Interface Builder is really easy and produces great results quickly. But when I wanted to add a toolbar to a window, I had to realize, that Interface Builder does not support toolbars. &#8230; <a href="http://www.dikant.de/2007/09/05/nstoolbar-basics/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Building a Cocoa based user interface using the Interface Builder is really easy and produces great results quickly. But when I wanted to add a toolbar to a window, I had to realize, that Interface Builder does not support toolbars. So I started reading the apple developer documentation regarding the NSToolbar class and found out that it is actually easy to work with toolbars. There is just no Interface Builder support for them. So here comes a small howto about NSToolbar and its use in your applications.</p>
<p>Cocoa works with distinct toolbar identifiers to distinguish between multiple different toolbars in one application. The toolbars can be fully customized by the user and changes to a toolbar are synchronized to all toolbars with the same identifier. For example in an application with multiple edit document windows, all these windows would have a toolbar with the same identifier. If a user would change on toolbar, alle currently opened edit windows would synchronize their toolbars to the new changes.</p>
<p><span id="more-29"></span></p>
<p>When you want to add a toolbar to your application, make sure, that your controller class has a handle to the window which will hold the toolbar. So you need to define an outlet</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">IBOutlet <span style="color: #400080;">NSWindow</span> <span style="color: #002200;">*</span>window;</pre></div></div>

<p>and connect this outlet in Interface Builder to the window you want to use for the toolbar. Now you need to initialize the toolbar during the application startup. The best place for this is the <code>awakeFromNib</code> method, because at the time this method is called, all nib-Files are already initialized and our outlets are already connected. If you want to add a toolbar from a <code>NSDocument</code> subclass, you need to do the initialization in <code>windowControllerDidLoadNib:</code>.</p>
<p>Let&#8217;s take a look at the toolbar initialization in the <code>awakeFromNib</code> method of our application controller:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>awakeFromNib
<span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// create the toolbar object</span>
    <span style="color: #400080;">NSToolbar</span> <span style="color: #002200;">*</span>toolbar <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSToolbar</span> alloc<span style="color: #002200;">&#93;</span> initWithIdentifier<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;MySampleToolbar&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// set initial toolbar properties</span>
    <span style="color: #002200;">&#91;</span>toolbar setAllowsUserCustomization<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>toolbar setAutosavesConfiguration<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>toolbar setDisplayMode<span style="color: #002200;">:</span>NSToolbarDisplayModeIconAndLabel<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// set our controller as the toolbar delegate</span>
    <span style="color: #002200;">&#91;</span>toolbar setDelegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// attach the toolbar to our window</span>
    <span style="color: #002200;">&#91;</span>window setToolbar<span style="color: #002200;">:</span>toolbar<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// clean up</span>
    <span style="color: #002200;">&#91;</span>toolbar release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The above code is pretty straightforward. We create a new toolbar object, set some basic attributes which enable the user to modify the toolbar contents and set the initial display mode to display both icons and text for the toolbar items. Then we register ourselves as a delegate for the toolbar. All further programming is then done in the delegate methods. After that we add the toolbar to our window, and clean up memory (don&#8217;t worry the window object retains our toolbar, so it will not be disposed right away).</p>
<p>When working with toolbars, we need to implement at least delegates for the following actions:</p>
<ul>
<li>get a list of all allowed toolbar items</li>
<li>get a list of all default items which should be displayed in our default toolbar</li>
<li>get a <code>NSToolbarItem</code> object for a given identifier</li>
</ul>
<p>Again, similar to the toolbar, the toolbar items are also identified with identifier strings. It is a good practice to define these strings as static variables:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>SaveToolbarItemIdentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;My Save Toolbar Item&quot;</span>;
<span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>SearchToolbarItemIdentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;My Search Toolbar Item&quot;</span>;</pre></div></div>

<p>The Cocoa framework has a couple of toolbar items predefined, for example <code>NSToolbarSeparatorItemIdentifier</code> or <code>NSToolbarSpaceItemIdentifier</code>.</p>
<p>The first delegate method we implement will return a list of all allowed toolbar items:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> toolbarAllowedItemIdentifiers<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSToolbar</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> toolbar <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span>SaveToolbarItemIdentifier, 
        SearchToolbarItemIdentifier,
        NSToolbarFlexibleSpaceItemIdentifier, 
        NSToolbarSpaceItemIdentifier, 
        NSToolbarSeparatorItemIdentifier, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>We simply need to return an array with the identifiers of all allowed toolbar items.</p>
<p>The delegate to return a list of default toolbar items looks similar:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> toolbarDefaultItemIdentifiers<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSToolbar</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>toolbar 
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span>SaveToolbarItemIdentifier,
        NSToolbarFlexibleSpaceItemIdentifier, 
        SearchToolbarItemIdentifier, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Our sample toolbar consists of two items, which are separated with a flexible space element which results in the first item being left aligned in the toolbar and the second item being right aligned. The order of the items returned in the array is the display order in the toolbar.</p>
<p>The interesting part is how the toolbar items are created. The toolbar can display two types of items. Simple icon based toolbar items and items which are based on a custom view. With custom view items you can implement search fields, or popup menus or anything else you can imagine. In our example we have a search field which is a custom view item. For this item we have created a new custom view in interface builder and placed an NSSearchField into the view. Our controller has an outlet which is connected to the custom view. The resulting delegate method looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSToolbarItem</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>toolbar<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSToolbar</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>toolbar
     itemForItemIdentifier<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>itemIdentifier
 willBeInsertedIntoToolbar<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>flag
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSToolbarItem</span> <span style="color: #002200;">*</span>toolbarItem <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>itemIdentifier isEqualTo<span style="color: #002200;">:</span>SaveToolbarItemIdentifier<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        toolbarItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSToolbarItem</span> alloc<span style="color: #002200;">&#93;</span> initWithItemIdentifier<span style="color: #002200;">:</span>itemIdentifier<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>toolbarItem setLabel<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Save&quot;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>toolbarItem setPaletteLabel<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>toolbarItem label<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>toolbarItem setToolTip<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Save Your Passwords&quot;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>toolbarItem setImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSImage</span> imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;save.png&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>toolbarItem setTarget<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>toolbarItem setAction<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>save<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>itemIdentifier isEqualTo<span style="color: #002200;">:</span>SearchToolbarItemIdentifier<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">NSRect</span> searchRect <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>searchView frame<span style="color: #002200;">&#93;</span>;
        toolbarItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSToolbarItem</span> alloc<span style="color: #002200;">&#93;</span> initWithItemIdentifier<span style="color: #002200;">:</span>itemIdentifier<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>toolbarItem setLabel<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Search Passwords&quot;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>toolbarItem setPaletteLabel<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>toolbarItem label<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>toolbarItem setToolTip<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Search Password Titles&quot;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>toolbarItem setView<span style="color: #002200;">:</span>searchView<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>toolbarItem setMinSize<span style="color: #002200;">:</span>searchRect.size<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>toolbarItem setMaxSize<span style="color: #002200;">:</span>searchRect.size<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>toolbarItem autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>For every toolbar item you need to the set its label, a palette label and a tooltip message. For simple toolbar items, you also need to define an icon image and an action method which will be called when the item is clicked.</p>
<p>The setup of custom view items is a little bit different. First we read the size of the custom view. This is used to set a fixed size for the toolbar item. We do not specify a custom action, because the <code>NSSearchField</code> is connected to an action method itself. But whether you need to define an action method for a custom view item, depends on your view.</p>
<p>That&#8217;s it for the basics of toolbar programming in Cocoa. If you want to dive deeper into this topic, I recommend to read the document <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/Toolbars/index.html">Toolbar Programming Topics for Cocoa</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dikant.de/2007/09/05/nstoolbar-basics/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Cocoa memory management 101</title>
		<link>http://www.dikant.de/2007/08/23/cocoa-memory-management-101/</link>
		<comments>http://www.dikant.de/2007/08/23/cocoa-memory-management-101/#comments</comments>
		<pubDate>Thu, 23 Aug 2007 10:00:07 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.dikant.de/2007/08/23/cocoa-memory-management-101/</guid>
		<description><![CDATA[Ok, so here it comes, my first Cocoa posting. I am pretty new to Cocoa and therefore this might be something you are more than familiar with, but for me it is handy to have a quick reference written down &#8230; <a href="http://www.dikant.de/2007/08/23/cocoa-memory-management-101/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Ok, so here it comes, my first Cocoa posting. I am pretty new to Cocoa and therefore this might be something you are more than familiar with, but for me it is handy to have a quick reference written down where I can look up things. So today I would like to talk about memory management. My coding background lies about 70% in Java, 20% in PHP and 10% in C/C++. So in the past mostly I did not have to worry about memory management topics, although I am familiar with the low level C/C++ way of allocating and releasing memory. Cocoa uses an approach somehow in the middle between automatic garbage collection and manual management. Once you get used to it, it is a very powerful tool and you have a much better control over the memory consumption in your applications than in Java while being less error prone than the C/C++ mechanism.</p>
<p>If you want to dive into the details of Cocoa memory management, I recommend you read the <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/index.html">Memory management Programming Guide For Cocoa</a> from Apple. For me it was the main source of information about this topic.</p>
<p><span id="more-27"></span></p>
<p><strong>The retain cycle</strong></p>
<p>Cocoa objects use an Attribute called <code>retainCount</code> which keeps track how many other objects claim ownership to it. Whenever you want to keep a reference to an object you send it the <code>retain</code> message, which will increase its <code>retainCount</code>. When you don&#8217;t need the reference any more you send a <code>release</code> message and the count will decrease. Once the <code>retainCount</code> drops to 0, the object will be disposed and the memory freed.</p>
<p>The most important rule you have to remember is:</p>
<blockquote><p>Whenever you create an object via methods that contain <code>alloc</code>, <code>new</code> and <code>copy</code> in their name, or you increase the retainCount via <code>retain</code> you need to <code>release</code> that object in the same context.</p></blockquote>
<p>For example let&#8217;s take a look at the following method:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>doSomething
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">id</span> <span style="color: #002200;">*</span>myObject <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SomeObject alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// do something with the object</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>myObject release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>We are creating a method variable pointing to a freshly created object. The variable will be disposed when we exit the method, therefore we need to make sure to release the object it is pointing to before exiting the method.</p>
<p>The same principal applies to instance variables of a class. Here you need to make sure that you release all the instance variables this class might have created, before disposing the class object. This is usually done in the <code>dealloc</code> method of the class.</p>
<p>When working with instance variables you should follow the best practice to access the variables only through their accessor methods. That way you can ensure a transparent handling of the release and retain operations. Let&#8217;s take a look at a typical setter method:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aName
<span style="color: #002200;">&#123;</span>
    aName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>aName copy<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>name release<span style="color: #002200;">&#93;</span>;
    name <span style="color: #002200;">=</span> aName;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>So the setter first creates a new copy of the object, then releases the old object and last it will store the reference to the new object in the instance variable. The order of the operations is especially importing. Because if we would first release the old object, and the new object would be a pointer to the old object, we would loose our data.</p>
<p>Now that the setter always allocates a new object, we need to make sure to release it when the class is destroyed:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>name release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>We don&#8217;t have to worry about the <code>name</code> object being <code>nil</code>, because Objective-C can send messages to <code>nil</code>-objects without generating null-Pointer exceptions or crashing.</p>
<p><strong>Autorelease objects</strong></p>
<p>So naturally the next question is: How do we return objects we have created inside a method when we have to release them before we exit the method?</p>
<p>This is the domain of the autorelease objects. Instead of sending an object the <code>release</code> message, you could send it the <code>autorelease</code> message, which will mark that object for <em>later</em> deletion. So, the retainCount will be reduced, but the object will not be disposed and we can use it in the code that called our method.</p>
<p>So how does this work? Basically it adds the object to a so called autorelease pool. This pool is initialized automatically every time an event cycle is triggered (for example via a MouseDown event). During the event cycle, all autorelease objects will be added to that pool and they will be disposed once the event cycle has finished. This means that you don&#8217;t have to worry about retaining an autorelease object as long as you use it only during a single user action. You only need to retain it if you want to store it between user actions.</p>
<p>If you stick to the convention to access your instance variables via the getter and setter methods, you do not need to worry about working with autoreleased objects, because the setter methods would automatically retain the object. That&#8217;s a best practice which really helps keep your code clear.</p>
<p>Besides the automatic autorelease pools, you can also create your own pool by creating a new instance of the object <code>NSAutoreleasePool</code>. The creation of your own autoreleass pool is mandatory in the following cases:</p>
<ul>
<li>You are writing a command line tool</li>
<li>You are spawning a new thread</li>
</ul>
<p>There is another use for custom pools. When you are working with a lot of autorelease objects inside a loop statement, this will lead to all those objects being stored in the pool until the event loop finishes. It is much more efficient to release the memory at the end of each loop cycle. In that case we would create a autorelease pool inside the loop:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span>i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i &lt; <span style="color: #2400d9;">1000</span>; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    NSAutoReleasePool <span style="color: #002200;">*</span>myPool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>NSAutoReleasePool alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>someVariable <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Some text&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// do some more stuff with lots of autorelease objects</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>myPool release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>In the above example, all the autorelease objects which have been created inside the loop, will be disposed of as soon as we release the pool object. This is a very powerful concept to keep your memory consumption low. </p>
<p><strong>Note on collections</strong></p>
<p>Collection objects retain the objects which are stored in the collection. You can release them once you added them to the collection, but keep in mind, that once the collection object is released, it will also release all objects stored in that collection.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dikant.de/2007/08/23/cocoa-memory-management-101/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
