Re: Removing Observers eats up memory
Re: Removing Observers eats up memory
- Subject: Re: Removing Observers eats up memory
- From: Markus Spoettl <email@hidden>
- Date: Mon, 25 Jan 2016 20:05:03 +0100
On 25/01/16 18:47, Ken Thomases wrote:
When the array changes, I remove all observers from these objects before
re-observing the new objects.
If at all possible, you should do this in a more targeted fashion. Add and
remove observers only from the objects which were actually added or removed
from the array. If you have properly encapsulated modifications of the array
in indexed collection mutating accessors, this is trivial.
OK, that's certainly an option, but the brute force way it is currently working
with surely should work, too, and not cause the symptoms I see.
Has anyone any idea how the removing of observers can cause this kind of
death spiral?
You should use Instruments to trace exactly what's going on during one of the
runaway cases.
When I break in the debugger I end up in various different library functions
concerned with hash tables and the like, such as
#0 0x00007fff9ddc6d03 in weak_entry_for_referent(weak_table_t*, objc_object*) ()
#1 0x00007fff9ddc84b6 in weak_read_no_lock ()
#2 0x00007fff9ddc8473 in objc_loadWeakRetained ()
#3 0x00007fff9ddc861f in objc_loadWeak ()
#4 0x00007fff9cbc81c5 in hashProbe ()
#5 0x00007fff9cbcbf3a in -[NSConcreteHashTable rehashAround:] ()
#6 0x00007fff9cbc8245 in hashProbe ()
#7 0x00007fff9cbc8014 in -[NSConcreteHashTable addObject:] ()
#8 0x00007fff9cbf3f48 in _NSKeyValueObservationInfoCreateByRemoving ()
#9 0x00007fff9cbf3933 in -[NSObject(NSKeyValueObserverRegistration)
_removeObserver:forProperty:] ()
#10 0x00007fff9cbf3837 in -[NSObject(NSKeyValueObserverRegistration)
removeObserver:forKeyPath:] ()
#11 0x00000001002e71f2 in -[TrackEditorViewController
unregisterObserverForDataPoint:] at
/Users/markus/Projects/rubiTrack/src/TrackEditorViewController.m:639
Not sure which instrument would help me finding out what is going on. I was
using the Time Profiler but it didn't really help, partly because it's really
difficult to know if I'm hitting the bug or not (it doesn't happen every time).
Also recreating the issue under the debugger is much easier.
By the way, you should consider implementing the observationInfo property on
your observed class. This avoids KVO storing observation info in a side
table. Note that the property is a void pointer, not an object pointer, and
therefore does no memory management.
This is interesting. When I implement this property in the proxy object, the
problem goes away (at least I wasn't able to duplicate it so far). Turn off the
implementation, it comes back. Not really sure what this tells me, though.
Regards
Markus
--
__________________________________________
Markus Spoettl
_______________________________________________
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