Thread: Vector.clear is creating problem

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    136

    Vector.clear is creating problem

    Hi,

    I was looking at an old multithreaded program which is creating problem (core dump)!. The debugger tells the problem at vector.clear().

    Code:
    =>[1] __rwstd::__destroy<UCProfileData>(pointer = 0x1142994b8), line 177 in "memory"
      [2] std::allocator_interface<std::allocator<UCProfileData>,UCProfileData>::destroy(this = 0xffffffff7fffad67, p = 0x1142994b8), line 513 in "memory"
      [3] std::vector<UCProfileData,std::allocator<UCProfileData> >::__destroy(this = 0xffffffff7fffb208, start = 0x114299650, finish = 0x114299fe0), line 147 in "vector"
      [4] std::vector<UCProfileData,std::allocator<UCProfileData> >::erase(this = 0xffffffff7fffb208, first = 0x1142979a0, last = 0x114299fe0), line 522 in "vector"
      [5] std::vector<UCProfileData,std::allocator<UCProfileData> >::clear(this = 0xffffffff7fffb208), line 550 in "vector"
    while looking more i found somewhere in code by commenting one assignment in vector program works OK. The assignment was in vector through an iterator also which is in a loop.
    Code:
    for (iter= (*it)->my_vector.begin(); iter!= (*it)->my_vector.end(); ++iter)
    {
    -- some code working on iterator and then
    (*iter)->value=1;
    }
    iter is of same type as it.my_vector

    Also, I observed one thing, where the vector.clear is there if i put a print to log then also it works ok.

    Please help what is happening wrong here.
    S_ccess is waiting for u. Go Ahead, put u there.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It would help if you provided a small but complete (as in compilable, then linkable, then executable) that illustrates the symptoms. In the process of creating such a sample, you might have an "ah!" moment. Otherwise, your problem will be tractable enough for someone else to help.

    All I can offer, based on the insufficient information you have provided, is indications of the nature of the cause;

    1) A failure when releasing memory (or destructing objects) - which is what vector.clear() does - usually means a problem in some code executed BEFORE the clear call.

    2) The problem in such BEFORE code is either a pointer molestation (writing to an invalid pointer, falling from the end of an array, etc etc) or, in multithreaded code, a failure to synchronise properly (eg a race condition, two threads trying to access the same element of a vector at once, etc etc).

    I suggest looking VERY carefully in the code you have marked as "-- some code working on iterator and then". However, the problem is potentially in code which occurs before that.

    As to why commenting out code, or adding a "print to log", can eliminate the symptoms, that is typical of such problems. Such things often cause a restructure of how your program uses memory at run time, which means that the invalid operation (whatever it is) corrupts something different. The operating system only detects SOME problems, and restructuring memory usage of your program can prevent the OS detecting the symptom.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I was looking at an old multithreaded program which is creating problem (core dump)!.
    Are you aware that STL isn't thread-safe.

    If you've been doing hooky things across multiple threads on the same STL objects, then the problem is going to be somewhere else.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    clear doesn't create the problem, it detects the problem.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Clear arrays before looping problem.
    By McAl in forum C Programming
    Replies: 20
    Last Post: 12-01-2011, 03:06 AM
  2. Creating custom class for vector of pairs
    By Mr_Miguel in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2009, 07:04 PM
  3. is there easy way to clear nested vector?
    By timchen in forum C++ Programming
    Replies: 1
    Last Post: 12-26-2007, 10:18 PM
  4. Custom Vector template: Want to create erase/clear function
    By Bird Killer in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:37 AM
  5. Using delete to clear a STL vector
    By starkhorn in forum C++ Programming
    Replies: 2
    Last Post: 05-19-2005, 02:59 AM