Basic Cocoa with bindings question

Discussion in 'Mac Programming' started by rebo, Aug 3, 2008.

  1. rebo macrumors newbie

    Joined:
    Mar 5, 2008
    #1
    Just confused here is there any reason why


    _properties = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys];


    Causes a crash, where


    _properties = [[NSMutableDictionary alloc] initWithObjects:values forKeys:keys];


    works fine in an application with bindings?

    Thanks, I'm sure this is a very simple thing i'm missing!
     
  2. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    The first one is autoreleased, so if you don't retain it after creating it, it'll be invalid sometime after method you created it in goes out of scope (you can't tell exactly when, it's whenever the event loop cycles and the runtime drains the autorelease pool). When bindings tries to use it, crash, because it no longer exists.

    The second one has a retain count of one because you used alloc, and will not be automatically released. Hence, no crash. You should be sending this object a release at some point in the future (probably in your dealloc method).

    You didn't say if you're using garbage collection memory management or not...
     
  3. rebo thread starter macrumors newbie

    Joined:
    Mar 5, 2008
    #3
    Thank you very much HiRez, that explains it!

    No not using Garbage collection memory management right now, still pretty new at cocoa programming!
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    I guess I'll be the one to be a stickler and say that this is an Objective-C memory management issue, not really a Cocoa issue. There is a Cocoa convention at work in terms of the naming of methods, but autorelease pool issues, retain/release issues etc. are all Objective-C language features.

    -Lee
     

Share This Page