473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

segmentation fault at destruction of stl vector

Hi everybody!
I came across a very strange problem with stl vectors during
developement of a mesh creation program. As the program is quite large
I can only post small parts of it. Basically I have a
std::vector<SPoly> xtop;
where SPoly simply is:

struct SPoly
{
int start, end;
int bmarker;
};

In my function (code reduced):

void MeshModel::createDomain()
{
std::vector<SPoly> xtop;
std::vector<NPoint> trinodes;
// lots of code, left out
// filling of xtop via repeated calls to xtop.push_back()
sortlist(xtop,trinodes);
// some other code, no changes to xtop
}

where sortlist is defined as:
void MeshModel::sortlist(std::vector<SPoly> &polies,
std::vector<NPoint> nodes)
{
// simple selection sort
int n=polies.size(); int m=nodes.size();
for(int i=0; i<n; i++)
{
int imin = i;
for(int j=i; j<n; j++)
{
assert(polies[imin].start < m); assert(polies[j].start < m);
if (nodes[polies[imin].start].point.y <
nodes[polies[j].start].point.y)
imin = j;
}
SPoly temp=polies[i];
polies[i]=polies[imin];
polies[imin]=temp;
}
}

at the last line of createDomain() I get a segmentation fault. Here the
backtrace from gdb:
----------------------------------------------------------------------
#0 0x40181057 in _int_free () from /lib/tls/libc.so.6
#1 0x40180048 in free () from /lib/tls/libc.so.6
#2 0x400bb233 in operator delete () from /usr/lib/libstdc++.so.5
#3 0x400a8dac in std::__default_alloc_template<true, 0>::deallocate ()
from /usr/lib/libstdc++.so.5
#4 0x0804aa8d in std::__simple_alloc<SPoly,
std::__default_alloc_template<true, 0> >::deallocate (
__p=0x80982d8, __n=32) at
/usr/include/c++/3.2.3/bits/stl_alloc.h:248
#5 0x0804aa08 in std::_Vector_alloc_base<SPoly, std::allocator<SPoly>,
true>::_M_deallocate (
this=0xfff93ca0, __p=0x80982d8, __n=32) at
/usr/include/c++/3.2.3/bits/stl_vector.h:123
#6 0x0804a990 in ~_Vector_base (this=0xfff93ca0) at
/usr/include/c++/3.2.3/bits/stl_vector.h:143
#7 0x0804a90a in ~vector (this=0xfff93ca0) at
/usr/include/c++/3.2.3/bits/stl_vector.h:375
#8 0x08055054 in MeshModel::createDomain (this=0x8081008) at
tetmesh.cpp:1021
#9 0x08049ff9 in main () at dcfemeshseries.cpp:66
----------------------------------------------------------------------
Why does the destrucion of the variable xtop fail ? I also changed the
struct into a class with appropriately defined assignment operator,
copy constructor and destructor (why should this be necessary?) - still
the same segm. fault!
I am lost here! Any help on this would be great!
Mark

Jul 23 '05 #1
3 5171
A couple of things you can do:

1. check that you are not getting an STL exception using a try catch
around the code. You check that you are accessing 'nodes' within
limits, but not 'polies'. If 'nodes' is larger than 'polies' it is
possible for 'imin' to go out of bounds.

2. Pass 'nodes' by reference (const reference if possible):

void MeshModel::sortlist(
std::vector<SPoly> &polies,
std::vector<NPoint> & nodes)

You do not modify 'nodes', and I believe the copy is undesirable. If
it is memory corruption outside the code above, then fixing this may
help you locate the cause of the corruption.

Jul 23 '05 #2

"mblome" <ma********@gmail.com> skrev i en meddelelse
news:11**********************@g47g2000cwa.googlegr oups.com...
Hi everybody!
I came across a very strange problem with stl vectors during
developement of a mesh creation program. As the program is quite large
I can only post small parts of it. Basically I have a
std::vector<SPoly> xtop;
where SPoly simply is:

struct SPoly
{
int start, end;
int bmarker;
};

In my function (code reduced):

void MeshModel::createDomain()
{
std::vector<SPoly> xtop;
std::vector<NPoint> trinodes;
// lots of code, left out
// filling of xtop via repeated calls to xtop.push_back()
sortlist(xtop,trinodes);
// some other code, no changes to xtop
}

where sortlist is defined as:
void MeshModel::sortlist(std::vector<SPoly> &polies,
std::vector<NPoint> nodes)
{
// simple selection sort
Why not use the built-in sort?
int n=polies.size(); int m=nodes.size();
for(int i=0; i<n; i++)
{
int imin = i;
for(int j=i; j<n; j++)
{
assert(polies[imin].start < m); assert(polies[j].start < m);
if (nodes[polies[imin].start].point.y <
nodes[polies[j].start].point.y)
imin = j;
}
SPoly temp=polies[i];
polies[i]=polies[imin];
polies[imin]=temp;
}
}

