System crashing when referencing array

Discussion in 'iOS Development' started by cybohemia, Apr 30, 2009.

  1. cybohemia

    cybohemia New Member

    Joined:
    Jan 25, 2009
    Messages:
    49
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    I created a simple tree data structure class that includes a member called children, which is an array of pointers to objects of the same class.

    Weird thing is, after checking that children is well defined in viewDidLoad, when I check it again (with nothing explicitly done in between) in viewWillAppear, the system crashes because children is no longer defined.

    Here are some code snippets that may clarify what I'm trying to describe:
    Node.m:
    Code:
    -(id) init {
      children = [NSMutable array];
      return self;
    }
    RootViewController.m:
    Code:
    -(void)viewDidLoad {
      [super viewDidLoad];
      node = [[Node alloc] init];
      NSLog(@"This is OK: node is %@", node);
      NSLog(@"This is OK: Node.children count is %d", [node.children count]);
    }
    
    -(void)viewWillAppear:(BOOL)animated {
      [super viewWillAppear:animated];
      NSLog(@"This is OK: node is %@", node);
      NSLog(@"This line crashes: Node.children count is %d", [node.children count]);
    }
    (By the way, node is a member of the RootViewController object and children has the retain property set.)

    Am I missing something basic...?

    Please Register or Log in to view images

    Any help is greatly appreciated!
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    children = [NSMutable array];
  3. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    not [NSMutableArray array]?
  4. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    He was pointing out the mistake.
  5. cybohemia

    cybohemia New Member

    Joined:
    Jan 25, 2009
    Messages:
    49
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    Opps - that was a typo: I was using [NSMutableArray array] ...guess I should've cut-n-pasted

    Please Register or Log in to view images



    I'm attaching a small zip of a test app I did that illustrates the issue: a Node class just has a children field, which is an array and the RootViewController allocates it in the viewDidLoad, printing out the defined children member, and then tries to print it out in viewWillAppear (but fails).

    Any help totally appreciated!

    Attached Files:

  6. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    It doesn't matter, it's still wrong. I don't even need to look at your app to see that. What you provided in the first post was sufficient.
  7. cybohemia

    cybohemia New Member

    Joined:
    Jan 25, 2009
    Messages:
    49
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    If I understood correctly, your reply to the first posting pointed out the typo error; if it's still wrong, can you tell me what in particular is wrong so I can fix it? Thanks!
  8. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Sigh... Seriously, it makes me very sad that I have to literally spell this out for you.

    : (


    You are using an autoreleased version of NSMutableArray which is getting released. Switch it to [NSMutableArray alloc] init] and release it in your dealloc.
  9. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    Not that this is the first of your problems, but I'm not thinking that implementation of a binary tree is very fast.
  10. cybohemia

    cybohemia New Member

    Joined:
    Jan 25, 2009
    Messages:
    49
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    I guess picking up iPhone programming by grabbing snippets here and there can get one (me, at least) in trouble. I got this code from slide 50 of the Stanford Notes on Objective-C.

    ...and I'm embarrassed that you have to spell it out for me

    Please Register or Log in to view images

    but thanks for doing so

    Please Register or Log in to view images

Share This Page