Skip to content

Commit

Permalink
fixed a memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
jivadevoe committed Jan 20, 2011
1 parent 2a623be commit f54f2aa
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions NSTimer+Blocks.m
Expand Up @@ -11,12 +11,18 @@ @implementation NSTimer (Blocks)

+(id)scheduledTimerWithTimeInterval:(NSTimeInterval)inTimeInterval block:(void (^)())inBlock repeats:(BOOL)inRepeats
{
return [self scheduledTimerWithTimeInterval:inTimeInterval target:self selector:@selector(jdExecuteSimpleBlock:) userInfo:[inBlock copy] repeats:inRepeats];
void (^block)() = [inBlock copy];
id ret = [self scheduledTimerWithTimeInterval:inTimeInterval target:self selector:@selector(jdExecuteSimpleBlock:) userInfo:block repeats:inRepeats];
[block release];
return ret;
}

+(id)timerWithTimeInterval:(NSTimeInterval)inTimeInterval block:(void (^)())inBlock repeats:(BOOL)inRepeats
{
return [self timerWithTimeInterval:inTimeInterval target:self selector:@selector(jdExecuteSimpleBlock:) userInfo:[inBlock copy] repeats:inRepeats];
void (^block)() = [inBlock copy];
id ret = [self timerWithTimeInterval:inTimeInterval target:self selector:@selector(jdExecuteSimpleBlock:) userInfo:block repeats:inRepeats];
[block release];
return ret;
}

+(void)jdExecuteSimpleBlock:(NSTimer *)inTimer;
Expand Down

4 comments on commit f54f2aa

@Air-Craft
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still necessary do you think with ARC enabled?

@jivadevoe
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think copying the block is still required to move the objects captured by the block out of the stack and onto the heap. Naturally, the release of the copied block would be subsequently done by ARC.

Whether the resultant code could be simplified back to the original version or not... I think that it probably could. ARC should "do the right thing" here WRT the copied block.

@Air-Craft
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that blocks are objects (http://thirdcog.eu/pwcblocks/#objcblocks) and therefore the only thing that's on the stack is the pointer to the object itself, thus there's no more need to copy it than there is any other Objective-C object. Either way it doesn't hurt to copy it... Thanks for the code by the way :)

@zakkhoyt
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Very useful.

Please sign in to comment.