Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dangling object references

Status
Not open for further replies.

Mike Lewis

Programmer
Jan 10, 2003
17,516
Scotland
I feel this should be simple, but I can't get my head round it.

I have a class which creates an object reference to another class (a timer). In doing so, it passes an object reference to itself to the timer.

In other words, the code looks like this (highly simplified):

Code:
goMyObject = CREATEOBJECT("MyClass")

* Other processing here

RELEASE goMyObject

RETURN

DEFINE CLASS MyClass AS Session

oTimer = NULL

FUNCTION INIT
THIS.oTimer = CREATEOBJECT("MyTimer", THIS)
ENDFUNC

FUNCTION DESTORY
THIS.oTimer = NULL
ENDFUNC

ENDEFINE

DEFINE CLASS MyTimer AS Timer

oCaller = NULL

FUNCTION INIT
LPARAMETER toCaller
THIS.oCaller = toCaller
ENDFUNC

FUNCTION DESTROY
THIS.oCaller = NULL
ENDFUNC

ENDDEFINE

This is all working fine, except that I can't release the object based on MyClass. I can see RELEASE goMyObject being executed in the debugger, but the Destroy doesn't get executed, and the object is still in memory when the program is ready to close.

Clearly, the fact that the main class stores a reference to the timer, and the timer stores a reference to the main clause, is upsetting VFP. But I can't see how to get round it.

Any insights will be appreciated.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 

Mike, it seems that you misspelled 'DESTROY' (' FUNCTION DESTORY') in the function name of MyClass. Could it be the reason?
 

Dave,

No, I didn't omit it from the example. I was assuming that setting the object reference to NULL would destroy it in the same way as calling its Release method.

Have I got that wrong?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 

Dave,

I just tried your suggestion, but a timer doesn't appear to have a Release method. In fact, isn't it just forms and toolbars that have a Release method?

Presumably, I can't do RELEASE THIS.oTimer for the same reason.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Mike,
Once your Object is reduced to a .NULL. value, it can be released as a memory variable, with RELEASE <memvarname>, which will make it go away. You don't need the reference to the object, just the name of your reduced variable.



Best Regards,
Scott

&quot;Everything should be made as simple as possible, and no simpler.&quot;[hammer]
 
Scott,

Once your Object is reduced to a .NULL. value, it can be released as a memory variable, with RELEASE <memvarname>,

Thanks for that, but I don't think that would work.

The problem lies in the fact that the object reference in question is stored in a property of the timer. So, based on what you are saying, I would have to do this:

Code:
FUNCTION DESTROY
THIS.oCaller = NULL
[b]RELEASE THIS.oCaller[/b]
ENDFUNC

But I don't think you can release a property like that - only a variable. 

In any case, the execution doesn't get to that point. When I try to release the main object (RELEASE goMyObject), the debugger shows the command being executed, but neither of the Destroy methods (of the main object or the subsidiary timer) is being called. 

What's really puzzling is that I can't recall this ever happening before, and yet I wouldn't have thought I was doing anything particularly unusual.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

[url=www.ml-consult.co.uk] My Visual FoxPro site: www.ml-consult.co.uk[/url]
 

Dave,

Thanks for your reply.

REMOVEPROPERTY() didn't do anything. And THIS.RemoveObject("MyTimer") wouldn't work because RemoveObject() only applies to containers -- it's the opposite of AddObject().

However, you've given me a clue to a possible workaround.

At present, my main object is based on a Session class. It does a CREATEOBJECT() on the timer at run time, and stores a reference to it. Perhaps what I should have done in the first place was to base my main object on a Container class, and add the timer to it at design time.

Come to think of it, that's how I usually design this sort of class, and have never had this problem before.

Unless someone can suggest anything better, I'll experiment in that direction.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Mike,
Please let us know the outcome on this... am very curious to see what you turn up.
-Scott


Best Regards,
Scott

&quot;Everything should be made as simple as possible, and no simpler.&quot;[hammer]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top