Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Can an ActiveX dll know its been 'freed'


This question is answered.


Permlink Replies: 4 - Last Post: Dec 2, 2014 4:19 AM Last Post By: Ian Patterson
Ian Patterson

Posts: 3
Registered: 8/8/10
Can an ActiveX dll know its been 'freed'  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 28, 2014 1:33 AM
We have a COM (ActiveX) dll that uses opens and uses a database.
When a calling application 'free's' the dll, can the dll know this so that it can destroy some objects it has created.
I am concerned about possible memory leaks.
Alternatively can I be certain that all objects created by the dll will be destroyed automatically?
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Can an ActiveX dll know its been 'freed'  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 28, 2014 2:39 PM   in response to: Ian Patterson in response to: Ian Patterson
Ian wrote:

We have a COM (ActiveX) dll that uses opens and uses a database.

When a calling application 'free's' the dll

It is not supposed to do that directly. COM DLLs should only be loaded via
CoCreateInstance() or equivilent, and then COM will handle releasing the
DLL when it is safe to do so.

can the dll know this so that it can destroy some objects it has created.

A COM DLL is supposed to export a DllCanUnloadNow() function:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms690368.aspx

Specifically to hand the situation where the DLL has active objects that
need to be released before COM is allowed to unload the DLL from memory.
Normally, DllCanUnloadNow() returns S_FALSE if the DLL has given out any
COM objects that have not been released yet. But you can customize DllCanUnloadNow()
to track other things as well, like your database connections. On the other
hand, if your DLL's COM object is managing the database, then it should be
releasing the database when itself is released. And COM should not be unloading
your DLL until your COM object has been released first.

I am concerned about possible memory leaks.

You are implementing proper reference counting on alll of your COM objects,
that will not be a problem.

Alternatively can I be certain that all objects created by the dll
will be destroyed automatically?

When an app asks your DLL for a COM object, it is the app's responsibility
to release that object when done using it.

--
Remy Lebeau (TeamB)
Ian Patterson

Posts: 3
Registered: 8/8/10
Re: Can an ActiveX dll know its been 'freed'  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 30, 2014 9:43 AM   in response to: Ian Patterson in response to: Ian Patterson
Ian Patterson wrote:
We have a COM (ActiveX) dll that uses opens and uses a database.
When a calling application 'free's' the dll, can the dll know this so that it can destroy some objects it has created.
I am concerned about possible memory leaks.
Alternatively can I be certain that all objects created by the dll will be destroyed automatically?

Thank you for your quick response and detailed answers. I am still a little confused as to the releasing of the dll.

Here is my pseudo code:
The COM dll is called ch_com.dll. Assume it has 1 exported function called Validate. This function uses a database to find a record.
V is a variant defined in a procedure on a form.
The calling application only creates 1 instance of the dll:
V := CreateOleObject('ch_com.ch_com') ;
then SomeReturnInfo := V.Validate('somedata') ;

The call to Validate can be performed any number of times.

So to make my question more explicit:
a. If this application is the only application to call the dll, when the application terminates, Windows frees all memory in the dll ?
b. If another application creates and instance of the dll before first application terminates, does the second application cause the dll to free memory, or is each creation of the dll handled totally separately?
Many thanks
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Can an ActiveX dll know its been 'freed'
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 1, 2014 9:49 PM   in response to: Ian Patterson in response to: Ian Patterson
Ian wrote:

Thank you for your quick response and detailed answers. I am still
a little confused as to the releasing of the dll.

Let COM worry about it for you.

The calling application only creates 1 instance of the dll:

V := CreateOleObject('ch_com.ch_com') ;

COM will load the DLL into the calling process, if not already loaded. Then
a new instance of the COM object is created, with a reference count of 1,
and returned from CreateOleObject() as an IDispatch. When assigned to V,
the reference count will increase to 2, then the IDispatch will go out of
scope, decrementing the reference count back to 1. While the reference count
is > 0, the Dll's DllCanUnloadNow() method should be returning S_FALSE whenever
COM calls it.

then SomeReturnInfo := V.Validate('somedata') ;

No change to the reference count.

The call to Validate can be performed any number of times.

That is perfectly fine. Eventually, V will go out of scope, at which time
the reference count will fall to 0, freeing the COM object, and allowing
DllCanUnloadNow() to return S_OK whenever COM calls it.

If this application is the only application to call the dll

Since it is a DLL, every process that uses the COM object will get its own
local copy of the DLL in memory. It doesn't matter how many copies are running,
unless they share global memory.

when the application terminates, Windows frees all memory in the dll ?

Normally, yes. Whether or not the COM object has been released correctly,
Windows unloads all DLLs that are still loaded in the process.

If another application creates and instance of the dll before first
application terminates, does the second application cause the dll to
free memory, or is each creation of the dll handled totally
separately?

Each process's local copy of the DLL is totally separate from every other
process's copy.

--
Remy Lebeau (TeamB)
Ian Patterson

Posts: 3
Registered: 8/8/10
Re: Can an ActiveX dll know its been 'freed'  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 2, 2014 4:19 AM   in response to: Ian Patterson in response to: Ian Patterson
Ian Patterson wrote:
We have a COM (ActiveX) dll that uses opens and uses a database.
When a calling application 'free's' the dll, can the dll know this so that it can destroy some objects it has created.
I am concerned about possible memory leaks.
Alternatively can I be certain that all objects created by the dll will be destroyed automatically?

Many thanks again for the detailed reply.
I think I now understand the process much better.
Regards, Ian
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02