On Mon, May 11, 2009 at 7:15 PM, jon <trambl...@mac.com> wrote: > from my very limited Objective-C programing experience of all of 10 days... > > it appears to me that my assignments to NSStrings seem to, at random, > disappear..... (i, being a new Objective-C programmer coming from pascal and > C, like to have a "global" string in several places, not that it is > correct or anything, but i still would like to know what is going on when i > do keep a string around for a good long time in the application) (or any > other object for that matter) > > I attribute it so far to my lack of understanding garbage collection and > retaining objects...
There are two types of memory management with Objective-C on the Mac: 1. Retain/release Under retain/release, every object has a reference count. -retain increments this count, and -release decrements it. If the reference count reaches zero, the object is deallocated. 2. Garbage Collection Under garbage collection, the runtime keeps track of pointers in the system that can keep an object alive. If any of these pointers point to an object, then it will be kept alive. If any of these objects, in turn, have pointers to other objects, those will be kept alive as well, and so on. If an object no longer has any of these pointers pointing to it, then the garbage collector will collect and finalize it. For the most part, unless you're writing framework code that can be called from apps using both memory management models, you should pick one and stick with it. > my wild guess right now is to do this below when ever i have the "NSString" > instance assignment to prevent, for instance, "theTitle" from "randomly > disappearing"... > > ok, tell me how badly this will go wrong... (although this would be more > like "theTitle" is declared in an area that is global, and then assigned > later.) I'll assume that you're talking about the retain/release style of memory management for now. It would be best to read <http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html>, and then ask specific questions about what you still don't understand. Essentially, if you don't have an outstanding reference on the object, it can and will go away when all of the other references on it are released. > I do know that "NSSTring" is "different" than other objects, but i'm not > sure how, they do appear to act like other objects though as far as i can > tell. Then forget what you know :) . The memory management rules apply to *all* objects, follow them, and all will be well. In places where some classes (such as NSString) implement special behavior, the do so it a way that nobody who follows the rules should ever notice. > NSString *theTitle = [[defaults stringForKey:@"the title"] retain]; In this case, you are correct in retaining the object, as that is the only to ensure that it remains valid for as long as you need it. You also must make sure that, when you are ready to have theTitle point to another object or nil, you release the old value of theTitle. -- Clark S. Cox III clarkc...@gmail.com _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com