-2

Possible Duplicate:
Understanding reference counting with Cocoa and Objective-C

I am pretty confused in memory management concept of iPhone programming and objective C. It is most useful concept for performance of iPhone Apps. I am want to know that when should I use

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];

and

[obj dealloc]

What is the use of retain? It just increase the retainCount by 1. Its really overhead to me. How retainCount helps to manage memory. Help me out of this.

Community
  • 1
  • 1
MouseCrasher
  • 453
  • 1
  • 5
  • 14
  • Never call `dealloc` on something that is not `super`. And never use `[super dealloc]` outside of `- (void)dealloc`. Oh, and never look at `retainCount`. – Matthias Bauch Nov 07 '12 at 11:52

5 Answers5

1

You should not call dealloc method.... Autorelease pool is used in Threaded environment and some other place too..

One thing is in ios 5+ , you don worry about retain , release all those stuff .. ARC will take care of everything

EXC_BAD_ACCESS
  • 2,699
  • 2
  • 21
  • 25
  • yeah i know that.. but its not good programming approach to ever relay on either ARC or GC. We should have own memory management for best performance of app. – MouseCrasher Nov 07 '12 at 11:59
  • S, Its good to have our own memory management...You just go thru https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html and http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/MemoryMgmt/Articles/MemoryMgmt.html.... THis will give you an idea... and also I think ARC doing good.. I didn't find any problem with that. – EXC_BAD_ACCESS Nov 07 '12 at 12:02
  • 1
    Wut? No, you should not implement your own memory management for "best performance". In fact, if you want the best performance, you should turn on ARC and embrace the system's memory management as much as possible. – bbum Nov 07 '12 at 16:00
1

You have to start with the Memory management basics from Apple.developer.

Read Memory Management

Read Memory Management Policy

Download the doc from here

Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
0

You need to create your own autorelease pool when you are creating a new thread.

Never call [obj dealloc]

For retain, release and other memory concepts please refer : MemoryManagement Please refer these 2 tutorials, it'll help you to understand the concept better:

  1. ios-memory-management-basics
  2. memory-management-in-objective-c-tutorial
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
0

A retain count > 0 will indicate to the autorealease pool that your object is in that the object is still used, still needed by other objects or its owner, etc. When this count reaches zero, that automatically means that the object is no longer needed so it will released. Generally each alloc/init, copy, create or retain must have its equivalent release/autorelease.

Also [obj dealloc], is not a good practice. The recomended use is [obj release] or [obj autorelease].

Vlad
  • 3,346
  • 2
  • 27
  • 39
0

In a reference-counted environment (as opposed to one which uses garbage collection), an NSAutoreleasePool object contains objects that have received an autorelease message and when drained it sends a release message to each of those objects. Thus, sending autorelease instead of release to an object extends the lifetime of that object at least until the pool itself is drained (it may be longer if the object is subsequently retained). An object can be put into the same pool several times, in which case it receives a release message for each time it was put into the pool.

ClassA *a = [[ClassA alloc]init]; here the retain count of a is 1,and a has only one owner. [a retain]; now, the retain count of a is 2 and it has two owners.

Now you need to release the 'a' object twice. Otherwise, it will cause memory leak

RKY
  • 266
  • 1
  • 7