Re: NSBitMapImageRep - memory footprint
Re: NSBitMapImageRep - memory footprint
- Subject: Re: NSBitMapImageRep - memory footprint
- From: Uli Kusterer <email@hidden>
- Date: Fri, 1 Sep 2006 12:15:44 +0200
Am 01.09.2006 um 05:43 schrieb Bruce Johnson:
Is it safe to assume that the removed representation is forever gone
and it's memory freed?
Yes. Though I'm not sure why you don't just use [NSImage
initWithSize: ...]. When you remove an image rep, NSImage will (auto)
release it, relinquishing its ownership. If nobody else retained the
image rep, it'll be released in response to that.
Or do I need to somehow release this reps memory footprint to avoid a
memory leak?
You don't release anything unless you retained or created it using
one of the methods that return an object owned by you. See Apple's
docs on memory management (mmalc posts a link to those in his "Cocoa
and List resources" post every month or so) for the details of
"return an object owned by you".
And that by adding a rep to the NSImage it is retained, so I then must
release it to avoid an extra increment in the retain count.
Well, essentially true, but you're thinking of this in a wrong way:
Don't think about "avoiding extra increments". There is nothing you
can do to keep NSImage from retaining your object. Instead, what you
should do is simply release or autorelease anything *owned by you*
when you don't need it anymore. If you create an NSImageRep using
alloc/init, you own it, so you release it. Had you used
imageRepWithData, you wouldn't own the object (in this particular
case, it would probably be autoreleased already, so you don't dispose
of it. Simple as that.
If you want to keep around an object that you don't own, retain it,
in which case you later have to release it again.
Don't look at the retain count, because for all you know the object
itself may start a timer or a new thread which retains it, and then
your retain count might magically be one more than you expected, but
still you *mustn't* release it.
The whole point of reference counting and release/retain is that
you *don't* have to care who else uses an object. If you want to use
an object, request ownership by retaining it, do your stuff, release
it. If nobody else wanted the object, it will get disposed, if anyone
else has been using it, it'll still be around to serve the other
code's needs. If it didn't work that way, you'd have to take great
care not to dispose an object for fear of disposing something under
someone else's ass, causing a crash. Don't fight it, just go with the
flow.
Cheers,
-- M. Uli Kusterer
http://www.zathras.de
PS - One nitpick: You can't release a memory footprint, only the
memory causing it :-)
_______________________________________________
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