• 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 is this code leaking memory?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why is this code leaking memory?


  • Subject: Re: Why is this code leaking memory?
  • From: matt neuburg <email@hidden>
  • Date: Fri, 20 Dec 2002 20:50:17 -0800

On Thu, 19 Dec 2002 20:31:26 +0100, Jan Van Boghout <email@hidden> said:

>I have two lines that load an imageRep of an image on the HD, and
>they're leaking memory like hell. Any ideas why this is happening? It
>shouldn't, that's the problem.
>
>Code:
>
>imageRep = [NSBitmapImageRep imageRepWithData:[NSData
>dataWithContentsOfFile:@"/Library/Desktop Pictures/Aqua Blue.jpg"]];
>
>[imageRep release];
>
>Say I want to load the Aqua Blue desktop pic. *Every* time this gets
>called my test app's virtual memory usage increases with 3 MB or more

(1) Reading virtual memory usage from some high-level tool such as "top", "ps" or Process Viewer tells you nothing about whether you are leaking. The issue is complex, and has been well explained by various people, especially Bill Bumgarner, so please, search the archives. At bottom level you must not expect memory usage to shrink each time you do a release, because malloc/free doesn't work that way; freed memory can simply be kept on hand in case it is needed again. And the virtual memory system sits on top of this and makes things even more complicated. The only way to know if you are leaking memory is to look at what objects you've got. If the number of them doesn't go up between invocations, you're not leaking.

(2) Your code may leave objects lying around merely because it uses some autorelease objects. To get rid of autoreleased objects promptly, wrap the call in an NSAutoreleasePool alloc/init and release.

(3) I'm less confident about the relevance of this point, but I seem to recall that to get rid of the cached version of an NSImage, one must recache it. Maybe there's some similar device needed here (sorry I dont know what it is). m.

m.

matt neuburg, phd = email@hidden, http://www.tidbits.com/matt
pantes anthropoi tou eidenai oregontai phusei
Subscribe to TidBITS! It's free and smart. http://www.tidbits.com/
_______________________________________________
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.

  • Prev by Date: Re: Increasing a cell's height!!! HELP!
  • Next by Date: NSTextView Alignment
  • Previous by thread: Re: Why is this code leaking memory?
  • Next by thread: Re: Why is this code leaking memory?
  • Index(es):
    • Date
    • Thread