Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
I was on the App Developers forum figuring out what I need to fill out to send in my first app and I saw someone was having problems with code crashing. In his post I saw this code

Code:
-(void)drawRect:(CGRect)rect
{
    UIImage *navImage = [UIImage imageNamed: @"top bar image.png"];
    [navImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [navImage release];
    [self release];
}

Right off the bat I knew [Self release]; is bad. But as I looked at it I saw that he created a method with no return, fine. Next he creates a UIImage object and assigns a png to it. Next step he makes a Rect for the image but then after all that it looks like he releases his navImage without doing anything with it, like add to subview, Dictionary or something.

Am I wrong in understanding it?
 
He is creating an image pointer which uses a convencience method, so this will crash, because his pointer will be auto-released. And will therefore crash when you try to release again. (release sent to deallocated instance). hope that helps.

PS: he is not creating a rect just for nothing, his method is DRAWinrect, means he will draw the image inside the rect, otherwise it'd just be initWithFrame, or CGRectMake, or whatever.
 
Thank you for the clarification. I have not explored DrawInRect yet. I knew Self Release looked strange but then I thought he was releasing before doing anything. I see what is happening now with drawInRect

Thanks!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.