statics destructing and g++

statics destructing and g++

Post by Demetriu » Wed, 11 Aug 1999 04:00:00



How does g++ know when to destruct statics?  Here's why I ask...
I have:
template<class T>
class CList
{
    private:
        class CNode
        {
                static CMemoryAllocator<T> m_Memory;
                public:
                    void *new(size_t size) {return m_Memory.Allocate();}

                    void delete (void *ptr) {m_Memory.Deallocate(ptr);}
        };
    //a simple list implementation
    ~CList() {Destroy();}

Quote:};

Here's the problem
I have a static instance of CList<SomeOtherClass> which in turn makes a
static instance of CMemoryAllocator<CList<someOtherClass>::CNode>
the problem is that
CMemoryAllocator<CList<someOtherClass>::CNode> goes out of scope (is
destructed),
before CList<SomeOtherClass> is destructed... and the destructor of
CList relies on CMemoryAllocator.

Basically they are destructing in the reverse order I need them to.  So
what is the mechanism that g++ uses to destruct statics.

thanks in advance
Demetrius

 
 
 

statics destructing and g++

Post by Kelly Burkhar » Thu, 12 Aug 1999 04:00:00



> How does g++ know when to destruct statics?  Here's why I ask...
> I have:
> template<class T>
> class CList
> {
>     private:
>         class CNode
>         {
>                 static CMemoryAllocator<T> m_Memory;
>                 public:
>                     void *new(size_t size) {return m_Memory.Allocate();}

>                     void delete (void *ptr) {m_Memory.Deallocate(ptr);}
>         };
>     //a simple list implementation
>     ~CList() {Destroy();}
> };

> Here's the problem
> I have a static instance of CList<SomeOtherClass> which in turn makes a
> static instance of CMemoryAllocator<CList<someOtherClass>::CNode>
> the problem is that
> CMemoryAllocator<CList<someOtherClass>::CNode> goes out of scope (is
> destructed),
> before CList<SomeOtherClass> is destructed... and the destructor of
> CList relies on CMemoryAllocator.

> Basically they are destructing in the reverse order I need them to.  So
> what is the mechanism that g++ uses to destruct statics.

> thanks in advance
> Demetrius

I can't answer specific to g++, but in general the order of
construction and destruction of static objects is unspecified.  All
you know for sure is that construction occurs before main and
destruction occurs after.

I'm not sure what the standard says about statics within methods like
m_Memory in your example above.  It wouldn't surprise me if the
standard considers a static a static, no matter where it is defined.

You may want to consider using a static pointer and allocating objects
on first access.  If your static objects require destruction to free
up resources that won't be freed up by process termination, you could
have a single static object that orchestrates the destruction of all
your static pointers in it's destructor.

--
Kelly R. Burkhart

[The litigation] industry was, of course, up and running before the
tobacco litigation, but that taught lawyers just how lucrative it
could be to blame individuals' foolishness on, say, Joe Camel.
   -- George F. Will

 
 
 

statics destructing and g++

Post by Thomas Rin » Sat, 14 Aug 1999 04:00:00




> > Here's the problem
> > I have a static instance of CList<SomeOtherClass> which in turn makes a
> > static instance of CMemoryAllocator<CList<someOtherClass>::CNode>
> > the problem is that
> > CMemoryAllocator<CList<someOtherClass>::CNode> goes out of scope (is
> > destructed),
> > before CList<SomeOtherClass> is destructed... and the destructor of
> > CList relies on CMemoryAllocator.

> > Basically they are destructing in the reverse order I need them to.  So
> > what is the mechanism that g++ uses to destruct statics.

> > thanks in advance
> > Demetrius

> I can't answer specific to g++, but in general the order of
> construction and destruction of static objects is unspecified.  All
> you know for sure is that construction occurs before main and
> destruction occurs after.

AFAIK the standard says that static objects within one translation unit
(source file) are constructed in the order of their declaration and
destructed in reverse order. The order in which static objects residing in
different translation units are constructed, however, is unspecified.

        -- Thomas