• 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: Undo prepareWithInvocationTarget: retain/release question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Undo prepareWithInvocationTarget: retain/release question


  • Subject: Re: Undo prepareWithInvocationTarget: retain/release question
  • From: Keith Blount <email@hidden>
  • Date: Wed, 9 Mar 2005 06:30:39 -0800 (PST)
  • Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys

Many thanks for your reply, much appreciated - and I
especially appreciate the Princess Bride paraphrase (I
love the film and the book).

Sorry, perhaps I wasn't very clear. When I said, "I
know I have to release an object...", I really meant
that I know that addSubview: retains the view that is
passed in. It may not be explicit in my code, but it
is retained by the superview. Therefore I release it
so that I have only one copy hanging around and don't
need to worry about it later. If I ever need to remove
it from its superview and keep the object, I can
always retain it before doing so.

What I usually do with subviews is to create a view,
add it as a subview, and then release it, allowing the
superview to keep it. I then just keep a pointer to
the view if I need one. Is this bad practice? It saves
me from having to release lots of subviews in dealloc
and I don't see why this would be a problem...

As for undo, my confusion arose because although I
hadn't explicitly retained the view anywhere, I
figured that once it had been passed into my method by
the undo manager as specified in
prepareWithInvocationTarget:, as I had added it as a
subview of another view (thus increasing its retain
count by one), I should release it. However, your
explanation clarifies things - in fact, I set up undo,
and then release the object, so the undo manager has
taken responsibility for retaining the object, not me.
When the manager passes the object into my method when
undo is called, it autoreleases the object, assuming
that I have finished with it, so I don't need to - at
least, this is what I figure is going on. Working on
this assumption, the crashes have disappeared from my
code, and there doesn't seem to be a memory leak (I
hope).

Thanks again,
Keith


-----------

FROM : buck
DATE : Wed Mar 09 01:56:01 2005

Undo follows the same conventions for releasing
objects as everything else
in Cocoa.

I did not see any place in the code posted where you
explicitly
alloc/retain/copy aView.  Therefore, I would be very
surprised if you are
happy with the result of explicitly releasing aView.

If you did not explicitly alloc/copy/retain and object
you are not
responsible for (and should not) explicitly release or
autorelease it.
If you did explicitly alloc/copy/retain and object you
are responsible for
ultimately explicitly sending release or autorelease
to it.

Why do you say "...I know I have to release an object
after adding it as a
subview..."  What on earth gave you that idea ?

Adding an object as a subview is NOT explicitly
alloc/copy/retaining the
object in YOUR CODE.  If the view to which you just
added a sub-view
implicitly retains the sub-view (as I am sure it
does), that is not your
concern.  To paraphrase "The Princes Bride," if you
didn't type it, you
didn't do it.

-------

FROM : Keith Blount
DATE : Wed Mar 09 01:27:47 2005

Hello,

In testing the undo/redo implementations in my app, I
have found that I am getting a crash when doing
something like the following:

[[[self undoManager] prepareWithInvocationTarget:self]
undoRemoveView:aView];

...

- (void)undoRemoveView:(NSView *)aView
{
  [self addSubview:aView];
  [aView release];
}

The crash is caused by releasing aView, as it is
apparently crashing with the same object being
released in an autorelease pool. This has had me
stumped, as I know I have to release an object after
adding it as a subview.

I am guessing this is to do with how the undo manager
works. Am I right in assuming that the undo manager
retains an object when it is used in a call to
prepareWithInvocationTarget:, and then autoreleased
after undo/redo has been called with that
target/method? In which case, I don't need to release
the object myself as it is done automatically...

Maybe I've missed something obvious in the undo
architecture intro or NSUndoManager description in the
docs, but I'd be very grateful if somebody could clear
this up for me.

Many thanks,
Keith




__________________________________
Celebrate Yahoo!'s 10th Birthday!
Yahoo! Netrospective: 100 Moments of the Web
http://birthday.yahoo.com/netrospective/
 _______________________________________________
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

  • Prev by Date: NSTableView and automatic resize to fit all information
  • Next by Date: Re: Weak linking frameworks?
  • Previous by thread: Re: Undo prepareWithInvocationTarget: retain/release question
  • Next by thread: Problem with distributed objects and NSArrayController
  • Index(es):
    • Date
    • Thread