Sweta Kishore
Sweta Kishore

Reputation: 51

gettin EXC_BAD_ACCESS while using nsstring

I am getting crazy during 4 hours and I really need help. Here is the code:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    //check if strGroup has prefix and suffix #
    BOOL result;

    result = [strGroup hasPrefix: @"#"];

    if (result)
    {
        result = [strGroup hasSuffix: @"#"];

        if (result)
        {

            NSMutableString* string = [NSMutableString stringWithString: strGroup];
            str = [strGroup substringWithRange: NSMakeRange (1, [string length]-2)];

            strToHoldAllContact = [NSString stringWithFormat:@"%@",str];
        }
    }
    NSLog(@"strToHoldAllContact=%@",strToHoldAllContact);
}

I am gettin the value of strToHoldAllContact correctly. But when I try to access strToHoldAllContact from another method I am getting the error:

[CFString respondsToSelector:]: message sent to deallocated instance 0x856f2a0

Upvotes: 0

Views: 216

Answers (4)

Saurabh Passolia
Saurabh Passolia

Reputation: 8109

with ARC, in .h declare strToHoldAllContact as:

@property(strong) NSString *strToHoldAllContact;

in .m, use it (after @synthesize) as self.strToHoldAllContact = [NSString stringWithFormat:@"%@",str]; this way you will not have problems.

without ARC,in .h declare strToHoldAllContact as:

@property(retain) NSString *strToHoldAllContact;

and use it same ways as with ARC in.m file.

Upvotes: 0

Feo
Feo

Reputation: 161

Use

strToHoldAllContact = [NSString stringWithFormat:@"%@",str];
[[strToHoldAllContact retain] autorelease];

and forget about release.

Upvotes: 1

Maksim
Maksim

Reputation: 2052

Just replace

strToHoldAllContact = [NSString stringWithFormat:@"%@",str];

to

strToHoldAllContact = [[NSString alloc] initWithFormat:@"%@",str];

And release it after you don't need it anymore.

Upvotes: 0

shabbirv
shabbirv

Reputation: 9098

where ever you are initializing or setting the string do this [strToHoldAllContact retain]; and don't forget to release it once you are done using it

Upvotes: 0

Related Questions