• 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: [NSCalendar currentCalendar] leak
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [NSCalendar currentCalendar] leak


  • Subject: Re: [NSCalendar currentCalendar] leak
  • From: Daniel Jalkut <email@hidden>
  • Date: Sun, 5 Nov 2006 11:01:58 -0500

On Nov 5, 2006, at 8:50 AM, Mike Abdullah wrote:
Again, not a problem. If the framework changes, my implementation will still be used.

Which begs the question, what use is it for you to override "currentCalendar" at all? This technique of using a category to "override" seems very fragile to me. I can't imagine there is a guarantee that yours will override the original, except that by luck it happens to be loaded dynamically later than the original.


If I was in your position I would take the much less fragile approach of simply defining your own uniquely named method "currentCalendarWithoutLeaking," and use that throughout your code.

What matt was getting at, I think, is it's dangerous to make assumptions about what the opaque framework is normally doing behind your back. He's right. You might think it's obvious that replacing a method like this outright is a safe thing to do, and it probably is, but you could definitely be wrong.

At the very least we might at least examine what's going on in the method, to reassure ourselves.

bl      0x92adb880 <dyld_stub_CFCalendarCopyCurrent>
bl      0x92ad49a0 <dyld_stub_CFMakeCollectable>
lwz     r0,88(r1)
addis   r4,r31,4076
addi    r1,r1,80
lwz     r4,-15100(r4)
mtlr    r0
lmw     r30,-8(r1)
ba      0xfffeff00 <objc_msgSend_rtp>

By looking at what "currentCalendar" actually does, we not only get a great idea of how we can best imitate it, we also see exactly what's causing the bug. Oops! Apple is using garbage collection to manage the memory for this object. Apparently somebody didn't get the memo that this wasn't going in for 10.4.

Since CFCalendar is toll-free bridged to NSCalendar, my non-leaking version, if I was using this method, would look something like this:

- (NSCalendar*) currentCalendarWithoutLeaking
{
// NSCalendar and CFCalendar are toll-free bridged
NSCalendar* currentCalendar = (NSCalendar*) CFCalendarCopyCurrent ();
return [currentCalendar autorelease];
}


Now you've got a reliable method that doesn't get its hands dirty overriding existing methods, is known to have identical conceptual behavior to the existing implementation, and guaranteed to have long- lived conceptual correctness in its own right.

Daniel

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: [NSCalendar currentCalendar] leak
      • From: "Adam R. Maxwell" <email@hidden>
    • Re: [NSCalendar currentCalendar] leak
      • From: Bill Bumgarner <email@hidden>
References: 
 >Re: [NSCalendar currentCalendar] leak (From: Matt Neuburg <email@hidden>)
 >Re: [NSCalendar currentCalendar] leak (From: Mike Abdullah <email@hidden>)

  • Prev by Date: Indicate loading while bindings read from Core Data
  • Next by Date: Re: Problem with IB not control-dragging to make connections
  • Previous by thread: Re: [NSCalendar currentCalendar] leak
  • Next by thread: Re: [NSCalendar currentCalendar] leak
  • Index(es):
    • Date
    • Thread