Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone ($4.99)

dotnetIQ ($4.99)

Your First iPhone App ($1.99)

iPocket Tools 9 in 1 ($0.99)

Catch-Me (Free)

Alien Strike ($0.99)

Historic Olympic Medal-Table ($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-09-2009, 10:49 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 96
Default Problems with "global" variable scope

Hey All!
I have declared a variable in the .h file and I want be able to access it from anywhere in the .m files. I am not sure if this is considered a "global" variable or not. In my code, I initialize the variable in the loadView function, and then access it in another function. The call in the second function is giving me errors:
.h file
Code:
@interface SearchParameterViewController : UIViewController <UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource> {
	NSMutableArray *titles;
	NSMutableArray *subtitles;
}
.m files
Code:
- (void)loadView {
  titles = [NSMutableArray arrayWithObjects:@"Title1", @"Title2", nil];
  NSLog(@"Length_a = %d", [titles count]);
}
...
- (NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component{
  NSLog(@"Length_b = %d", [category_titles count]);
}
The output is:
Length_a = 2
>>> CRASH <<<<

Any ideas why this is happening?
blueFLoyd8 is offline   Reply With Quote
Old 07-10-2009, 01:01 AM   #2 (permalink)
Registered Member
 
Join Date: May 2008
Posts: 20
Default you must init object

titles = [NSMutableArray arrayWithObjects:@"Title1", @"Title2", nil]; <-- it's autorelease your object. you should use init before add any objects.
odbc2000 is offline   Reply With Quote
Old 07-10-2009, 09:16 AM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 61
Posts: 463
Default

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
RLScott is offline   Reply With Quote
Old 07-10-2009, 01:43 PM   #4 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 96
Default

Thanks for the advice. I allocated them and it works fine. Then, I release them in the class' dealloc() function
blueFLoyd8 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


» Advertisements
» Stats
Members: 21,505
Threads: 35,786
Posts: 156,783
Top Poster: smasher (2,449)
Welcome to our newest member, SimonK
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:13 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0