"Static" destructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jahn Otto Næsgaard Andersen

    "Static" destructor

    Hi,

    I have a class A with a static pointer to some data. The pointer is static
    because I want to access the data from all instances of the class. The data
    itself is allocated when the first instance of A is constructed:

    # a.h

    class A
    {
    private:
    static char* sm_data;

    public:
    A();
    };

    # a.cpp

    A::A()
    {
    if (sm_data == NULL)
    sm_data = new char[1024];
    }

    char* A::sm_data = NULL;



    This works fine, but what should I do to make sure that sm_data is
    deallocated when I close the application? I could always use reference
    counting and delete [] sm_data when the last instance of A is deleted, but
    that is a little cumbersome. I could also have a static function that is
    called from the application's main class when it exits, but then I'd have to
    rely on other parts of the application.

    My question is: how would you solve this?


    Regards,
    Jahn Otto



  • Jahn Otto Næsgaard Andersen

    #2
    Re: "Static&qu ot; destructor

    Oops - typo. I wrote "I could always use reference counting[....]", but I
    meant instance counting.


    Jahn Otto


    Comment

    • Gianni Mariani

      #3
      Re: "Static&qu ot; destructor

      Jahn Otto Næsgaard Andersen wrote:[color=blue]
      > Hi,
      >
      > I have a class A with a static pointer to some data. The pointer is static
      > because I want to access the data from all instances of the class. The data
      > itself is allocated when the first instance of A is constructed:
      >
      > # a.h
      >
      > class A
      > {
      > private:
      > static char* sm_data;
      >
      > public:
      > A();
      > };
      >
      > # a.cpp
      >
      > A::A()
      > {
      > if (sm_data == NULL)
      > sm_data = new char[1024];[/color]
      ^^^^^^^ no need for this
      [color=blue]
      > }
      >
      > char* A::sm_data = NULL;[/color]

      you could do this:

      char* A::sm_data = new char[1024];
      [color=blue]
      >
      >
      >
      > This works fine, but what should I do to make sure that sm_data is
      > deallocated when I close the application?[/color]

      You may not need to if this is a stan-alone application.

      I could always use reference[color=blue]
      > counting and delete [] sm_data when the last instance of A is deleted, but
      > that is a little cumbersome.[/color]

      Use a helper class - in this case a std::string is great (and reccomended).

      class A
      {
      private:
      std::string sm_data;

      public:
      A();
      };

      # a.cpp

      A::A()
      {
      }


      ok - so if you don't like that because it breaks too much code here is a
      bad bad bad hack. (not reccomended)

      class A
      {
      private:
      static char* sm_data;

      public:
      A();

      private:
      class Nuker
      {
      public:
      ~Nuker()
      {
      delete[] sm_data;
      }

      Nuker() {};

      private:
      Nuker( const Nuker & );
      Nuker & operator = ( const Nuker & );

      };

      static Nuker a_nuker;
      };

      A::A()
      {
      Nuker & nuker = a_nuker;
      }

      char* A::sm_data = new char[1024];

      A::Nuker A::a_nuker;


      I could also have a static function that is[color=blue]
      > called from the application's main class when it exits, but then I'd have to
      > rely on other parts of the application.
      >
      > My question is: how would you solve this?
      >[/color]

      Comment

      • Karl Heinz Buchegger

        #4
        Re: "Static&qu ot; destructor



        "Jahn Otto Næsgaard Andersen" wrote:[color=blue]
        >
        > My question is: how would you solve this?[/color]

        You are heading for a 'singleton'.
        Try google to read more about it.


        --
        Karl Heinz Buchegger
        kbuchegg@gascad .at

        Comment

        • Alexander Terekhov

          #5
          Re: "Static&qu ot; destructor


          "Jahn Otto Næsgaard Andersen" wrote:

          [... lazy init stuff ...]
          [color=blue]
          > This works fine, but what should I do to make sure that sm_data is
          > deallocated when I close the application?[/color]

          Refcounting and thread-sharing aside for a moment***, try this:


          (Subject: Re: inheritance and singletons)

          regards,
          alexander.

          ***) http://opengroup.org/austin/mailarch.../msg04858.html

          Comment

          • jeffc

            #6
            Re: "Static&qu ot; destructor


            "Jahn Otto Næsgaard Andersen" <spamtrap8@jott o.no> wrote in message
            news:Kxdab.2009 4$BD3.3418836@j uliett.dax.net. ..[color=blue]
            >
            >
            > This works fine, but what should I do to make sure that sm_data is
            > deallocated when I close the application?[/color]

            Why don't you just delete it if it's not zero, and then set it to zero?


            Comment

            • Kevin Goodsell

              #7
              Re: &quot;Static&qu ot; destructor

              Jahn Otto Næsgaard Andersen wrote:
              [color=blue]
              > Hi,
              >
              > I have a class A with a static pointer to some data. The pointer is static
              > because I want to access the data from all instances of the class. The data
              > itself is allocated when the first instance of A is constructed:
              >
              > # a.h
              >
              > class A
              > {
              > private:
              > static char* sm_data;[/color]

              If you can replace this with

              static std::string sm_data;

              then do so. It will save you headaches, and solve your immediate problem.

              Another alternative is to use a smart pointer here. Something similar to
              std::auto_ptr should work, but auto_ptr itself is not appropriate,
              because it will use 'delete' rather than 'delete[]'. You'd have to
              define your own 'auto_array' or use one from an existing library.

              Using a smart pointer would let the rest of the code remain basically
              untouched, so it's a good "quick fix". However, quick isn't always the
              best way. Your overall design would probably benefit from switching to
              something that allocates and manages memory for you, such as the
              std::string suggested above.

              -Kevin
              --
              My email address is valid, but changes periodically.
              To contact me please use the address from a recent posting.

              Comment

              Working...