construction/deletion of global variables in a com component 
Author Message
 construction/deletion of global variables in a com component

developing with visual studio, Visual C++ 6.0, win2000

Hello,
I have made a new atl project to make a com-ponent packaged in a dll.

I also have made a few global variables and put them in the cpp file that
implements the com class.

When will the destructor of these global variables be called?

Greetings,

----------------------------------------------------------------------------
----------------------

other loosely related question: how to use cached data in a coclass? Is it
good practice to complex C++ objects as global variables in an com
component?
--------------------------------
Besides, i have a more general question concerning desing of com components.
For i guess my com class may have been designed badly.

I am new to developing in com, and i am trying to convert an old-fashioned
executable into a com component. My old executable initialized all kinds of
variables at startup ( file pointer to write to a log file, some very small
database tables that were kept in memory for performance reasons, an open
oracle database session, ... ) and then waits for requests and answers these
requests. On exit, this program frees its oracle session, closes its logfile
etc.

To make this executable a coclass (_ATL_APARTMENT_THREADED), i have
proceeded like this:
i have made a few global variables, and an interface exporting one methoda
function to answer requests. (gets in some strings, numbers, recordsets and
returns a recordset.)
The first time a request is received, it sees that the global variables have
not been initialized yet, and it initializes them before answering the
request. The second time a request is received, the request can be answered
more quickly using cached data in the global variables. Does this look sound
practice to you?
And I wonder when these global variables are destroyed. For on destruction
of for instance a C++ global object encapsulating a open file stream to an
open log file, a goodbye message should be written in that log file. And i
don't see that message.

----------------------------------------------------------------------------
----------------------



Mon, 24 May 2004 01:25:10 GMT  
 construction/deletion of global variables in a com component
Hello Filip,

Quote:
> I have made a new atl project to make a com-ponent packaged in a dll.
> I also have made a few global variables and put them in the cpp file that
> implements the com class.
> When will the destructor of these global variables be called?

    Usually global variable constructors/destructors are called by C runtime
library during image loading/unloading process. System calls CRT-provided
entry-point function, which prepares library, and after initializes all
global variables, then CRT calls application XXXMain function.

     ATL project compiled without CRT support ( e.g. ATL with _ATL_MIN_CRT
defined ) implements own entry point function that doesn't call
constructors/destructors for global variables, [1]. Try removing
_ATL_MIN_CRT definition from "Project | Settings | C++ | Preprocessor
definitions" edit box.

    Another possible solution is storing global variables as pointers or raw
structures (as it's done for _Module global variable), but they should be
initialized manually. There is CComObjectRootEx::ObjectMain static function
for such purposes. It's called by ATL framework ( inside
_Module.Init/Term ). Override ObjectMain and place there
initialization/deinitialization code for class-related global data.

BTW all global variables should be synchronized against concurrent access.

[1] MSDN Knowledge Base Q166480 - "INFO: Active Template Library (ATL)
Frequently Asked Questions."

Quote:

> Greetings,

> --------------------------------------------------------------------------
--
> ----------------------

> other loosely related question: how to use cached data in a coclass? Is it
> good practice to complex C++ objects as global variables in an com
> component?
> --------------------------------
> Besides, i have a more general question concerning desing of com
components.
> For i guess my com class may have been designed badly.

> I am new to developing in com, and i am trying to convert an old-fashioned
> executable into a com component. My old executable initialized all kinds
of
> variables at startup ( file pointer to write to a log file, some very
small
> database tables that were kept in memory for performance reasons, an open
> oracle database session, ... ) and then waits for requests and answers
these
> requests. On exit, this program frees its oracle session, closes its
logfile
> etc.

> To make this executable a coclass (_ATL_APARTMENT_THREADED), i have
> proceeded like this:
> i have made a few global variables, and an interface exporting one methoda
> function to answer requests. (gets in some strings, numbers, recordsets
and
> returns a recordset.)
> The first time a request is received, it sees that the global variables
have
> not been initialized yet, and it initializes them before answering the
> request. The second time a request is received, the request can be
answered
> more quickly using cached data in the global variables. Does this look
sound
> practice to you?
> And I wonder when these global variables are destroyed. For on destruction
> of for instance a C++ global object encapsulating a open file stream to an
> open log file, a goodbye message should be written in that log file. And i
> don't see that message.

> --------------------------------------------------------------------------
--
> ----------------------



Mon, 24 May 2004 05:57:53 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Global variable or not global variable

2. COM Global variables

3. COM Global variables

4. Global variable in COM

5. Construction order of members/global objects

6. Uninitialized global data (was Re: Global variables)

7. use local variables in a switch construction

8. static local variables vs global variables

9. Variables & Global Variables

10. global variables and external variables?

11. COM Component w/o Component Services

 

 
Powered by phpBB® Forum Software