• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: blocks and autorelease weirdness
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: blocks and autorelease weirdness


  • Subject: Re: blocks and autorelease weirdness
  • From: Julien Jalon <email@hidden>
  • Date: Mon, 19 Apr 2010 12:38:20 +0200

I have to add that using dispatch_async on main queue within an NSApp will
automatically drain the autorelease pool.

AppKit drains the autorelease pool upon NSEvents dispatch.

On Mon, Apr 19, 2010 at 12:36 PM, Julien Jalon <email@hidden> wrote:

> I don't see anything wrong with the output being 1/2:
>
> -(void) test
> {
>        testString = NULL;
>
>        dispatch_async(dispatch_get_global_queue(0, 0), ^{
>                dispatch_async(dispatch_get_main_queue(), ^{
>
>                        NSString* aString = [[NSMutableString alloc] init];
> * // retain count is 1*
>
>                        NSLog(@"retainCount: %d",[aString retainCount]);
>
>                        testString = [aString retain]; *// retain count is
> 2*
>
>                        [aString *auto*release]; *// retain count is still
> 2 - object will be released sometimes in the future*
>
>
>                        dispatch_async(dispatch_get_main_queue(), ^{
>                                NSLog(@"retainCount: %d",[testString
> retainCount]);* // you can't assume the future release has already been
> called*
>                        });
>                });
>        });
> }
>
> On Mon, Apr 19, 2010 at 11:35 AM, Henk Kampman <
> email@hidden> wrote:
>
>> Have a look at the following code
>>
>> -(void) test
>> {
>>        testString = NULL;
>>
>>        dispatch_async(dispatch_get_global_queue(0, 0), ^{
>>                dispatch_async(dispatch_get_main_queue(), ^{
>>
>>                        NSString* aString = [[NSMutableString alloc] init];
>>                        NSLog(@"retainCount: %d",[aString retainCount]);
>>
>>                        testString = [aString retain];
>>
>>                        [aString release];
>>
>>                        dispatch_async(dispatch_get_main_queue(), ^{
>>                                NSLog(@"retainCount: %d",[testString
>> retainCount]);
>>                        });
>>                });
>>        });
>> }
>>
>> As expected the output is:
>>
>> retainCount: 1
>> retainCount: 1
>>
>> However when I change [aString release] to [aString autorelease] the
>> output shows:
>>
>> retainCount: 1
>> retainCount: 2
>>
>> Why?
>>
>> BTW I first noticed this behavior when my application started leaking the
>> QTMovie objects I was creating in a block.
>>
>> -
>>
>> Henk_______________________________________________
>>
>> Cocoa-dev mailing list (email@hidden)
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
>>
>
>
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: blocks and autorelease weirdness
      • From: Julien Jalon <email@hidden>
References: 
 >blocks and autorelease weirdness (From: Henk Kampman <email@hidden>)
 >Re: blocks and autorelease weirdness (From: Julien Jalon <email@hidden>)

  • Prev by Date: Re: blocks and autorelease weirdness
  • Next by Date: Re: blocks and autorelease weirdness
  • Previous by thread: Re: blocks and autorelease weirdness
  • Next by thread: Re: blocks and autorelease weirdness
  • Index(es):
    • Date
    • Thread