I am currently working on my first app for the iPhone, I am almost done and but I am having a problem with memory management and such. Keep in mind I am pretty good with java and I only have been learning Objective C for about 4 days.
So, the exact problem lies in this area (Between the lines of asterisks’).
Note: All of the code lies inside a big game loop if that matters.
else
{
***********************************
NSString *rand = [NSString stringWithFormat:@"%@", randNumberS];
while(lastTime + interval >= currentTime)
{
!!!!!!!!!NSString *user = [NSString stringWithFormat:@"%@", userText];
if([user isEqualToString: rand])
{
***********************************
score += 10;
randNumberS = nil;
timeToGenerateNum = true;
bottomClear = true;
break;
}
else
{
//NSLog(@"%@ != %@, %i", userText, randNumberS, score);
}
}
NSLog(@"Game Over! Your score was %i!", score);
}
}
Every time I ran before I enabled zombies (Note: The code ran for a few seconds btw), I got a Thread 6: Program received signal: "EXC_BAD_ACCESS" at the line marked with the “!”. After I enabled zombies, it runs for a few seconds and then it stops working and the message -[CFString respondsToSelector:]: message sent to deallocated instance 0x11168440 appears in the console. It also flags the same line with the “!”
I looked both of these up and they both point to poor memory management, I have tried releasing the NSString objects but my program won’t let me release the objects (Note: I get this error message "release" is unavailable: not available in automatic reference counting mode).
Any help would be greatly appreciated, thanks!
EDIT:
userText is used in a various number of methods, but mostly in this one.
-(IBAction)button1Clicked:(id)sender
{
if(userText == nil)
{
userText = [NSString stringWithFormat:@"%i", 1];
}
else
{
userText = [NSString stringWithFormat:@"%@%i",userText , 1];
}
bottomLabel.text = userText;
NSLog(@"Test 1");
}
The
userTextvariable is not a valid object, which is what your error messages indicate. In general, EXC_BAD_ACCESS happens when you try to use a pointer which points to something that is no longer around. Then with Zombies enabled, the message is even more clear,userTextused to be a string, but has been deallocated.EDIT:
If userText is an instance variable, it is recommended to use properties and then the dot notation. Somewhere there is an @interface section where userText is declared. It should look like this:
Then in an @implementaiton area, something like this:
These, together, make dot-notation available, and then you should use self.userText to access it everywhere (except custom accessors):