at the last line of createDomain() I get a segmentation fault. Here the
backtrace from gdb:
----------------------------------------------------------------------
#0 0x40181057 in _int_free () from /lib/tls/libc.so.6
#1 0x40180048 in free () from /lib/tls/libc.so.6
#2 0x400bb233 in operator delete () from /usr/lib/libstdc++.so.5
#3 0x400a8dac in std::__default_alloc_template<true, 0>::deallocate ()
from /usr/lib/libstdc++.so.5
#4 0x0804aa8d in std::__simple_alloc<SPoly,
std::__default_alloc_template<true, 0> >::deallocate (
__p=0x80982d8, __n=32) at
/usr/include/c++/3.2.3/bits/stl_alloc.h:248
#5 0x0804aa08 in std::_Vector_alloc_base<SPoly, std::allocator<SPoly>,
true>::_M_deallocate (
this=0xfff93ca0, __p=0x80982d8, __n=32) at
/usr/include/c++/3.2.3/bits/stl_vector.h:123
#6 0x0804a990 in ~_Vector_base (this=0xfff93ca0) at
/usr/include/c++/3.2.3/bits/stl_vector.h:143
#7 0x0804a90a in ~vector (this=0xfff93ca0) at
/usr/include/c++/3.2.3/bits/stl_vector.h:375
#8 0x08055054 in MeshModel::createDomain (this=0x8081008) at
tetmesh.cpp:1021
#9 0x08049ff9 in main () at dcfemeshseries.cpp:66
----------------------------------------------------------------------
Why does the destrucion of the variable xtop fail ? I also changed the
struct into a class with appropriately defined assignment operator,
copy constructor and destructor (why should this be necessary?) - still
the same segm. fault!
I am lost here! Any help on this would be great!
This could very well be a fault introduced elsewhere. The heap seems to be
corrupted and this can happen in any number of situations - e.g. by using an
object you have deleted, by deleting an object twice or by writing past the
end of an array.

/Peter
Mark

Jul 23 '05 #3
mblome wrote:
Hi everybody!
I came across a very strange problem with stl vectors during
developement of a mesh creation program. As the program is quite
large I can only post small parts of it. Basically I have a
std::vector<SPoly> xtop;
where SPoly simply is:

struct SPoly
{
int start, end;
int bmarker;
};
The problem isn't in the code you've shown. You will have to
try harder to make a code sample that does demonstrate the
problem. In fact, if you try and do this on your own machine
you will probably come across the problem anyway.
void MeshModel::createDomain()
{
std::vector<SPoly> xtop;
std::vector<NPoint> trinodes;
What is the definition of NPoint? It would be the culprit if
its copy constructor / assignment operator / destructor were
incorrect.
sortlist(xtop,trinodes);
}

void MeshModel::sortlist(std::vector<SPoly> &polies,
std::vector<NPoint> nodes)
Pass 'nodes' by const reference to avoid unnecessary copying.
{
// simple selection sort
int n=polies.size(); int m=nodes.size();
for(int i=0; i<n; i++)
{
int imin = i;
for(int j=i; j<n; j++)
{
assert(polies[imin].start < m);
assert(polies[j].start < m);
if (nodes[polies[imin].start].point.y <
nodes[polies[j].start].point.y)
imin = j;
}
What happens when your assert fails? Maybe that is what is
causing the crash? How about if polies[imon].start is negative?
SPoly temp=polies[i];
polies[i]=polies[imin];
polies[imin]=temp;
You could do this more easily:
std::swap(polies[i], polies[imon]);
}
} Why does the destrucion of the variable xtop fail ? I also changed
the struct into a class with appropriately defined assignment
operator, copy constructor and destructor (why should this be
necessary?)


You need those functions if a class contains handles to
resources that need to be freed or released. In your posted
code, SPoly does not need those functions.

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
3
by: VB | last post by:
Hi, here File.cpp and File.h: File.cpp: ---------------------- #pragma warning (disable: 4786)
1
by: sandwich_eater | last post by:
I get a segmentation fault in my program when calling a function "TestFn" that has been passed as a pointer into another function. The following excerpt should give enough information as to what I...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
1
by: samuel.y.l.cheung | last post by:
Hi, I wrote a template to use copy() algorithm, called copyAll: template<class T> void copyAll(const T& src , T& dest ) { copy (src.begin(), src.end(), back_inserter(dest)); } but when I...
5
by: silverburgh.meryl | last post by:
Hi, I have a segmentation fault in line 66 of GroupResult.h and I can't figure out why that causes any problem, I appreciate if anyone can help. line 66 of Result.h: 66 size_t size()...
1
by: jwlkr | last post by:
Hi, I am trying to sort a vector of a user defined type: a class which represents points in cartesian coordinates. The vector of points needs to be sorted according to the value of the...
8
by: Bryan | last post by:
Hello all. I'm fairly new to c++. I've written several programs using std::vectors, and they've always worked just fine. Until today. The following is a snippet of my code (sorry, can't...
2
by: Steve | last post by:
I have segmentation fault when calling resize on an stl vector. I'm unsure if I'm doing something horribly wrong or if the stl is being dumb. The code breaks down like this: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.