Local declaration of "" hides instance variable

Discussion in 'iOS Development' started by Chicken, Jan 20, 2009.

  1. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    my app works fine with these errors just wondering if there is a way to solve it
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    You have something similar to this in your implementation file

    NSObject *someObject = [[[NSObject alloc] initWithBlah] autorelease];

    Make it
    someObject = [[[NSObject alloc] initWithBlah] autorelease];
  3. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Ah thanks alot skylar your great
  4. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    Thank you Skylar, helped me too

    Please Register or Log in to view images

    .
  5. bddckr

    bddckr Active Member

    Joined:
    Dec 2, 2007
    Messages:
    1,434
    Likes Received:
    18
    Device:
    iPhone 4 (Black)
    I love the 'initWithBlah'

    Please Register or Log in to view images


    And I hope you guys (Skylar does it for sure...

    Please Register or Log in to view images

    ) know about Memory Management, the autorelease is fine, but not always the best choice.
    (The code Skylar provided is ok but you should remember that releasing stuff at the right time is very important, especially on a portable platform. Just thought someone should mention this.)
  6. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    That's a great point. If I were actually writing what I posted here into a real app, I would not use autorelease.

    To tell the truth, I don't like to use autorelease all that much. Whenever possible, I use convenience constructors (which will autorelease themselves when they go out of scope), or I manually release the objects I create.

    Example: [[UIImage imageNamed

    Please Register or Log in to view images

    "image.png"] autorelease]; will give you an error, most likely a double free error. This is because the object gets released automatically as a part of its nature, and the app tries to release it.

    Example: NSString aString = [NSString stringWithFormat

    Please Register or Log in to view images

    "Variable\t%d", variable]; /*more lines of code*/[aString release]; will also cause you an error. Again, most likely a double free error.

    Anyone reading this, what I posted incorrectly in my last post was autoreleasing an object that was assumed to be an ivar. Not that there's anything wrong with this, but you probably should have just made the variable local instead.



    EDIY: If you guys are having issues with memory management, go ahead and set NSZombieEnabeled. It will keep objects alive in memory when released, and log out a message when they are freed or accessed again. This is a very important tool if you are having memory issues. Just, make sure to turn it off before you release your app.
  7. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    I don't like using auto release IMO its easier to just release it when you need to like skylar said you can get quite a few errors. I've has errors like the ones skylar mentioned in previous apps.

Share This Page