replaceTextStorage: and retain/release woes
replaceTextStorage: and retain/release woes
- Subject: replaceTextStorage: and retain/release woes
- From: Keith Blount <email@hidden>
- Date: Mon, 22 Aug 2005 03:40:02 -0700 (PDT)
Hello,
I'm having some difficulty getting NSLayoutManager's
-replaceTextStorage: to work without either causing
over-releasing crashes or ending up with text storages
not getting released when examining my project using
ObjectAlloc.
Here is my situation:
My project has a list of text documents. When one is
opened, a text storage is created for that document
and it is opened in a text view. There can therefore
be an arbitrary number of text views using the same
text storage at any one time. The text storage for any
text view can also be swapped to another text storage
at any time.
Thus, I need to use -replaceTextStorage: to swap the
text storage of a text view whenever the document
changes. However, because of the way this works, I
also need to use -removeLayoutManager first to
disassociate the text view and text storage -
otherwise, using -replaceTextStorage will not only
swap the text storage for the given text view, but
also for all text views that are currently using the
same text storage, which I don't want. Here is my code
to do this:
// Note that *newTextStorage has been created and
// is held in a dictionary that contains all text
// storages for documents that have been opened
// prevent out of bounds errors
[textView setSelectedRange:NSMakeRange(0,0)];
// Make sure that switching the text storage only
// affects the specified text view
[[textView textStorage] removeLayoutManager:[textView
layoutManager]];
// Now swap the text storage
[[textView layoutManager]
replaceTextStorage:newTextStorage];
This works fine - until I close a text view that is
sharing its text storage with another text view. In
that case, it seems that newTextStorage gets released
and deallocated. The only way I have found away around
this is by doing this:
[[textView layoutManager]
replaceTextStorage:[newTextStorage retain]];
And yet this causes memory leaks...
This is giving me a big headache. If anybody could
give me some advice, or show me the best way to swap
in and out a text view's text storage so that it
doesn't affect any other text views and so that the
text storage isn't over-released, I would be very
grateful.
Many thanks in advance,
Keith
____________________________________________________
Start your day with Yahoo! - make it your home page
http://www.yahoo.com/r/hs
_______________________________________________
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