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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Objects - set to Null or RELEASE? 1

Status
Not open for further replies.

dbMark

Programmer
Apr 10, 2003
1,515
US
I see that in many examples and usage that objects are set to Null when we are done with them. But when I tested the TYPE() it was still "O" for Object. So why would we set the object to Null and not RELEASE the object instead? At least then I would have an easy way to check if it existed or not. Or is there some better way to detemine an object set to Null isn't really functional any more?

I just need to test whether it is really capable of being used. If I just set to Null then I can't use TYPE(). Or do you recommend testing for something within the object? I really want a generic way to make this test.

Code:
objMail = CREATEOBJECT("CDO.Message")
? TYPE("objMail")
  O
objMail = Null
? TYPE("objMail")
  O
RELEASE objMail
? TYPE("objMail")
  U
 
Try using ISNULL() or VARTYPE() instead of TYPE().
ISNULL() will return a .T. or .F. and VARTYPE() will return 'X' if it is .NULL.

I could be off base, but in the case of an object, my understanding is when you redefine a variable as .NULL. or reassign it that value, it releases the associated active-x or application which would be instantiated from creating the object, and frees up the extra memory used.
Just releasing the variable may not free up the extra resources in use.


-Dave Summers-
[cheers]
Even more Fox stuff at:
 
Thanks, just what I needed to know (the obvious solution).

I also just discovered that if I had the object declared as public or local, then release it, then create it again, it is now private. So releasing it also wipes out the public/local status and reverts to the default private scope.
 
DBMark,

If I may say so, you sound slightly confused about the fact that TYPE() is returning "O" even though the variable is NULL.

The thing about NULL is that it is not a data type. Any variable can be of any data type and can also be NULL. "Object" is a data type, so it's perfectly possible for a variable to be of type "object" and be NULL at the same time. That's why ISNULL() can return .T. and Type() can return "O" for the same variable.

Keep in mind also that an object is automatically destroyed when all references to it go out of scope. Within a method, if you do loObject = CREATEOBJECT("MyClass"), the object will cease to exist at the end of the method (unless you previously copied loObject to another, non-local, variable). There's no need to explicitly release it or set it to NULL.

In general, the only times I explicitly release an object is if it is instantiated globally and I don't want it to hang around in the development environment, or if an object reference is stored in the property of another object that has a wider scope. There might be other times as well, but it's something I do generally.

Hope this helps.

Mike




__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Yes, I'm from the old school DOS days, so OOP and the extra data types and null signifying no data is like an add-on for me. Newer concepts and all that. But getting better all the time.

As for TYPE() versus VARTYPE(), there are some things each does better than the other and in this case the newer VARTYPE() is better.
 
Code:
myObj = CREATEOBJECT("CDO.Message")
? TYPE("myObj")  && "O"
? VARTYPE(myObj) && "O"
? ISNULL(myObj)  && .F.
myObj = Null
? TYPE("myObj")  && "O"
? VARTYPE(myObj) && "X"
? ISNULL(myObj)  && .T.
RELEASE myObj
? TYPE("myObj")  && "U"
? VARTYPE(myObj) && "U"
? ISNULL(myObj)  && {invalid, errors}
While there are multiple factors to consider, such as persistence between program levels and objects, I have determined that the generic test for my needs is:
Code:
? VARTYPE(myObj) $ "UX"
and second place goes to:
Code:
? TYPE("myObj") = "U" OR (TYPE("myObj") = "O" AND ISNULL(myObj) = .T.)
If I am sure it was previously instantiated then this simpler code would suffice:
Code:
? VARTYPE(myObj) = "X"
and second place goes to:
Code:
? TYPE("myObj") = "O" AND ISNULL(myObj) = .T.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top