• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Why my object becomes invalid ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why my object becomes invalid ?


  • Subject: Re: Why my object becomes invalid ?
  • From: "David W. Halliday" <email@hidden>
  • Date: Fri, 08 Aug 2003 21:35:42 -0400

kubernan @ 10191 Tec. wrote:

Hello,

I read again and again the retain/release rules but i always have a "strange"
behavior that causes a crash (signal 10).

In the -(id)init method of my main class i allocate a string :
(not compiled code)

@interface myClass : NSObject
{
NSString *myString
}

-(id)init
{
self= [super init];

myString = [[NSString alloc] init];


This NSString has a retain count of 1.

myString = [myString stringByAppendingString:@"test"];


You just leaked the first NSString (you should have sent it an autorelease message, at least), and the new NSString assigned to 'myString' has been autoreleased (it was not allocated, copied, or retained by you). Unless you send this new NSString a retain message it will disappear at some time in the future (sometime after leaving this method).

return self;

}

-(void)myTestMethod
{
// the first time this method is called there is no problem
// with the second call, i have a crash on myString object
// there is no manipulation on myString between the first and second call


That's because the NSString you have assigned to 'myString' has been autoreleased, and, therefore, will disappear at some time in the future. It just happens that this didn't happen before the first call to this method, but had happened by the second call.

NSLog([myDict objectForKey:@"theKey"]); // <- always gives something
NSLog(myString); // first time is OK, second call --> crash
[anOtherClass doSomethingWithTheParameter:[myString stringByAppendingString:[myDict objectForKey:@"theKey"]]];
}

Is it possible that [myString stringByAppendingString:[myDict objectForKey:@"theKey"]] as argument of the method
of anOtherClass makes something on myString object itself ?


Not at all. In fact, the NSString you sent to 'anOtherClass' is a completely different object, and, in fact, has, itself, been autoreleased (which is a good thing, since you haven't assigned it to any variables, within this method).

Thx for your help.
K.


I do hope this helps, but it does suggest you need to brush up on retain/release/autorelease (not that I didn't, before replying, just to make sure I wouldn't be steering you wrong).
Perhaps you thought that the 'stringByAppendingString:' message modified the NSString to which the message is sent? That would be a completely different issue (but since NSStrings are immutable, we /know/ this cannot happen with any methods of NSString).

David Halliday
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Why my object becomes invalid ? (From: "kubernan @ 10191 Tec." <email@hidden>)

  • Prev by Date: Re: Why my object becomes invalid ?
  • Next by Date: NSTableView sizeToFit is Evil!
  • Previous by thread: Re: Why my object becomes invalid ?
  • Next by thread: Passing FirstResponder calls to next responder
  • Index(es):
    • Date
    • Thread