Basic Cocoa with bindings question

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

  1. rebo macrumors newbie

    Mar 5, 2008
    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


    Jan 6, 2004
    Western US
    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

    Mar 5, 2008
    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


    Jan 10, 2005
    Dallas, TX
    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.


Share This Page