开发者

losing my string

开发者 https://www.devze.com 2023-01-20 07:15 出处:网络
I\'m having a problem with a string creation and comparison that seems to lose it\'s contents. Currently I have this:

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(@"开发者_StackOverflow中文版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 :(


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.


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.)


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消