Destructing global variables in correct order

Discussion in 'C++' started by Jarmo Muukka, May 16, 2006.

  1. Jarmo Muukka

    Jarmo Muukka Guest

    Hi all!

    I have a small problem I cannot find a good solution. The problem is
    destructing global (static) variables in correct order. I try to mimic it in
    here.

    // file1.cpp contains implementation of Class1

    static Class1 instance;

    // static
    void* Class1::alloc()
    {
    return ::instance.allocresource();
    }

    // static
    void Class1::free( void* resource )
    {
    ::instance.freeresource( resource );
    }
    --------------------------------------
    // file2.cpp contains implementation of Class2

    void Class2::foo()
    {
    this->Add( Class1::alloc() );
    }

    Class2::~Class2()
    {
    this->Clear(); // this calls Class1::free( resource ) in a loop
    }
    --------------------------------------
    // file3.cpp
    // this is an user file that uses Class2

    static Class2 resources;
    --------------------------------------
    So, Class2 uses Class1 and both classes (instances) store data into their
    own variables. Everything would be perfect if Class2 "resources" would be
    destructed before Class1 "instance". Compiler just decides to destruct
    "instance" first. It causes a problem because destructor of "resources"
    needs "instance" (of course it has to tell to "instance" that it does not
    need resources anymore).

    More background: Class2 is used generally and it should be possible to be
    global variable. Class1 is a global resource that Class2 needs. It is just
    for implementation (user of Class2 should not be aware of it).

    How would you solve the problem?

    Thanks in advance,
    JMu
    Jarmo Muukka, May 16, 2006
    #1
    1. Advertising

  2. Jarmo Muukka

    flopbucket Guest

    Someone may correct me here, but in general, you can't decide the order
    of destruction of global variables.

    In this case here, can you put Class1 inside Class2?
    flopbucket, May 16, 2006
    #2
    1. Advertising

  3. Jarmo Muukka

    Tomás Guest

    flopbucket posted:

    > Someone may correct me here, but in general, you can't decide the order
    > of destruction of global variables.



    Maybe you can if they're defined in the same translation unit?

    #include <string>

    std::string a
    std::string b;

    int main() {}

    "a" gets constructed first then "b".
    "b" gets destructed first, then "a".

    Not sure if that's how it works... might look it up.

    -Tomás
    Tomás, May 16, 2006
    #3
  4. Jarmo Muukka

    Jarmo Muukka Guest

    "Tomás" <> wrote in message
    news:hKiag.9230$...
    > flopbucket posted:
    >
    > > Someone may correct me here, but in general, you can't decide the order
    > > of destruction of global variables.

    >
    >
    > Maybe you can if they're defined in the same translation unit?
    >
    > #include <string>
    >
    > std::string a
    > std::string b;
    >
    > int main() {}
    >
    > "a" gets constructed first then "b".
    > "b" gets destructed first, then "a".
    >
    > Not sure if that's how it works... might look it up.
    >
    > -Tomás


    Objects cannot be defined in same translation unit. And, I cannot put Class1
    inside Class2. It would take too much resources. And, I do not like global
    variables. I try to avoid them as much as I can.

    I solved the problem in a following way. Instead of static object, I have
    static pointer to the object. This is reference counted. When there is a
    first call to alloc in Class2, I create the object and each alloc increments
    the reference count. Call to free will decrement the reference count and
    when count drops to zero, I delete the object.

    And of course the Class1 does not have static interface anymore. The object
    has to be created to use the interface. Class2 does that (desciption above).

    Thanks,
    JMu
    Jarmo Muukka, May 17, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wayne
    Replies:
    2
    Views:
    456
    Wayne
    Nov 11, 2003
  2. kvt

    self destructing programs

    kvt, Dec 9, 2007, in forum: C Programming
    Replies:
    9
    Views:
    299
    David Thompson
    Dec 24, 2007
  3. kvt
    Replies:
    8
    Views:
    349
    James Kanze
    Dec 11, 2007
  4. destructing error

    , Jan 27, 2008, in forum: C++
    Replies:
    9
    Views:
    522
    Grizlyk
    Feb 2, 2008
  5. mrstevegross
    Replies:
    2
    Views:
    419
    John Posner
    Dec 17, 2009
Loading...

Share This Page