Solved

std::list  .size method

Posted on 2012-03-17
6
453 Views
Last Modified: 2012-03-18
I have seen sometimes core files in my application and when I bactraced I found it is coring at executing .size() function on std::list

std::list<std::string> myStringList

myStringList.size() sometimes cores

myStringList is member of static class which has only one instance for the entire application. I understand it should be protected by lock which I am not doing but could .size() on list ever core?
0
Comment
Question by:learningunix
  • 3
  • 3
6 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 37732911
Many things can cause a core dump given the right (or better : wrong) set of circumstances.

What kind of core dump is it ?

Is there a possibility of memory corruption ?

Since you mentioned locking, are multiple threads trying to access the list ?
0
 

Author Comment

by:learningunix
ID: 37733004
I was looking more into the code, I was having lock when I add entry to the list but when retrieving the size() I don't lock it. Could this cause the core?

Based on my design, yes it is possible one thread can update the list while I am trying to retrieve the size() of the list by other thread

Here's what I see when it execute size() on the list.

Program terminated with signal 11, Segmentation fault.
#0  0x00007f219fff95b1 in std::_List_const_iterator<string*>::operator++ (this=0x7f21ab5e8f58) at /usr/include/c++/4.3/bits/stl_list.h:221
221             _M_node = _M_node->_M_next;
(gdb) bt
#0  0x00007f219fff95b1 in std::_List_const_iterator<string*>::operator++ (this=0x7f21ab5e8f58) at /usr/include/c++/4.3/bits/stl_list.h:221
#1  0x00007f219fff95e4 in std::__distance<std::_List_const_iterator<string*> > (__first=..., __last=...) at /usr/include/c++/4.3/bits/stl_iterator_base_funcs.h:84
#2  0x00007f219fff962d in std::distance<std::_List_const_iterator<string*> > (__first=..., __last=...) at /usr/include/c++/4.3/bits/stl_iterator_base_funcs.h:119
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 37733030
>> it is possible one thread can update the list while I am trying to retrieve the size() of the list by other thread

Then that's a big suspect :)

You will need some way of ensuring this doesn't happen, and proper locking is one such way.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:learningunix
ID: 37733218
hmmm... I thought executing .size() on list is just a passive command and this should be ok. looks like my assumption is wrong
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 200 total points
ID: 37734230
A std::list is a linked list. Calculating the size of a linked list is not "passive", but involves traversing all nodes in the list.
0
 

Author Closing Comment

by:learningunix
ID: 37734757
Thanks a lot.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to understand recursion 12 229
returning a dereferenced pts in C++ 10 158
Safe conversion? 4 72
Are the two lines the same? If not what is the difference? 2 22
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question