Search code examples
comolepowerbuilder

Is it expensive to connect to COM objects?


I'm actually involved into a legacy system maintenance that has been written in PowerBuilder.

My actual objective is to handle the errors that may occur when connecting to COM objects.

The COM objects have been written in VB.NET and are mainly Web services.

I have defined a base factory which handles the instantiation of the COM objects through the PB function ConnectToNewObject().

So, when I instantiate a COM object, I get an instance of an oleobject.

In the code to be replaced, I can see that the oleobject used may be immediately destroyed. In some other places, it isn't. To add to the confusion, sometimes it is previously disconnected.

So I can encounter either:

oleobject lole_new_com_object
lole_new_com_object = create oleobject
lole_new_com_object.ConnectToNewObject("MyAssembly.MyNamespace.MyClass")

...

lole_new_com_object.DisconnectObject() // Disconnect before destroy...
destroy lole_new_com_object

or:

oleobject lole_new_com_object
lole_new_com_object = create oleobject
lole_new_com_object.ConnectToNewObject("MyAssembly.MyNamespace.MyClass")

...

                                      // No disconnect...
destroy lole_new_com_object

So I wonder whether it is expensieve to instantiate an object this way.

  1. Would it be best to disconnect before any destroy?
  2. Would it be best to destroy any unreturned oleobject?
  3. Would it be best to define them as singletons?

I am quite comfortable in PowerBuilder, though I am no expert. For such concerns, I don't know how PB handles things.


Solution

  • From the official documentation here: Shutting down and disconnecting from the server

    You can rely on garbage collection to destroy the OLEObject variable. Destroying the variable automatically disconnects from the server.

    It is preferable to use garbage collection to destroy objects, but if you want to release the memory used by the variable immediately and you know that it is not being used by another part of the application, you can explicitly disconnect and destroy the OLEObject variable

    So you can keep the destroy w/o the disconnect. As for the destroy itself, it depends on the type of object. You don't have to do it, especially for in-process objects. Usually, it's used for COM objects such as Microsoft Word or Excel (when you want to make sure the exe is really gone when you want it to be gone).