As adbc2000 said, the creation of
titles produces an autoreleased object. That means the block of memory that holds that object will be released and the object will cease to exist when the function returns if no one does a
retain on that object.
You probably don't need a global object in your application, if you think about it more carefully. Often a programmer will think he needs a global object just because he does not want to take the time to think of a way to use Objective-C memory management properly. Using global objects opens the door to all sorts of opportunities for using dangling pointers, and requres extra effort on your part to analyze object lifetimes and ownership. Using Objective-C properly will take some of that burden away from you. So if at all possible, try to play by the rules of Objective-C memory management and make every object owned by some other object.
Now, if that didn't dissuade you from using global objects, here is how you can do it:
In your .H file, define a pointer to an object outside of the @interface...@end block, such as:
Code:
extern NSMutableArray *global_titles;
@interface....
...
@end
Then declare the pointer outside of the @implementation...@end block in some .M file, as in:
Code:
NSMutableArray *global_titles;
@implementation...
...
@end
Now all that does is allocate some static memory for the pointer to the object. It does not create the object itself or allocate memory for it. That you must do inside of some Objective-C method. But here's the rub: Since you are circumventing the usual protections afforded by the Objective-C retain counting system, you must take the responsibility for making sure this object is created once and only once, and that it is created before any method tries to use it. You could tie it to another object that you know will only have one instance for the lifetime of the application, such as the App Delegate, or in your case, the View Controller, assuming there is only one main View Controller, and it only gets created once. But remember, a low memory warning from the system can arrive at any time, and if your view is not visible at the time, it may be released, and re-created when needed again, so a method like ViewDidAppear would not be a good one in which to create your global object. Whereever you do decide to create it, once and only once, do something like this:
Code:
global_titles = [NSMutableArray arrayWithObjects:@"Title1", @"Title2", nil];
[global_titles retain];
Doing a
retain will ensure that this object will not be released when the function returns. You could do a matching
release when your application exits, but it is not really necessary because the system will reclaim all memory allocated by your application at that time.
Now, wth all that done, you can access global_titles from anywhere in your code as long as the .H file that defines it is included. Remember, you don't have a setter method defined for this object, so if you change the object, make sure you do the equivalent of what a retaining setter would do:
Code:
MSMutableArray *newArray = ....some autoreleasing creating function...
[global_titles release];
global_titles = newArray;
[global_titles retain];
See how much automatic protection you are giving up by using a global object?
Robert Scott
Ypsilanti, Michigan