Odd looking code

Discussion in 'iOS Programming' started by larswik, Oct 17, 2011.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    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?
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    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.
     
  3. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    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!
     

Share This Page