• 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: [OT] Retain count riddle
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [OT] Retain count riddle


  • Subject: Re: [OT] Retain count riddle
  • From: Bob Smith <email@hidden>
  • Date: Wed, 31 Mar 2004 13:46:22 -0800

This discussion raises a question I'd like to pose. (I'm relatively new to Cocoa and Objective-C, so apologies if this is a dumb question or one already answered a hundred times.) In this example the macro does not work because [path lastPathComponent] returns a newly created and autoreleased object, so each time it is invoked you get a different object. However, if that method returned a weak reference to an object, such as methods like [mySubView window] do, the macro would have worked fine because the returned object would always be the same one. How do I know which sort of object reference a particular method is going to return? Likewise, in designing my own objects, are there any rules I should follow about this sort of behavior?

Bob S.


On Mar 30, 2004, at 12:23 PM, Christopher Behm wrote:

Well, sub in your arguments to the macro:

[[path lastPathComponent] retain];
[m_Path release];
m_Path = [path lastPathComponent];

If lastPathComponent returns a new object at each call (and it probably does, likely an autoreleased string), then you'd have your problem. Even if it didn't return a new object, it'd be bad practice to do it that way. I'm not macro guru, so I don't know if there's a way to get what you want either.

Chris

On Tuesday, March 30, 2004, at 02:49PM, Sailesh Agrawal <email@hidden> wrote:

So I had this bug the other day that I thought would make an interesting
riddle.

The code looked like this:

#define SAFE_SET(old, new) [new retain]; [old release]; old = new;

...

- (void) foo : (NSString*)path {
SAFE_SET(m_Path, [path lastPathComponent]);
}

Now the problem was any time I accessed m_Path my app would crash. This
isn't cocoa specific, I'm just dumb for using macros where I should have
been using a function. Can anyone guess what the problem is ?

good luck !
Sailesh
_______________________________________________
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.
_______________________________________________
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.
_______________________________________________
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.


  • Follow-Ups:
    • RE: [OT] Retain count riddle
      • From: "Jonathan E. Jackel" <email@hidden>
    • Re: [OT] Retain count riddle
      • From: Shawn Erickson <email@hidden>
References: 
 >Re: [OT] Retain count riddle (From: Christopher Behm <email@hidden>)

  • Prev by Date: Wanted Gradient Fill for NSBezierPath
  • Next by Date: Re: Clicking through a transparent window?
  • Previous by thread: Re: [OT] Retain count riddle
  • Next by thread: Re: [OT] Retain count riddle
  • Index(es):
    • Date
    • Thread