I then extract the cid code by plugging it into a NSString variable like this:

cid = [response objectForKey:@"cid"];

Later in an another method I try to use cid in an ASIFormDataRequest

[request setPostValue:cid forKey:@"cid"];

but that crashes the programme. If I place cid = @"7" (for example) right before this setPostValue:cid statement everything works fine, so I know the functionality of the request is fine. Likewise if I NSLog(@"%@",cid); that also works.

I believe my question is identical to this one, but no one has given a concrete answer as to how to solve it.

Any ideas?

EDIT 1

There is not crash log. Xcode just gives me this message in the bottom bar:

** EDIT 2 **

Here are some images from me printing with the debugger. I don't know if I've done this correctly though.

Here is how I'm plugging in response data into CID. CID is an NSString declared in the header file with the whole @property(nonatomic, retain) bit.

Eric, can you paste a little bit more of the code, including the variable definitions? In addition, my recommendation to you would be to run this in the debugger and set a breakpoint on the line that crashes. When you hit that breakpoint, please edit your question to include the type of cid and the value, as presented in the debugger. The next interesting thing to know is if the application crashes in the debugger when you step over that line.
–
Art TaylorSep 11 '11 at 9:22

2 Answers
2

CID is an NSString declared in the header file with the whole @property(nonatomic, retain) bit.

Even though you’ve declared it as a retain property, you’re not using it as a property. By using:

cid = [number stringValue];

you’re directly assigning an object to cid, which is the backing instance variable, thus bypassing the property setter that would retain that object. This means that as soon as the corresponding autorelease pool is drained, cid probably no longer points to a valid object.

When you’re interested in an object that should survive autorelease pool drains then you should take ownership of it. With retain properties, this means invoking the property setter, e.g.:

self.cid = [number stringValue];

This will retain the [number stringValue] assigned to the cid property and, if everything else in your program is correct with regard to memory management, you’ll be able to use self.cid in other parts of your program, as in:

Later in an another method I try to use cid in an ASIFormDataRequest

For the record, NSString properties are usually copy instead of retain to prevent side effects when the property is assigned a mutable string.

Absolutely terrible answer. Every single thing is wrong. First, the test for cid != nil is utterly useless before [cid release], because the message-to-nil behavior makes [cid release] safe regardless of cid is nil. Second, using [[NSString alloc] initWithString:foo] is absolutely wrong. If [response objectForKey:@"cid"] is a string already, then it's useless. If it's not, you just passed a non-string to -initWithString: and will almost certainly crash. Third, you've now made cid contain either an autoreleased or a retained object, which is absolutely wrong.
–
Kevin BallardSep 11 '11 at 9:29

all I want to convey is that...an autorelease object returned by other method will not be available in other method.
–
MohammadSep 11 '11 at 9:42

Oh I also forgot, the entire premise of [cid release] is flawed, because cid is an autoreleased object and the [cid release]will cause a crash.
–
Kevin BallardSep 11 '11 at 9:46