7 Replies - 4427 Views - Last Post: 10 May 2012 - 05:30 PM

#1 soulslicer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-April 12

Confusion over autorelease/init and AutoReleasePools..

Posted 30 April 2012 - 08:23 AM

Okay, just several questions regarding these concepts.

1. What exactly is the difference between creating an object as autorelease and creating it as init. Now I know init means your object will not be released to the autoreleasepool and will have to be manually released. Is this right?

If so when exactly is the release in an autorelease object called. For example, look at this code below
    NSMutableArray *test=[[NSMutableArray alloc]autorelease];
    
    for(int i=0;i<5;i++)
        [test addObject:[NSNumber numberWithInt:i]];

The program just crashes, because it seems the array test has already been released? Then what is the purpose of setting to autorelease??

2. Now for autoreleasepools, I can't understand this at all. I looked over the apple documentation, and I created a similar template of code as such?
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSMutableArray *test=[[NSMutableArray alloc]autorelease];
    
    for(int i=0;i<5;i++)
        [test addObject:[NSNumber numberWithInt:i]];
    
    for(NSNumber *num in test){
        NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init]; 

        NSLog(@"%@ \n",(NSString*)num);
        //[test retain];
        NSLog(@"retainCount for test: %lu", [test retainCount]);
        
        [loopPool drain];
    }

    [pool drain];

I pretty much have no clue what this does. Btw, these are nested autorelease pools. Why do we have them here? Is this how it is implemented?

3. Now onto the topic of retain and retain count. When I create an object, i know it will have a retain count of 1. And i can check this using the [obj retainCount] function. But there seems to be another function called [obj retain], and now the retain count jumps by 1. Why would I want to do that?

Is This A Good Question/Topic? 0
  • +

Replies To: Confusion over autorelease/init and AutoReleasePools..

#2 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: Confusion over autorelease/init and AutoReleasePools..

Posted 30 April 2012 - 02:28 PM

1)
Have you read the description of the autorelease selector or of the NSAutoreleasePool class? It says it adds the object to the current autorelease pool. That means you need to create an autorelease pool.

2)
I have no idea what the apple developers were trying to show in that example and since you have given no context I have no answer for you.

3)
retain/release are an alternative way to do memory management. It's called reference counting. When you create a copy of an object you call retain and when an object's pointer goes out of scope you call release. When an object's reference count gets to 0 it destroys itself.
Was This Post Helpful? 0
  • +
  • -

#3 soulslicer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-April 12

Re: Confusion over autorelease/init and AutoReleasePools..

Posted 01 May 2012 - 12:33 AM

Okay but how does the 1st piece of code there crash?
Was This Post Helpful? 0
  • +
  • -

#4 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: Confusion over autorelease/init and AutoReleasePools..

Posted 01 May 2012 - 03:39 AM

Have you created an autorelease pool to which you can add the NSMutableArray *test? If you haven't then that's probably why the code crashes.
Was This Post Helpful? 0
  • +
  • -

#5 AlbuquerqueApache  Icon User is offline

  • D.I.C Addict

Reputation: 49
  • View blog
  • Posts: 523
  • Joined: 21-February 10

Re: Confusion over autorelease/init and AutoReleasePools..

Posted 02 May 2012 - 07:56 AM

If you have Xcode 4.2, you may want to enable ARC so you don't have to worry as much about manual memory management. I know it is good to know, but in reality, how many modern program languages are requiring manual memory management?

*waits for the flames*

but seriously, how many?
Was This Post Helpful? 0
  • +
  • -

#6 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: Confusion over autorelease/init and AutoReleasePools..

Posted 02 May 2012 - 06:00 PM

I guess it depends on your definition of modern. If we count languages for which there is still a large market then at the very least you have C/C++. Objective C kind of.
Was This Post Helpful? 0
  • +
  • -

#7 soulslicer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-April 12

Re: Confusion over autorelease/init and AutoReleasePools..

Posted 04 May 2012 - 01:50 AM

iOS doesn't support manual memory management.
Was This Post Helpful? 0
  • +
  • -

#8 sahrdan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-May 12

Re: Confusion over autorelease/init and AutoReleasePools..

Posted 10 May 2012 - 05:30 PM

NSMutableArray *test=[[NSMutableArray alloc]autorelease]; 


should be:


NSMutableArray *test=[[[NSMutableArray alloc] init] autorelease]; 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1