Solved

Scope of new ActiveXObject... when is it destructed?

Posted on 2003-11-22
9
1,050 Views
Last Modified: 2012-05-04
I wrote a COM object in C++ and and it uses some resources.  Now in my JScript code I have fns like:

function DoSomething() {
     var oMyObj= new ActiveXObject( "MyApp.MyObject" );
     var nCnt= oMyObj.CountItems();
     return nCnt;
}

The problem is that the Browser does not seem to release the resouces until I close or refresh the webpage.  In my COM object, my destructor pops up a message box to indicate that it is destructing (and freeing the resources).  But that message box does not pop up as expected.    Commands like

       delete oMyObj;
or
       oMyObj= void;  // I've see oMyObj=nothing in VB...
or
       allowing the the object to go out of scope (end of function)

do not invoke the destructor.

For now, I added a oMyObj.Close() so that I could manually free up the resource.  But I'd rather know the exact rules of when the destructor will be called and do this right.  Can anyone help?

-- Dan
0
Comment
Question by:DanRollins
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 1

Accepted Solution

by:
cj_nepenthe earned 200 total points
Comment Utility
Hi Dan,

"oMyObj = null" should release the reference to your object, but it's difficult to predict exactly when it'll be destoryed. JavaScript uses automatic garbage collection, so when the object is freed really depends on the implementation.
Sorry I can't be more help!  It's probably best to stick with the close() method approach.
0
 
LVL 10

Assisted Solution

by:NetGroove
NetGroove earned 250 total points
Comment Utility
Hello Dan,

one additional note.
If you write, like you did, a var clause in front of your object assignment, then is that object reference created locally to the lifetime of that block where it is defined, in your case is the block the function body block.
Without the var would the lifetime of the object be global for the page (window, document level).

Also releasing a reference to an object does not immediately invoke the destructor. It only calls the Release counter of the object to reduce the reference count to that object.

0
 
LVL 10

Assisted Solution

by:NetGroove
NetGroove earned 250 total points
Comment Utility
Hey, perhaps you should invoke your destructor from the Release method call!
0
 
LVL 7

Expert Comment

by:Xxavier
Comment Utility
"If you write, like you did, a var clause in front of your object assignment, then is that object reference created locally to the lifetime of that block where it is defined, in your case is the block the function body block."

    Yes the object is no longer accessible after the completion of the function call but that does not mean the resources (ie space in RAM) it had used is now available for use by the program, it has become garbage as cj_nepenthe pointed out.

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 49

Author Comment

by:DanRollins
Comment Utility
I added code like this (which I copied from ATLCOM.H) to my C++ object definition.

      ULONG InternalRelease()
      {
            AfxMessageBox("CMyObj Release");
            ATLASSERT(m_dwRef > 0);
            return _ThreadModel::Decrement(&m_dwRef);
      }
      void FinalRelease()
      {
            AfxMessageBox("CMyObj Final Release");
      }

FinalRelase is called when I close the browser.  Release is called many times -- but none of them when I'm expecting them!  Not at the end of the scope, and not after delete and not after obj=null

It is true that if I use (in JScript) {

      var oMyObj= null;
      function DoSomething() {
            oMyObj= new ActiveXObject( "MyApp.MyObject" );
      ...

That  I can minimize the number of times Release gets called (obviously when var is used inside of the function, a new instance is created each time the fn is called).  I found an MDSN INFO reference that describes how to force garbage collection in VBScript, version 1.1 by allocating a huge string -- but that is a total kludge.

It looks like I'll need to close the database connection after each use becasue I don't feel right about my customers needing to call Close() or Cleanup() functions all over the place.

-- Dan
0
 
LVL 7

Expert Comment

by:Xxavier
Comment Utility
  With the var then there are certainly going to be multiple instances hanging around before garbage collection but I am not sure (prepared to be convinced otherwise) that removing the var reduces  the problem. Assigning an identifier to an object reults in that identifier being a pointer, (essentially), to that object so the statement in the function (no var)

         oMyObj= new ActiveXObject( "MyApp.MyObject" );

would result in a new object being created and the resources used would increase unless the new instance overwrote the old instance, but I am not sure it does this for suppose I had

       oMyObj= new ActiveXObject( "MyApp.MyObject" );
       oMyObj1=oMyObj;
       oMyObj= new ActiveXObject( "MyApp.MyObject" );

then what is oMyObj1 pointing to? Unless the implementation checks to see if the oldobject pointed to by oMyObj has no other references. My guess this is system dependent.
       

           
0
 
LVL 7

Expert Comment

by:Xxavier
Comment Utility
Dam it muddled thinking oMyObj1 will point to the new object, but the point still stands resources will increase unless the old instance is overwritten, I am not sure this is always the case.
0
 
LVL 7

Assisted Solution

by:Xxavier
Xxavier earned 50 total points
Comment Utility
I was right in the first place they do act as pure pointers to the resource. This results in a red background with the date in seconds in the text box. So reassigning the identifier can not clear the resource unless the implemtation can check to see if there are no other pointers to it, and it will have to wait for garbage collection.

<form name="F1">
  <input type="text" name="T1">
</form>
<script>
  obj=new Date()
  obj1=obj
  obj=document
  obj.bgColor="red"
  obj.F1.T1.value=obj1.getTime()
</script>

0
 
LVL 49

Author Comment

by:DanRollins
Comment Utility
No "magic bullet" solution that I was hoping to get, but verification of my fears.  

I ended up closing the database connection between calls.  I found that if I just keep one connection open, the opens and closes are very fast, so it does not seem to be a hardship.  I also added a Close() member function to allow a manual way to force closing when the resourse usage is tight (I can only have 10 concurrent connections with this runtime DBMS).

-- Dan
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now