tag:blogger.com,1999:blog-5262416307089128677.post6060311062942351486..comments2011-03-23T14:51:10.121-04:00Comments on Gents with Beards: Simple audio recording and playbackMarchttp://www.blogger.com/profile/17118958970216180780noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-5262416307089128677.post-61620311487766601702010-03-26T16:38:27.846-04:002010-03-26T16:38:27.846-04:00Awesome work Marc!!! Thanks.Awesome work Marc!!! Thanks.vibhor goyalhttps://www.blogger.com/profile/10133512638902067812noreply@blogger.comtag:blogger.com,1999:blog-5262416307089128677.post-59881543932669301162009-06-22T01:42:23.027-04:002009-06-22T01:42:23.027-04:00The alloc/init and retain/release cycles are key t...The alloc/init and retain/release cycles are key to understanding idiomatic Objective-C memory management. Unfortunately, they also differ enough from most other environments that it's quite confusing at first. In summary: it's more manual boilerplate than C++ smart pointers, but pretty consistent across the board.<br /><br />First, take a quick look at the documentation for <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/init" rel="nofollow">-[NSObject init]</a>. <br /><br />I want to highlight that init operates on an object (the receiver of the init message), but it also returns an object. The object returned by init may not be the same object that handled the init message. In other words, [SomeClass alloc] and [[SomeClass alloc] init] might be different objects. Why? It can used to share instances rather than always creating new objects (similar to Java's <a href="http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#valueOf(int)" rel="nofollow">Integer.valueOf()</a>). It's also used to implement <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW34" rel="nofollow">class clusters</a>.<br /><br />As a result, you are encouraged to always call alloc and init in the same, nested expression (i.e. [[Class alloc] init] ). Never keep a reference around to the object that alloc returned. It may be OK with some classes (probably most classes), but it's not a good habit to get into. <br /><br />I agree that re-initting the same object might well lead to resource leaks and other bad stuff. The same is true in any environment. You may consider making the AVAudioPlayer a local variable inside the play method. I don't know whether -[AVAudioPlayer play] blocks or not. If it does not, it would be hard to know when to release instances.Danhttps://www.blogger.com/profile/06099373265709774874noreply@blogger.com