0

I'm running into a problem with a string that contains encoded characters. Specifically, if the string has encoded characters it eventually becomes invalid while a "normal" string will not.

in the .h file:

@interface DirViewController : TTThumbsViewController 
<UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
    NSString *sourceFolder;
    NSString *encodedSourceFolder;
}

@property (nonatomic, retain) NSString *sourceFolder;
@property (nonatomic, retain) NSString *encodedSourceFolder;

in the .m file:

- (id)initWithFolder:(NSString*)folder query:(NSDictionary*)query {

    if (self = [super init]) {

        sourceFolder = folder;

    }

  return self;
}

Up to now everything seems to run as expected. In viewDidLoad I have the following:

sourceFolderCopy = [self urlEncodeValue:(sourceFolder)];

//I also have this button, which I'll refer to later:
UIBarButtonItem *importButton = [[UIBarButtonItem alloc] initWithTitle:@"Import/Export" style:UIBarButtonItemStyleBordered 
                                                                target:self
                                                                action:@selector(importFiles:)];
self.navigationItem.rightBarButtonItem = importButton;

Which uses the following method to encode the string (if it has characters I want encoded):

- (NSString *)urlEncodeValue:(NSString *)str { 

    NSString *result = (NSString *) CFURLCreateStringByAddingPercentEscapes (kCFAllocatorDefault, (CFStringRef)str, NULL, CFSTR(":/?#[]@!$&’()*+,;="), kCFStringEncodingUTF8); 

    return [result autorelease]; 

}

If I NSLog result, I get the expected values. If the string has characters like a white space, I get a string with encoding. If the string doesn't have any characters that need to be encoded, it just gives me the original string.

I have a button on the nav bar which begins an image import process by opening an action sheet. Once the method for the action sheet starts, my string is invalid - but only if it contains encoded characters. If it is just a "normal" string, everything is fine and acts as expected. Am I off on my encoding? At first I thought it might be a memory problem but I can't figure out why that would affect only encoded strings.

Here's where the action sheet is defined (and the first place I can see the encoded string becoming invalid) the NSLog statements are where it crashes:

- (IBAction)importFiles:(id)sender {

NSLog(@"logging encodedSF from import files:");
NSLog(@"%@",encodedSourceFolder);//crashes right here
NSLog(@"%@",sourceFolder);

if (shouldNavigate == NO)
{
    NSString *msg = nil;
    msg = @"It is not possible to import or export images while in image selection mode.";

    UIAlertView *alert = [[UIAlertView alloc] 
                          initWithTitle:@"Unable to Import/Export" 
                          message:msg 
                          delegate:self 
                          cancelButtonTitle:@"OK" 
                          otherButtonTitles:nil];
    [alert show];
    [alert release];
    [msg release];
}   

else{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] 
                                  initWithTitle:@"What would you like to do?" 
                                  delegate:self 
                                  cancelButtonTitle:@"Cancel"
                                  destructiveButtonTitle:nil 

                                  otherButtonTitles:@"Import Photos (Picker)", @"Export Photos", nil, nil];

    [actionSheet showInView:self.view];
    [actionSheet release];
  }
}

I don't get any crash errors going to the console. By using breakpoints I was able to see that the encodedSourceFolder is invalid in the action sheet method.

shoreline
  • 53
  • 1
  • 6

2 Answers2

0

You should copy your passed in folder string in your initWithFolder:query: method like this or create a new string with:

- (id)initWithFolder:(NSString*)folder query:(NSDictionary*)query {

    if (self = [super init]) {

        sourceFolder = [folder copy];

    }

    return self;
}

Otherwise your string gets autoreleased elsewhere.

schaechtele
  • 1,130
  • 1
  • 9
  • 12
  • you could also use "sourceFolder = [[NSString alloc] initWithString:folder];" – schaechtele Mar 30 '10 at 07:19
  • sourceFolder = [folder copy]; results in the same behavior. A string with encoded characters becomes invalid at the exact same spot while a string without encoded characters remains. – shoreline Mar 31 '10 at 01:23
  • sourceFolder = [[NSString alloc] initWithString:folder]; also results in the behavior in the original post. – shoreline Mar 31 '10 at 02:50
0

Do not use retain for NSString properties. Use copy:

@property (nonatomic, copy) NSString *sourceFolder;

There are several questions/answers here that explain this further, such as Chris Hanson's response at:

NSString property: copy or retain?

Community
  • 1
  • 1
Shaggy Frog
  • 27,575
  • 16
  • 91
  • 128
  • using copy instead of retain results in the same behavior; a string without encoded characters is still invalid at the same point in my application while a "normal" string is not invalidated and is accessable. – shoreline Mar 31 '10 at 01:20
  • It appears autoreleasing the result in urlEncodeValue: was the problem. I'm not sure why that would only affect a string with encoded characters, since a string gets passed through it regardless. I suppose I need to make a copy of the string at that point, not in the source folder. – shoreline Mar 31 '10 at 03:06