From: brad@meaddata.com (Brad Craig)
Subject: Question about global object deallocation.
Organization: Mead Data Central, Dayton OH
Date: Thu, 23 Jul 1992 17:38:21 GMT

Here is an example program that I wrote to test some strangeness
I was encountering with a String class and some global const
String objects that were defined. I simplified the problem by
creating the following header file and source files ...

--------------------------- Int.h --------------------------
#ifndef INTEGER_H
#define INTEGER_H

class Integer;

extern const Integer Zero;
extern const Integer One;
extern const Integer Two;
extern       Integer Three;
extern       Integer Four;

class Integer {
  public:
    Integer(const int i = 0);
    ~Integer(void);
  private:
    int _i;
};

#endif
--------------------------- Int.h --------------------------

--------------------------- Int.C --------------------------
#include <iostream.h>

#include "Int.h"

const Integer Zero;
const Integer One(1);
const Integer Two = 2;
      Integer Three(3);
      Integer Four = 4;

Integer::Integer(const int i) : _i(i)
{
    cout << "Integer::Integer(const int " << _i << ")" << endl;
}

Integer::~Integer()
{
    cout << "Integer::~Integer() i = " << _i << endl;
}
--------------------------- Int.C --------------------------

--------------------------- main.C --------------------------
#include "Int.h"

main()
{;}
--------------------------- main.C --------------------------

When these files are compiled, linked an executed
the following output appears ...

Integer::Integer(const int 0)
Integer::Integer(const int 1)
Integer::Integer(const int 2)
Integer::Integer(const int 3)
Integer::Integer(const int 4)
Integer::~Integer() i = 4
Integer::~Integer() i = 3
Integer::~Integer() i = 0

So my question is why doesn't _One_ and _Two_ get deallocated? 
I looked at the translated 'C' code and here is the code that
initializes and deallocates the globals ...

--------------------------- Int..c --------------------------

[ irrelevant stuff deleted ]

char __sti__Int_C___dt_ ()
{ __ct__13Iostream_initFv ( & iostream_init ) ;

__ct__7IntegerFCi ( (struct Integer *)(& Zero ), (int )0 ) ;
__ct__7IntegerFCi ( (struct Integer *)(& One ), (int )1 ) ;
__ct__7IntegerFCi ( (struct Integer *)(& Two ), (int )2 ) ;
__ct__7IntegerFCi ( & Three , (int )3 ) ;
__ct__7IntegerFCi ( & Four , (int )4 ) ;

}

char __std__Int_C___dt_ ()
{ __dt__7IntegerFv ( & Four , 2) ;

__dt__7IntegerFv ( & Three , 2) ;

__dt__7IntegerFv ( (struct Integer *)(& Zero ), 2) ;

__dt__13Iostream_initFv ( & iostream_init , 2) ;

}
static struct __linkl { struct __linkl * next;
char (*ctor)(); char (*dtor)(); } __link = 
{ (struct __linkl *)0, __sti__Int_C___dt_ ,__std__Int_C___dt_ };


[ irrelevant stuff deleted ]

--------------------------- Int..c --------------------------

The __dt__7IntegerFv ( (struct Integer*)(& One ), 2), and
__dt__7IntegerFv ( (struct Integer*)(& Two ), 2) dont get inserted. 
Is there a reason that I am not seeing as to why cfront knows
to allocate _One_ and _Two_ but not to deallocate them?

Additionally, If I remove the external declarations from _Int.h_
then _One_ and _Two_ are deallocated.

This was tested with cfront 3.0.1 and cfront 2.1.0 and the same
results occurr. The above _Int..c_ is from cfront 3.0.1.

Thanks in advance for any help.


-- 
Brad Craig        | "This generally results in   |             (513) 865-1536
Mead Data Central |  heap insanity, and ultimate |Common Object Library Group
P.O. Box 933      |  abnormal program death"     |          brad@meaddata.com
Dayton, OH 45401  |    -- James O. Coplien       |    ...!uunet!meaddata!brad
Newsgroups: comp.lang.c++