Wrox Programmer Forums
|
VB Components Issues specific to components in VB.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Components section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 20th, 2004, 10:53 AM
Registered User
 
Join Date: Mar 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Com context


I have following code:

Function oFunctionRS(ByRef oDbConnection) As ADODB.Recordset

      .............
      some code
      .............


      Set oTempRS = oDbConnection.Execute(sSQL)
        Set oFunctionRS = oTempRS
      .............

        oContext.SetComplete

        Set oTempRS = Nothing
        Set oContext = Nothing

ExitHandler:

        Set oTempRS = Nothing
        Set oContext = Nothing

        Exit Function

ErrorHandler:

      oContext.SetAbort

      .............
      some error logging code
      .............

        ' allow resume next after error
        Resume ExitHandler

End Function

Should I clear oTempRS and oContext in exithandler and in a main code, or
only in exithandler?
I also have seen a lot of examples in internet, where functions have only oContext.SetAbort
or oContext.SetComplete and there is no Set oContext = Nothing.
So I also don't understand - should I Set oContext = Nothing in a main function code, or
after oContext.SetComplete or oContext.SetAbort it somehow clears automatically?

Thanks for any suggestions.






 
Old March 22nd, 2004, 02:13 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What

Set mYVariable = nothing

does, is to decrese the reference count of the myVariable object. myVariable will be destroyed only the rerefence count goes to zero. In your case

Set oFunctionRS = oTempRS

increases the reference count of oTempRS, therefore even though in your code you set oTempRS to nothing that will NOT be destroy oTempRS, because someone else (in this case oFunctionRS) is still using it.

Objects are set to nothing automaticall when they go out of scope. Local variables when the local method exits, global variables when the module they belong to terminate etc. I usually set objects to nothing only when I am done with them and I just want to release the memory; but because I like to write small methods with short lifespan, I do not bother.

Hope this helps (I know is quite of confusing)

Marco
 
Old March 23rd, 2004, 08:34 AM
Registered User
 
Join Date: Mar 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Thanks, this things are really confusing.

 
Old June 24th, 2004, 08:57 AM
Authorized User
 
Join Date: May 2004
Posts: 83
Thanks: 0
Thanked 1 Time in 1 Post
Default

Remember as a rule of thumb to always destroy objects explicitly as soon as u r finished with them. It is all about to be on the safe side.

In the code excerpt you have set objects to Nothing twice: once in the main code and again in the ExitHandler. Whether an error occurs or not the code in the ExitHandler will execute. So, for that reason, eliminate the object destruction code from the main code and place it only in the ExitHandler.

Also note that setting oTempRS to Nothing will not release the resources occupied by this object coz another object reference, namely oFunctionRS, now points to the same object. All the statement 'Set oTempRS = Nothing' does is destroy the reference oTempRS. The actual resources will be relaesed only when all the references to the object r destroyed or go out of the scope.

I hope this discussion should help.

ejan.

ejan





Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to set context in web.xml or context.xml. dchicks Apache Tomcat 1 March 7th, 2008 07:59 AM
Context Menu ? zoltac007 BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 1 June 7th, 2006 05:44 PM
Context User davidroth BOOK: ASP.NET Website Programming Problem-Design-Solution 0 April 14th, 2004 12:49 AM
context help smukher Visual C++ 0 June 19th, 2003 08:09 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.