4

I have a variable lastPostsGrabbedCounter, an NSNumber, that is defined below.

.h
NSNumber *lastPostsGrabbedCounter;
@property (nonatomic, retain) NSNumber *lastPostsGrabbedCounter;

.m
@synthesize postDetailViewController, lastPostsGrabbedCounter;

- (void)viewWillAppear:(BOOL)animated {
    self.lastKnownLocation = [[CLLocation alloc] init];
    self.lastPostsGrabbedCounter = [[NSNumber alloc] initWithInt:25];
 [self showActivityViewer];

}

This .m file is a table controller in my main view. When the app is loaded this viewWillAppear gets called, but if I navigate to another TAB and come back and I try to use the lastPostsGrabbedCounter var, I it shows it as nil?

Why isn't it retained when I navigate away?

atxe
  • 5,029
  • 2
  • 36
  • 50
jdog
  • 10,351
  • 29
  • 90
  • 165
  • 1
    Can you show the source for the whole class? Also, you're leaking this number by using [[NSNumber alloc] initWithInt:25]. Your property is already retaining the number, so you should use [NSNumber numberWithInt:25] instead, which is autoreleased and gets a single retain from the property. – André Morujão Apr 03 '11 at 17:26
  • How do you know it doesn't get retained? If you're checking using `-retainCount`, the answer is that small `NSNumber`s are cached to avoid creating new objects when it's not necessary. You can read something about it in this other question: http://stackoverflow.com/questions/656902/nsnumber-retain-count-issue – Georg Schölly Apr 03 '11 at 20:39

1 Answers1

4

If the NSNumber was not being retained, then your app would (most likely) crash or, at least, behave badly. That you are seeing a nil indicates that there is a very different problem occurring.

Check to make sure you aren't nilling it out on some other code path. Also, make sure that you are coming back to the instance you think you are. A handful of NSLog(@"%@ %p", [self class], self]); spread throughout the methods can be very helpful.

And as André said, you are leaking the number; over-retaining it.

bbum
  • 162,346
  • 23
  • 271
  • 359
  • Everytime I leave the tab mentioned above and come back when the viewWillAppear method runs if I set a breakpoint I the lastPostsGrabbedCounter even after being set shows "invalid summary"? – jdog Apr 03 '11 at 20:18
  • Try printing the object directly `po astPostsGrabbedCounter`. That code is dead simple, there must be something else broken. – bbum Apr 03 '11 at 20:33
  • Ok so trying everything added in the comments above I now get "Arithmetic on pointer to interface 'NSNumber', which is not a constant size in non-fragile ABI" on this line self.lastPostsGrabbedCounter = self.lastPostsGrabbedCounter + 25; And a Thread 1: Program received signal: "EXC_BAD_ACCESS" on this line @synthesize postDetailViewController, lastPostsGrabbedCounter; – jdog Apr 04 '11 at 00:49
  • That line of code makes no sense. An NSNumber is not a scalar type that can be used with regular old C operations. It is an object and needs to be treated as such. If there are *any* compiler warnings, fix 'em (or post more code/warnings). – bbum Apr 04 '11 at 01:59