James Dunay
James Dunay

Reputation: 2724

Trying to figure out if this is a memory leak: Xcode

I have the following method and would like to know if I were to run this a hundred times would this be a memory leak.

-(void)displayPointsOnBoard:(int)wordPoints atCoor:(NSNumber*)coor{

    NSLog(@"%i", wordPoints); 

    CGPoint p = [[points objectForKey:[coor stringValue]] CGPointValue];
    UIImageView* pointDisplay = [[UIImageView alloc] initWithFrame:CGRectMake((p.x - 15), (p.y - 35), 76.f, 40.f)];
    pointDisplay.backgroundColor = [UIColor clearColor];
    pointDisplay.image = [UIImage imageNamed:@"point_display_box.png"];
    [self addSubview:pointDisplay];

    [UIView beginAnimations:@"" context:NULL]; // Begin animation
    [UIView setAnimationDuration:.4];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    pointDisplay.frame = CGRectOffset(pointDisplay.frame, 0.f, -35.f);
    [UIView commitAnimations]; // End animations

    [UIView beginAnimations:@"" context:NULL]; // Begin animation
    [UIView setAnimationDuration:.3];
    [UIView setAnimationDelay:.3];
    pointDisplay.alpha = 0;
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDidStopSelector:@selector(removeDisplay:)];
    [UIView commitAnimations]; // End animations

    [pointDisplay release];
}

I release pointDisplay, but I still have it in my view (even though its alpha is 0.0). And I'm wondering if this might cause problems. Any help would be awesome! Thanks!

Upvotes: 0

Views: 200

Answers (4)

ader
ader

Reputation: 5393

IF you copy, alloc or new once then you only release once.

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html

Upvotes: 0

jbat100
jbat100

Reputation: 16827

[pointDisplay release];

Counteracts

UIImageView* pointDisplay = [[UIImageView alloc] initWithFrame:CGRectMake((p.x - 15), (p.y - 35), 76.f, 40.f)];

Then adding the subview

[self addSubview:pointDisplay];

has increased the retain count of pointDisplay; the corresponding release will be sent to pointDisplay automatically when it is removed from its superview or when self is deallocated (UIView releases it's subviews automatically in its dealloc). The fact that it has 0.0 alpha, or is hidden doesn't make any difference.

Upvotes: 1

bitmapdata.com
bitmapdata.com

Reputation: 9600

if you want get rid of pointdisplay from you're view, below refer code

[pointDisplay removeFromSuperView];

Upvotes: 1

Daniel
Daniel

Reputation: 22395

This wouldnt cause a memory leak, the memory management looks ok on it... if you would call this 100 times, your memory would keep increasing, but this wouldnt be considered a leak, because if you release the view you are adding on, the UIImageViews will be released as well... A memory leak occurs when you allocate some object or data and then have no way to release is later...for example

-(void)blah
{
   NSString *s=[[NSString alloc] init];

}

the above will cause a memory leak because the method creates a string with a +1 ref count, when the method exits you no longer have a reference to the string, therefore you wont be able to release it and the system will not reclaim the memory allocated for the string..in your example, the UIView you add on retains the UIImageView, but when you release the UIView all the UIImageViews on it will be released as well.

Hope this helps

Upvotes: 2

Related Questions