How to debug memory allocation issues? - iphone

I am writing an iPhone app that that is trying to create a second a view when the user clicks on an element in UITableView. The code looks like
ReplyToViewController *reply = [[ReplyToViewController alloc] initWithNibName:#"ReplyTo" bundle:nil];
reply.delegate = self;
Message *message = [resultData objectAtIndex:indexPath.row];
int dbid = [message.bizid intValue];
NSLog(#"dbid=%d",dbid);
reply.currentMessage = message;
reply.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:reply animated:YES];
The reply object gets created properly and the view is proper. Last line in above code segment calls some framework code which eventually calls the viewDidLoad method of the ReplyToViewController. Address of the reply object in the above code and the address of the object in viewDidLoad is not same.
Any idea where this new object is coming from? How do I debug? I also added init method the following method in ReplyToViewController hoping that it will get called and I can find who is creating this new object. But it does not stop in this method.
Any help will be greatly appreciated.
- (id) init
{
/* first initialize the base class */
self = [super init];
return self;
}
// Following gets called from the 1st code segment.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
{
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(currentMessage.text]; // THIS returns nil and the program fails later in the code.
}

I'm sure this is unrelated, but I figure this:
NSLog(currentMessage.text];
Should be this:
NSLog(currentMessage.text);
Also, I've found that Analyzing (Cmd+Shift+A) my code always helps to track down potential memory leaks and prevent overzealous memory allocation.

The most likely explanation is a reporting error.
Usually when people see a different address for an object it's because they used the wrong format descriptor in their log statements.
A common error is:
NSLog(#"Object address=%i", myObject); // any numerical formatter %d,%f...
... which produces a random number. You really want:
NSLog(#"Object address=%%qX",&myObject);
... which dumps the address in hex.
Another mistake is:
NSLog(#"Object address=%%qX",&[myObject description]);
... which returns the address of the description string which changes every time.
There are others but you get the idea.
If you're using log statements, check the address in the debugger instead to confirm it's a different object.
Unrelated, but I would get rid of the class' initializer methods because they don't do anything but call the super. You might as well just let the compiler default to the super if you don't customize.

Related

How do i get as NSArray from a block equal to a class data member?

- (void)viewDidLoad
{
[super viewDidLoad];
[VenueManager searchNear:#"Orlando"
onLoad:^(NSArray *objects) {
self.locationObjects = objects;
[self.tableView reloadData];
} onError:^(NSError *error) {
NSLog(#"%#", error);
}];
}
This code is in my viewDidLoad method of my UITableViewController class. It is the starting point for using RestKit to parse a JSON file from FourSquare. I was pulling my hair out because i couldn't get the objects to show up in my Table View until i put [self.tableView reloadData];. With out that call the app never even hit my - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) because after the block was done executing locationObjects would be nil.
Before when I was debugging it the self.locationsObjects = objects worked when i was in the block (i am very unfamiliar with blocks by the way). As soon as i was out of the block the debugger would say locationObjects was nil, even though it had said it had 30 objects just like objects did when i had a break point at the assignment statement.
Can some one help me understand what is going on here.
Additional info:
Right now everything is working, or appears to be working my table is populated with the objects request from the JSON document. Originally I was doing this exact same thing in a normal ViewController and trying to set the objects from the block equal to locationObjects. Then using a prepareForSegue method i was trying to pass the locationObjects to the tableViewController in the standard method i have learned from numerous tutorials. I would get a SIGBAT error. The thread would terminate because of an unrecognized selector sent to the table view controller. Through debugging i would find that locationObjects could be nil in the prepareForSegue method. Here is the code from the viewController file.
Also I would get a warning here locationTableViewController.locationObjects = self.locationObjects; saying something about assigning a pointer of type NSArray to strong NSArray, or something like that ( i have since changed a lot attempting to get the code working and deleted some storyboard assets, so i'm not 100% sure of the wording).
#implementation CoffeeShopViewController
#synthesize venueCountLable = _venueCountLable;
#synthesize locationObjects = _locationObjects;
- (void)viewDidLoad
{
[super viewDidLoad];
[VenueManager searchNear:#"Orlando"
onLoad:^(NSArray *objects) {
self.venueCountLable.text = [NSString stringWithFormat:#"%d", objects.count];
self.locationObjects = objects;
} onError:^(NSError *error) {
NSLog(#"%#", error);
}];
}
- (void)viewDidUnload
{
[self setVenueCountLable:nil];
[super viewDidUnload];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"locationTableSegue"])
{
LocationTableViewController *locationTableViewController = segue.destinationViewController;
locationTableViewController.locationObjects = self.locationObjects;
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#end
Try:
#property (nonatomic, copy) NSArray *locationObjects;'
Edit on why this works:
To be honest, I really don't know the underlying reason for this to work and strong not working.
When I saw the problem, it appeared to me that strong being equivalent of retain - inserting copy instead of strong could secure that locationObjects wouldn't be nullified. Thinking again over it, I suspected that my assumption could be wrong - retain literally meant 'Do not release this object because now there is one more guy holding it.'
That, however, works somewhat differently. See this.
What Malaxeur's answer and comments below tells could possibly apply to NSArray in your example - despite strong ownership to locationObjects, what you are given is a reference to objects NSArray (an NSMutableArray*) instead of copy of it. Once out of scope (block end), it is no longer usable, and ARC claims it. Using copy in turn forces it to create another space in memory just for locationObjects, which would remain forever until you free it up.
I still do not consider this a perfect explanation as I have never understood blocks fully. I would keep this open to everyone who knows better, would fill up as soon as I get something that's useful.

Initializing a static singleton object with NSCoder

I'm working on an iPhone app and facing some troubles with my shared singleton class.
I'm using a shared singleton to store two variables
int gameRuns and int totalScore
'gamRuns' just increments every time the user loads the app, and 'totalScore' is obvious :D
the issue is as follows, I load the singleton and init using my own method when the app loads using this code:
+ (SingletonLevelState*)sharedLevelStateInstance {
static SingletonLevelState *sharedLevelStateInstance;
#synchronized(self) {
if(!sharedLevelStateInstance) {
//Init a singleton
sharedLevelStateInstance = [[SingletonLevelState alloc] init];
sharedLevelStateInstance->gameRuns = 1;
sharedLevelStateInstance->totalScore = 0;
}
}
return sharedLevelStateInstance;
}
This is working great as I can reference this class from any other class and always get a pointer to the same object, so this works fine from other objects:
sharedLevelState = [SingletonLevelState sharedLevelStateInstance];
sharedLevelStateInstance.gameRuns++;
Now I added the NSCoder protocol, and added the two methods initWithCoder and encodeWithCoder as follows :
- (void) encodeWithCoder: (NSCoder *)coder
{
//encode level data
[coder encodeInt:self->gameRuns forKey:#"gameRuns"];
[coder encodeInt:self->totalScore forKey:#"totalScore"];
}
- (id) initWithCoder: (NSCoder *) coder
{
if(self = [super init]){
self->gameRuns = [coder decodeIntForKey:#"gameRuns"];
self->totalScore = [coder decodeIntForKey:#"totalScore"];
}
return self;
}
Now when the app loads, I check to see if we already have a saved sate, if it exists, I just unarchive the class with that file, if not, I init that class using my custom method above, then set its defaults, encode it to file so we have a saved state, here's the code:
//Load Level state
sharedLevelStateInstance = [SingletonLevelState sharedLevelStateInstance];
//Check if file is saved
NSFileManager *fm = [[NSFileManager alloc] init];
NSString *gameStatePath = [NSString stringWithString:[self getSavePath]];
if([fm fileExistsAtPath:gameStatePath]){
[self loadState];
sharedLevelStateInstance.gameRuns = sharedLevelStateInstance.gameRuns+1;
NSLog(#"Loaded %d times", [sharedLevelStateInstance gameRuns]);
}
[fm release];
Now the last line in the if statement works perfectly, it increments every time I load the app as expected and I feel really happy lol.
However, the problem arises when I try to get a reference of the singleton in another class by doing the following:
sharedLevelStateInstance = [SingletonLevelState sharedLevelStateInstance];
NSLog(#"Played: %d times", sharedLevelStateInstance.gameRuns);
It always counts back to 1, I know what happens but I'm not sue what's the best way to solve it, when I initWithCoder the singleton, It's not returning a static object, it creates a new one, when I init my sharedLevelStateInstance, it calls my first custom method, initializing it to the defaults hardcoded.
So StackOverflow, can you please help me ?!
I just need to know what's the best way to get a reference to the same object without allocating a new one every time I initWithCoder !
Thanks :)
So, you code should probably look like this:
if(self = [[SingletonLevelState sharedLevelStateInstance] retain])
Which sets the variables of the singleton, and returns the singleton. Be sure to retain the singleton, so that when the NSCoder releases this instance, it doesn't fully deallocate your singleton.

Inheritance in Objective C

I have a main game class which renders the game using Open GL. Now I thought I could inherit from this class and then just call [super init] in its init method to get a copy of it. The plan was to make some modifications in the copy but as it seems this doesn't work.
The following is the header file of my class:
#import "GameView.h"
#interface CloneView : GameView {
}
-(id)initWithFrame:(CGRect)frame;
#end
And this is the Clone view class:
#implementation CloneView
-(id)initWithFrame:(CGRect)frame{
return [super initWithFrame:frame];
}
#end
If I set a break point in the init method in the GameView class it stops there. Thing is: my clone view doesn't get rendered, the screen stays black.
What am I missing? Thanks for your help!
Edit
Just for the record: I tried without implementing initFrame and got the same result. (as expected as the initFrame as above isn't doing anything apart from calling super)
Edit 2
I'm adding my clone to another view so I'm creating two Eagle contexts. Could that be the reason why it doesn't work?
If you are not adding anything in the init function of CloneView than you don't even have to rewrite it. You can just have your class inherit from GameView and it automatically copies it's init function.
This is from the apple docs
You should assign self to the value returned by the initializer because the initializer could return an object different from the one returned by the original receiver.
So Try doing this
-(id)initWithFrame:(CGRect)frame{
if(self = [super initWithFrame:frame] ) {
//Do whatever you need to do here.
}
return self;
}
This should fix your issue if you need to do something in your init method. Otherwise you can skip the init method altogether.
try doing this it may work..
return(self=[super initWithFrame:frame])
which ensures the super class method is copied properly to the current method
TNQ
I finally located the problem:
I needed to write a second init method. The problem was that the following code was being executed twice:
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)[super layer];
[eaglLayer setOpaque:YES];
m_context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
if (!m_context || ![EAGLContext setCurrentContext:m_context]) {
printf("FAIL \n");
[self release];
return nil;
}
[Textures loadTexture];
if ([self createFramebuffer]) {
[self draw];
}
This code was in the initFrame method of the game class. I made a second init method that does not execute this code. Instead, this code is executed in the parent view class of the clone. Now it works, YAY!!
Thanks for trying to help me!

Problem with reinitializing of string in iPhone

I have an interface like this:
#interface MacCalculatorAppDelegate:NSObject
<UIApplicationDelegate> {
// ...
UIButton *operatorPressed;
NSString *waitingOperation;
}
And I am initializing waitingOperation variable in my implementation like this:
- (id)init {
if (self = [super init]) {
waitingOperation = #"not set";
}
return self;
}
And I want to reinitialize this variable in a function. This is calculator program and when user clicks on operators button the following function will be invoked:
- (IBAction)operatorPressed:(UIButton *)sender {
if([#"+" isEqual:operand]) {
waitingOperation = #"+";
}
}
But after the check in if statement, my program won't do anything and this happens when I am trying to reinitialize waitingOperation variable.
I am new to objective-c, please help me understand what's wrong here.
Thanks in advance.
There are several things to note here.
waitingOperation=#"not set";
As it stands, this will eventually crash your program. Objective C string literals are autoreleased instances of NSString, which means unless you assign it to a retained property or retain it manually, the memory will be deallocated, leaving a dangling pointer.
-(IBAction) operatorPressed:(UIButton *)sender {
Have you verified that this method is actually being called? You have to assign the IBAction in Interface Builder. Step through it in the debugger or use NSLog to verify that this method is being called.
if([#"+" isEqual:operand])
Where is operand coming from?
waitingOperation=#"+";
Same problem as above, this will get deallocated behind the scenes, leaving you with a dangling pointer.
Also, note that if you know that both variables are NSStrings, using isEqualToString: is faster than using isEqual:.
Finally, this stuff shouldn't be part of your app delegate. This is view controller logic.
If your operand is also a string, then check for isEqualToString instead of isEqual

instance variables not accessible

Serious Problem here... i'm getting ECX_BAD_ACCESS if i try to NSLog an instance variable of my custom object. Following Function is called in my ViewController, payload holds String Data which is pulled from a url.
- (void) initVcardWithData:(NSString *)payload {
NSLog(#"1. initVcardWithData");
aVCard = [[vcardItem alloc] initWithPayload:payload];
VCardViewController *aVCardViewController = [[VCardViewController alloc] initWithVCard:aVCard];
[self presentModalViewController:aVCardViewController animated:YES];
[aVCard release];
}
So far so good. The initWithWithVCard function is as follows, theVCard and theVCardN are defined in #implementation and also set as a #property (nonatomic, retain) in (.h).:
-(id)initWithVCard:(vcardItem *)aVCard {
if(self = [super init]) {
theVCard = [aVCard retain];
theVCardN = [theVCard.PersonName retain];
}
NSLog(#"---- vCardViewController :: initWithVcard :: FirstName: %#", theVCard.PersonName.FirstName);
return self;
}
If i access the theVCardN object in my ViewController aVCardViewController within ViewDidLoad everything works like a charm. I set some labels with data from that object.
If i then try to access the instance variables from theVCardN within a function which is called from an IBAction which is connected to a button in View, i get an EXC_BAD_ACCESS error at the debugger console. The Function which tries to pull data from the instance variables is as follows:
-(IBAction)addressbookButtonTapped {
NSLog(#"RETAIN COUNT FOR theVCard: %i", [theVCard retainCount]);
NSLog(#"RETAIN COUNT FOR theVCardN: %i", [theVCardN retainCount]);
NSLog(#"Save to Adressbook: %#", theVCardN.FirstName);
//[self dismissModalViewControllerAnimated:YES];
}
The RetainCounter for theVCardN right before calling NSLog outputs "1". The NSLog Line then returns EXC_BAD_ACCESS in Debugger Console.
Any idea ?
Do not call -retainCount. Absolute retain counts are useless.
retainCount returns the absolute retain count of an object. The actual value will be an implementation detail that is very often completely out of your control as the system frameworks may do any number of things internally to cause the retain count to be modified in ways you don't expect.
It is useless for debugging and their are a wealth of tools that are specifically focused on tracking down these kinds of issues.
First, if there is a crash, there is a backtrace. Post it. Probably not that interesting in this case, but, still, always look to the backtrace to at least confirm that it is crashing where/how you think it is.
From the evidence posted, it sounds like theVCardN.FirstName is either set to garbage or the underlying string has been over-released. Turn on zombie detection mode and see if that is the case. Since it is crashing on FirstName, then show the code related to creating/storing the FirstName.
Also, instance variables and methods should always start with a lowercase letter; PersonName should be personName & FirstName should be firstName.
Maybe i'm reading the code wrong or misunderstanding your class structure, but it looks like you logging:
NSLog(#"Save to Adressbook: %#", theVCardN.FirstName);
Above, where you say it is still working, you are logging:
theVCard.PersonName.FirstName
Are you missing the "PersonName"? Meaning you should be logging:
NSLog(#"Save to Adressbook: %#", theVCardN.PersonName.FirstName);