0

i think i don't finish to understand all about memory and that stuff but this is my problem:

I have a variable defined idActual on a view that will be pushed (var defined in its header), i can read (NSLog(idActual)) and set it to nil without problems. BUT when i change its value i get an CFString error, that its supposed to be due to bad memory management, i've tried this:

i can do this: nextView.idActual = nil;

i cant do this:

a) nextView.idActual = @"1";

b) NSString *str = [NSString stringWithFormat:@"1"]; 
   nextView.idActual =  str;

c) NSString *str = [[NSString alloc] initWithFormat:@"1"];
   nextView.idActual = str;
   [str release];

a, b and c always give me the CFString error: *** -[CFString isEqualToString:]: message sent to deallocated instance

5
  • I can print its value, in NSLog but i cannot modify it?... why D:
    – CalixJumi
    Sep 5, 2011 at 17:53
  • Please add more details about where you declared idActual.
    – rjgonzo
    Sep 5, 2011 at 18:23
  • This isn't the cause of the problem that you're asking about, but ... that isn't the way that stringWithFormat: is intended to be used. That string should be a printf style format string, followed by variables, like this [NSString stringWithFormat:@"%d words", numWords]. If you have a string, like @"1", and you want to create a new NSString, you would use [NSString stringWithString:@"1"].
    – Mike Hay
    Sep 5, 2011 at 18:44
  • Moreover, if you already have an immutable string, like @"1", you don't need to create a new string. Just assign your string literal there. (The time to make a new string is when you are passed the string from somewhere else, in which case it may be mutable, so you should copy it.) Sep 5, 2011 at 19:46
  • thanks for your comments, and it wasnt it the problem xD i fixed it already i just had to put a "self", i dont know why... but it was solved
    – CalixJumi
    Sep 5, 2011 at 19:51

1 Answer 1

0

It appears that the CFString (NSString) that is contained in nextView.idActual has already been released when you go to change the value. If you can post more of the related code, that would help.

I'm guessing that idActual is declared as @property(nonatomic,retain). When you try to set a new value into idActual, the setter method for that property is called (It's possible that the setter was automatically generated). The first thing that the setter method is doing is trying to compare the old value and the new value - and then it crashes.

When that setter method attempts to compare the new value to the old value, it runs into trouble because the old value is already deallocated.

Are you calling [nextView.idActual release] before you assign these new values ? If you are, comment out that line, and see if that fixes your problem. The auto-generated setter method will release the old value for you.

3
  • Lol i solved it, it had nothing to do with that piece of code but i thought it was due that but no... it was problem of a varA = anyObject, and it solved by self.varA = anyObject, i will never understand that
    – CalixJumi
    Sep 5, 2011 at 19:49
  • because if you don't use self.variable = other you're not taking advantage of the synthesized property ( which calls retain or copy behind the scenes). if you only do myvar = other, other does not get retained. Sep 5, 2011 at 20:57
  • ohhhhhhhhhh that clarifys everythin :D! (well, the last step that gave me headache all the week) thank you so much!!!!
    – CalixJumi
    Sep 6, 2011 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.