views:

57

answers:

4

Hey

I'm having a problem with a string creation and comparison that seems to lose it's contents. Currently I have this:

switch (creditPos) 
{
    case 0:
        [creditCart.faceImage setImage:[NSString stringWithFormat:@"%@credits_face1.png", _director.platformPrefix]];
        break;
    case 1:
        [creditCart.faceImage setImage:[NSString stringWithFormat:@"%@credits_face2.png", _director.platformPrefix]];
        break;
    case 2:
        [creditCart.faceImage setImage:[NSString stringWithFormat:@"%@credits_face3.png", _director.platformPrefix]];
        break;
    case 3:
        [creditCart.faceImage setImage:[NSString stringWithFormat:@"%@credits_face4.png", _director.platformPrefix]];
        break;
    case 4:
        [creditCart.faceImage setImage:[NSString stringWithFormat:@"%@credits_face5.png", _director.platformPrefix]];
        break;
    default:
        break;
}

faceImage is an object I've created and inside the function for setImage I have...

- (void)setImage:(NSString *)inImageName {

NSLog(@"Before Break");

// By default set the scale to 1.0f and the filtering to GL_NEAREST
if(![imageName isEqualToString:inImageName])
{
    NSLog(@"Hit");}

The problem I'm having is that when I pass in the string using the NSString stringWithFormat, it will work perhaps 5-8 times before somehow bugging up and sending something completely random instead to the function such as -36.657.

How is this possible? Nothing in the parameters is changing as _director.platformPrefix is set at the start of the program and never altered. The only thing which changes is creditPos to select with string to create + pass to the function. Somehow the string being created is just gibberish after some iterations and trying to compare it to the last string passed in crashes the code without any error thrown back.

Help :(

A: 

Sounds like one of the strings was dealloced in between. Perhaps an over-release or not properly reacting to memory warnings or viewDidUnload.

For debugging purposes, try to retain _director.platformPrefix and print out the retainCount of that object. If the problem goes away, you've indeed a retainCount issue. In almost 99% of the cases, the final solution is not to just retain it but find the code that wrongly releases it.

Ortwin Gentz
A: 

How are you assigning inImageName to imageName. If you aren't using a property and you aren't retaining then your inImageName string is getting garbage collected. The stringWithFormat method will return an autoreleased string that will get cleaned up automatically unless you retain it.

Either create in your header:

@property (nonatomic, retain) NSString *imageName;

and in your implementation:

@synthesize imageName;

Or, add a [inImageName retain]; in there somewhere when you assign it to imageName. (Just make sure you release the imageName string before assigning it and in your dealloc method.)

dj2
I was using: @property (nonatomic, readonly) NSString *imageName; to prevent any setting of imageName. Under the if statement for checking if the strings were equal I simply had imageName = inImageName. instead I now have imageName = [inImageName retain]; and this has fixed it, thanks for the heds up about stringWithFormat returning an autoreleased string.
A: 

You might want to try alloc'ing the string as this may be an autorelease issue.

NSString *myString = [[NSString alloc] initWithFormat:@"%@credits_face%d.png",_director.platformPrefix, creditPos+1];
[creditCart.faceImage setImage:myString];

Then have the setImage method release the string.

Ben
That will probably not help because `_director.platformPrefix` has the autorelease issue not the newly created string. Aside from that, it's better style indeed to parametrize the string instead of the ugly switch statement.
Ortwin Gentz
[NSString stringWithFormat:] returns a new autoreleased string as well.
Ben
Thanks Ben, I added a retain to inImageName when I assigned it to imageName and it's fixed it for now. Edit: Actually used your NSString alloc line instead and didn't need the retain. Thanks all the more.
-1. That style will generally result in a leak. You are not responsible for retaining it since you do not want to own it.
tc.
Yes they will have to be careful with it, not sure its a -1 though, but thanks.
Ben
A: 

I agree with Ortwin; it sounds like platformPrefix is not being properly retained, and it manages to stay intact for a little before being overwritten.

randallmeadows