Completely Deleting Pointers

Discussion in 'C++' started by Min, Jun 30, 2003.

  1. Min

    Min Guest

    CFool
    {
    public:
    void StartWorldWarIII() { /*** Launch Nuclear Missiles **/ }
    };

    int main()
    {
    CFool* pFool = new CFool();
    delete pFool;
    //////******* const unsigned int GARBAGE = 0xA3;
    ///// ******* memset( pFoo, GARBAGE , sizeof( CFool) );
    pFool->StartWorldWarIII();

    return 0;
    }

    Among other techniques from Writing Solid Code by Steve Maguire, I tried to
    scramble the content by filling garbages ( the commented lines ). But it
    does not guarantee that "pFoo->StartWorldWarIII()" will crash, but the
    object will output garbage, the next time it is used. (an obvious attempt
    to indicate that the instance has been deleted.)

    Anyway, Anybody knows any trick or technique to guarantee
    "pFoo->StartWorldWarIII()" will crash ? for debugging purpose.
    (I have a feeling that I am asking it in a wrong NG, but what the heck.)

    TIA
     
    Min, Jun 30, 2003
    #1
    1. Advertising

  2. Min

    Haplo Guest

    In article <eR_La.327039$>,
    says...
    > CFool
    > {
    > public:
    > void StartWorldWarIII() { /*** Launch Nuclear Missiles **/ }
    > };
    >
    > int main()
    > {
    > CFool* pFool = new CFool();
    > delete pFool;
    > //////******* const unsigned int GARBAGE = 0xA3;
    > ///// ******* memset( pFoo, GARBAGE , sizeof( CFool) );
    > pFool->StartWorldWarIII();
    >
    > return 0;
    > }
    >
    > Among other techniques from Writing Solid Code by Steve Maguire, I tried to
    > scramble the content by filling garbages ( the commented lines ). But it
    > does not guarantee that "pFoo->StartWorldWarIII()" will crash, but the
    > object will output garbage, the next time it is used. (an obvious attempt
    > to indicate that the instance has been deleted.)
    >
    > Anyway, Anybody knows any trick or technique to guarantee
    > "pFoo->StartWorldWarIII()" will crash ? for debugging purpose.
    > (I have a feeling that I am asking it in a wrong NG, but what the heck.)


    How about

    pFool = NULL;

    after you delete it ?


    Haplo
     
    Haplo, Jun 30, 2003
    #2
    1. Advertising

  3. Min escribió:

    > int main()
    > {
    > CFool* pFool = new CFool();
    > delete pFool;
    > //////******* const unsigned int GARBAGE = 0xA3;
    > ///// ******* memset( pFoo, GARBAGE , sizeof( CFool) );
    > pFool->StartWorldWarIII();
    >
    > return 0;
    > }
    >
    > Among other techniques from Writing Solid Code by Steve Maguire, I tried to
    > scramble the content by filling garbages ( the commented lines ). But it


    I read that book, and it clearly explains that you must not use memory
    you have freed. You must not put garbage after delete it.

    Regards.
     
    =?iso-8859-1?Q?Juli=E1n?= Albo, Jun 30, 2003
    #3
  4. Min

    Andre Kostur Guest

    Haplo <> wrote in
    news::

    > In article <eR_La.327039$>,
    > says...
    >> CFool
    >> {
    >> public:
    >> void StartWorldWarIII() { /*** Launch Nuclear Missiles
    >> **/ }
    >> };
    >>
    >> int main()
    >> {
    >> CFool* pFool = new CFool();
    >> delete pFool;
    >> //////******* const unsigned int GARBAGE = 0xA3;
    >> ///// ******* memset( pFoo, GARBAGE , sizeof( CFool) );
    >> pFool->StartWorldWarIII();
    >>
    >> return 0;
    >> }
    >>
    >> Among other techniques from Writing Solid Code by Steve Maguire, I
    >> tried to scramble the content by filling garbages ( the commented
    >> lines ). But it does not guarantee that "pFoo->StartWorldWarIII()"
    >> will crash, but the object will output garbage, the next time it is
    >> used. (an obvious attempt to indicate that the instance has been
    >> deleted.)
    >>
    >> Anyway, Anybody knows any trick or technique to guarantee
    >> "pFoo->StartWorldWarIII()" will crash ? for debugging purpose.
    >> (I have a feeling that I am asking it in a wrong NG, but what the
    >> heck.)

    >
    > How about
    >
    > pFool = NULL;
    >
    > after you delete it ?


    That doesn't guarantee a crash. That invokes "Undefined Behaviour",
    which might include a crash (then again, it might do nothing. I know of
    at least one implementation where the above code will function perfectly
    fine, if StartWorldWarIII doesn't reference any class members....)

    To the OP, you might want to implement your own smart pointer which will
    call something like abort() or terminate() if one attempts to dereference
    a NULL pointer.
     
    Andre Kostur, Jun 30, 2003
    #4
  5. Min

    Min Guest

    Sorry, there is more clarification.

    delete pFool;
    //////******* const unsigned int GARBAGE = 0xA3;
    ///// ******* memset( pFoo, GARBAGE , sizeof( CFool) );

    should be read as
    //////******* const unsigned int GARBAGE = 0xA3;
    ///// ******* memset( pFoo, GARBAGE , sizeof( CFool) );
    delete pFool;

    Of course, "pFool = NULL;" could partially solve the problem. But failure
    to do so won't. Consider this:

    //******** Code Begins ******************
    CFool* pSaddam = new CFool();
    CFool* pUday = pSaddam;

    //////******* const unsigned int GARBAGE = 0xA3;
    ///// ******* memset( pSaddam, GARBAGE , sizeof( CFool) );
    delete pSaddam;
    pSaddam = NULL;

    pUday->StartWorldWarIII();

    //************************************
    It is very obvious in this example. But what I am trying to accomplish is
    than even forgetting to set to NULL, any pointers still pointing to an
    invalid memory block will gurantee to crash, if it is used again after
    deletion. By scrambling pSaddam before deletion, pUday might not do things
    properly, but it is still doing something, instread of plain crashing.
     
    Min, Jun 30, 2003
    #5
  6. Min escribió:

    > Sorry, there is more clarification.
    >
    > delete pFool;
    > //////******* const unsigned int GARBAGE = 0xA3;
    > ///// ******* memset( pFoo, GARBAGE , sizeof( CFool) );
    >
    > should be read as
    > //////******* const unsigned int GARBAGE = 0xA3;
    > ///// ******* memset( pFoo, GARBAGE , sizeof( CFool) );
    > delete pFool;


    That way the destructor of the object pointed by pFool will probably
    crash. I think the only safe mode to put garbage in the memory used by
    the object is overloading operator new and operator delete.

    Regards.
     
    =?iso-8859-1?Q?Juli=E1n?= Albo, Jun 30, 2003
    #6
  7. One problem you have with this is once the memory is freed, that
    memory is up for grabs. So even if you do something that 'guarantees'
    a crash, once that memory is allocated again, the memory contents will
    get changed.

    So, the first thing to note is that if you want a true guarantee, you
    can't actually free it. Now this means that you will have a memory
    leak. If this is an object that gets allocated constantly, then you
    are probably SOL. But if these objects are only created rarely, you
    might be fine.

    And also to note, this means that objects that are put on the stack
    are beyond hope. Due to the way stacks work, there is no way to
    prevent the memory from being freed.

    So, with all those caveats, here is quickly thought out method. First
    create
    the base class death_on_delete:

    class death_on_delete
    {
    public:
    base() : deleted_(false) {}
    ~base() { deleted_ = true; }
    void operator delete(void *) { /* do nothing */ }
    void check() const { assert(deleted_ == false); }

    private:
    bool deleted_;
    };

    Any class that derives from this and is created on the heap will never
    truly go away. Again, this does not work for classes on the stack
    (nor for classes that override operator delete).

    Then, in all your methods, you need to add a call to
    death_on_delete::check(). This method will assert if it is called
    after the function has been deleted.

    Have fun.

    samuel
     
    Ron Samuel Klatchko, Jul 1, 2003
    #7
    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. Xamalek
    Replies:
    7
    Views:
    701
  2. Harry Barker
    Replies:
    2
    Views:
    537
    Alf P. Steinbach
    Apr 19, 2006
  3. Lawrence D'Oliveiro

    Completely Deleting A Directory

    Lawrence D'Oliveiro, Apr 26, 2010, in forum: Python
    Replies:
    4
    Views:
    414
    Lawrence D'Oliveiro
    Apr 27, 2010
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    714
  5. crea
    Replies:
    2
    Views:
    428
    Nobody
    Dec 28, 2012
Loading...

Share This Page