• 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: Finding memory leaks
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Finding memory leaks


  • Subject: Re: Finding memory leaks
  • From: Jens Alfke via Cocoa-dev <email@hidden>
  • Date: Wed, 20 May 2020 13:18:24 -0700


> On May 20, 2020, at 7:03 AM, Gabriel Zachmann via Cocoa-dev
> <email@hidden> wrote:
>
> This line is in a method I declared like that:
>
> - (void) loadNextImageWithIndex: (unsigned long) next_index image:
> (CGImageRef *) next_image
>                      withProps: (CFDictionaryRef *) next_props

Off-topic, but that's an odd signature. Wouldn't it be clearer to return the
CFDictionaryRef instead of making it an 'out' parameter?

> The caller then makes a copy of next_props for later use, and CFRelease's
> next_props.
> (That copy is also released later.)

The "copy" may be the same object. In CoreFoundation and Foundation, if you
copy an immutable object, you just get back a new reference to the same object.
Same effect, but it saves memory and time. So the leaky code may be in
whatever's managing that copy you made.

> So it is unclear to me why the Leaks tool thinks that the above line leaks
> memory.

Leaks can be hard to track down; I sympathize.

Avoiding CF objects as much as possible helps. Recall that an NSDictionary* is
the same as a CFDictionaryRef. You can use bridge casts to convert, and if the
CF ref needs to be released later, you can call CFBridgingRelease which casts
and autoreleases in one operation.

Another useful but not-well-known function is CFAutorelease, which is the
equivalent of pre-ARC Obj-C `autorelease`. You basically call it instead of
CFRelease; but you can call it immediately and keep using the object, because
autorelease just promises that the object will be released sometime later.

—Jens
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Finding memory leaks (From: Gabriel Zachmann via Cocoa-dev <email@hidden>)

  • Prev by Date: Re: Finding memory leaks
  • Next by Date: Re: Finding memory leaks
  • Previous by thread: Re: Finding memory leaks
  • Next by thread: Re: Performance issue on macOS 10.15 obtaining display name for ~/Desktop, ~/Documents, and ~/Downloads
  • Index(es):
    • Date
    • Thread