Explicitly calling destructor

Discussion in 'C++' started by engaarea@gmail.com, Sep 8, 2006.

  1. Guest

    hi all,

    What are the circumstances in which one would have to call a destructor
    explicitly.


    Regards
    5
     
    , Sep 8, 2006
    #1
    1. Advertising

  2. schrieb:
    > hi all,
    >
    > What are the circumstances in which one would have to call a destructor
    > explicitly.
    >


    Yes, after a placement new.


    best regards,
    -- Markus

    >
    > Regards
    > 5
    >
     
    Markus Grueneis, Sep 8, 2006
    #2
    1. Advertising

  3. Guest

    Markus Grueneis wrote:
    >
    > Yes, after a placement new.
    >
    >
    > best regards,
    > -- Markus
    >



    Can you give me an example
    Thanks
     
    , Sep 8, 2006
    #3
  4. wrote:
    > What are the circumstances in which one would have to call a destructor
    > explicitly.


    You should only call a destructor explicitly on an object you have
    created using placement new - see the FAQ
    [http://www.parashift.com/c -faq-lite/] section 11.

    -- PH
     
    Paul Henderson, Sep 8, 2006
    #4
  5. Rolf Magnus Guest

    wrote:

    > hi all,
    >
    > What are the circumstances in which one would have to call a destructor
    > explicitly.


    Usually, the allocation/deallocation of storage and the
    construction/destruciton of an object are done together. If you want to
    separate those things, you can use the placement new operator to construct
    an object into alreaedy allocated storage. The counterpart to that is the
    explicit destructor call. It destroys the object, but doesn't deallocate
    the storage. This is e.g. used (indirectly through an allocator) by the
    standard container classes, like std::vector, because those do their own
    memory management.
     
    Rolf Magnus, Sep 8, 2006
    #5
  6. Aman JIANG Guest

    wrote:
    > hi all,
    >
    > What are the circumstances in which one would have to call a destructor
    > explicitly.
    >
    >
    > Regards
    > 5



    When you are in the circumstance that unable to use delete,
    Or when you need to manage memory by yourself.


    class Dolphin
    {
    int m_iDummy;
    public:
    Dolphin() { cout << "Dolphin" << endl; }
    virtual ~Dolphin() { cout << "~Dolphin" << endl; }
    };

    int main()
    {
    unsigned char buffer [256];

    // construct a Dolphin in buffer
    Dolphin * d = new(buffer) Dolphin;

    // buffer is in stack, not in heap, so you cannot use delete
    // But you must destruct it, too:
    d->~Dolphin();
    }

    -----------------------------------------
    Dolphin
    ~Dolphin
     
    Aman JIANG, Sep 8, 2006
    #6
  7. Moonlit Guest

    Hi,

    For example:

    Consider a Variant class ( which can hold a number of types like
    long/int/string etc)
    To save on memory you define a union with all those variables
    Now to properly construct a string in the area of the union you would need
    placement new
    On destruction of your string (maybe because the variant type is assigned a
    'long') you have to destruct the string, but not free its memory (it can't
    since it is not allocated on the heap, but in the area where the union is
    located ) in this case you would explicitely call delete:


    Example:

    union {

    Int8 Char;

    Int64 Long;

    //UInt8 UChar;

    //UInt64 ULong;

    double Double;

    bool Bool;

    char String [ sizeof( std::string ) ];

    char Map [ sizeof( std::map<UVar*,UVar*, UFindVar> ) ];

    char SRefPtr[ sizeof( MSRefPtr<ISerialize> ) ];

    char WRefPtr[ sizeof( MWRefPtr<ISerialize> ) ];

    char KeyStroke[ sizeof( MKey ) ];

    };



    UVar::UVar( const string& String ):

    Type ( eString )

    {

    new( this->String ) string( String );

    }


    void UVar::CleanUp()

    {

    switch( Type )

    {

    case eString:

    reinterpret_cast<std::string *const>( String )->~string();

    break;

    //...etc

    }

    }


    Regards, Ron AF Greve

    http://moonlit.xs4all.nl

    <> wrote in message
    news:...
    >
    > Markus Grueneis wrote:
    >>
    >> Yes, after a placement new.
    >>
    >>
    >> best regards,
    >> -- Markus
    >>

    >
    >
    > Can you give me an example
    > Thanks
    >
     
    Moonlit, Sep 8, 2006
    #7
  8. posted:

    > hi all,
    >
    > What are the circumstances in which one would have to call a destructor
    > explicitly.



    Most typically when the construction was explicit (e.g. via "placement new"):

    char buffer[sizeof(string)]; /* Pretend it's suitably aligned. */

    string *const p = ::new(buffer) string("Hello");

    p->~string();

    --

    Frederick Gotham
     
    Frederick Gotham, Sep 8, 2006
    #8
    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. Timothy Madden
    Replies:
    3
    Views:
    3,936
    Timothy Madden
    Nov 19, 2004
  2. frs
    Replies:
    20
    Views:
    794
    Alf P. Steinbach
    Sep 21, 2005
  3. arun
    Replies:
    2
    Views:
    577
    benben
    Jun 13, 2006
  4. Jimmy Hartzell
    Replies:
    0
    Views:
    440
    Jimmy Hartzell
    May 19, 2008
  5. Michael Tsang
    Replies:
    8
    Views:
    685
    Alf P. Steinbach
    Dec 14, 2009
Loading...

Share This Page