Pointers stored in the list getting corrupted??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bmsce engg
    New Member
    • Jun 2011
    • 8

    Pointers stored in the list getting corrupted??

    Hi,
    Have been struggling to find the reason for this occasional problem with pointers that I am storing in a list (STL C++).
    My class name is : Tamper

    All I do is this:

    1- Instantiate Tamper with new and push the address in a list of type Tamper. The number of pushes are huge, may be in hundreds (dont think it matters as long as i have space).

    Tamper *ptr=new Tamper();
    list<Tamper *> = mylist;
    mylist.push_bac k(ptr);

    2- At a later point, I simply use list iterators and retrieve the stored pointers and call "delete" on them, which sounds straight forward and it works fine for me. But once in a while, I see that my process dumps core with SIGSEGV or SIGBUS, and stack points to "delete" operation.

    list<Tamper *>::iterator it = mylist.begin();

    for(; it != mylist.end() ; ++it)
    {
    delete *it;
    *it=0;
    }

    How do I get rid of this occasional but consistent problem while freeing the memory. I don't think I am trying to release someone else's memory.

    Appreciate your help here.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    What you show is straightforward . You delete pointers you allocated and you set those deleted pointers to zero to avoid an exception by deleting them again.

    Are you sure you never pushed a local pointer? That wil do it every time.

    Is this a multi-threaded program? If so, the delete and he subsequent setting of the pointer to zero is no thread safe.

    Comment

    • bmsce engg
      New Member
      • Jun 2011
      • 8

      #3
      Thanks for your reply first of all!!!
      This is not multithreaded, and there is no pushing of local pointer. I already did something like this just to make sure I am deleting my own pointers:

      for(; it != mylist.end() ; ++it)
      {
      if ( typeid(*it) == typeid(Tamper *) )
      {
      delete *it;
      *it=0;
      }
      }


      This works in a similar way as before. But dumps core as usual.

      The point here is, somehow the addresses are getting corrupted.
      - Is there a better way to validate the address before deleting?
      - Could there be environment issue here?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        I ran your code for 1000 pointers and failed to produce a crash.

        There's something else going on that's not in the code you have shown. Using naked pointers in C++ is very dangerous. The Tamper* should be a handle object.

        There is no way to validate an address. The most you can do is say that a zero address is invalid. All non-zero addresses are presumed valid.

        Is there by chance a delete of the Tamper* somewhere else in the code and at that place the pointer is not set to zero?

        Comment

        • mac11
          Contributor
          • Apr 2007
          • 256

          #5
          I think you'd be better off having your list hold smart pointers (such as boost::shared_p tr). Then you don't have to explicitly delete anything and are much less likely to run into problems with raw pointers.

          For an example see:

          Comment

          • bmsce engg
            New Member
            • Jun 2011
            • 8

            #6
            Thanks for your answer.
            I will check and make sure I am not deleting this address somewhere else, though I am sure I am not doing it.
            I will come back to you weaknessforcats after some more check.

            Comment

            • bmsce engg
              New Member
              • Jun 2011
              • 8

              #7
              Thanks mac11... I will check the possibility of using boost shared ptrs.

              Comment

              Working...