Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Observer Pattern: Dangling References
Message
General information
Forum:
Visual FoxPro
Category:
Object Oriented Programming
Miscellaneous
Thread ID:
00354611
Message ID:
00355134
Views:
24
Hi David,

I'd tried the detach method below in the subject object. I was calling the subject.detach from the observer.destroy. It seemed no matter what I did with the subject's zorder it always got destroyed first. I'm not using pemstatus() or getpem().
*subject.detach method
lparameter cObserverName
local cObserverName
for x = 1 to alen(This.aObservers)
	if this.aObservers(x).Name = cObserverName
		this.aObservers(x) = .null.
	endif
next 
As it stands, I've duplicated the above code (without the conditional) in a method called DetachAll, and I'm calling it from the form.destroy per Nancy's suggestion. It works, but seems to break from the pattern as defined by G&H.

Yes, the .null. was added in desperation, not unlike looking in the refridgerator for your lost keys ;-)

>Ken,
>
>If you are going to have an Attach() method, you should have a corollary Detach() method so that an observer can unlink from the item it is observing. You may want to give each of your objects a unique key to make it easier to determine which object is requesting to be detached. When your observer destructs it can request it be Detached, iff the observed item still exists. The observed item should be able to this.aObsevers = .null. if that doesn't seem to be working you might explicitly loop through the array setting each array member to .null.
>
>Are you positive there are no other references that are dangling? Are you using PEMSTATUS() or GETPEM() these are known to under specific conditions cause internal reference count increases that we have absolutely no control over.
>
>You don't have to .null. the oObserver that is marked as lparameter.
>
>>I'm working on an observer pattern utilizing a listbox as the subject, and custom classes (textbox & label) as observers. I'm using the following Attach method in the listbox:
>>
lparameter oObserver
>>
>>if type('This.aObservers(1)') # 'O'
>>	x = 1
>>else
>>	x = alen(this.aObservers) + 1
>>endif
>>
>>dime this.aObservers(x)
>>this.aObservers(x) = oObserver
>>oObserver = .null.
>>
>>I can not seem to get rid of these references. When and where should I be clearing them? I've been trying to do it in the destroy event to no avail. I must be missing something...
kenweber
GCom2 Solutions
Microsoft Certified Professional

Previous
Next
Reply
Map
View

Click here to load this message in the networking platform