problem while running a STL CPP application.

V

Vinu

Hi,

I am facing a problem while running a STL CPP application Compiled
using gcc in Solaris environment.

Following is the code :
#include <set>
{
template <class SerImp>
class Services
{
public:
void Add(SerImp *TOSer)
{
if (TOSer)
m_list.insert(TOSer);
}

void Remove(SerImp *TOSer)
{
m_list.erase(TOSer);
}
protected:
typedef std::set<SerImp*> SerL;
typedef SerL::iterator SerIter;
SerL m_list;
};


Whenever I try to do "m_list.insert(TOSer)" the application is
giving Segmentation fault.


This is the call stack.

#0 0xfef64ac8 in std::_Rb_tree_decrement (__x=0xff35356c)
at /rsft/users/akankari/gcc-3.4.2/libstdc++-v3/src/tree.cc:94

#1 0xff2839a8 in std::_Rb_tree_iterator<R::BaseSerImp*>::eek:perator--
(this=0xffbef3f0) at stl_tree.h:195

#2 0xff283434 in std::_Rb_tree<R::BaseSerImp*, R::BaseSerImp*,
std::_Identity<R::BaseSerImp*>, std::less<R::BaseSerImp*>,
std::allocator<R::BaseSerImp*> >::insert_unique (this=0xff353568,
__v=@0xffbef550) at stl_tree.h:877

#3 0xff282f08 in std::set<R::BaseSerImp*, std::less<R::BaseSerImp*>,
std::allocator<R::BaseSerImp*> >::insert (this=0xff353568,
__x=@0xffbef550) at stl_set.h:314

#4 0xff282bf0 in Services<R::BaseSerImp>::Add (this=0xff353568,
TOSer=0x246d8) at Services.h:48


Can anybody suggest what the reason for this Segmentation fault is? We
have to use any special switch while compiling this application because
the same application is working fine in windows?

Thanks,
Vinu
 
J

Jonathan Mcdougall

Vinu said:
Hi,

I am facing a problem while running a STL CPP application Compiled
using gcc in Solaris environment.

Following is the code :
#include <set>
{
template <class SerImp>
class Services
{
public:
void Add(SerImp *TOSer)
{
if (TOSer)
m_list.insert(TOSer);
}

void Remove(SerImp *TOSer)
{
m_list.erase(TOSer);
}
protected:
typedef std::set<SerImp*> SerL;
typedef SerL::iterator SerIter;
SerL m_list;
};

The code is alright.
Whenever I try to do "m_list.insert(TOSer)" the application is
giving Segmentation fault.

Looks like something gets corrupted *before* this call. We've got no
way to know what.
Can anybody suggest what the reason for this Segmentation fault is? We
have to use any special switch while compiling this application because
the same application is working fine in windows?

This may be because your program has undefined behavior: it may crash
at any moment. It just happens to work on Windows. Double check
accesses to the set (derived classes have access to it, remember) and
operations on memory around this class. It is impossible to help you
more unless you can give some more context.


Jonathan
 
V

Vinu

Jonathan said:
The code is alright.


Looks like something gets corrupted *before* this call. We've got no
way to know what.


This may be because your program has undefined behavior: it may crash
at any moment. It just happens to work on Windows. Double check
accesses to the set (derived classes have access to it, remember) and
operations on memory around this class. It is impossible to help you
more unless you can give some more context.


Jonathan


In another file I created one global object of class BaseServices which
is derived from the "class Services"


BaseServices gBaseServices;

gBaseServices.Add(ptr);

Here ptr is valid pointer . while debugging it is going to th function
Add(ptr).

Crashing on m_list.insert(TOSer); this line.

Vinu Warrier
 
J

Jonathan Mcdougall

Vinu said:
In another file I created one global object of class BaseServices which
is derived from the "class Services"

BaseServices gBaseServices;
gBaseServices.Add(ptr);

Here ptr is valid pointer . while debugging it is going to th function
Add(ptr).

Crashing on m_list.insert(TOSer); this line.

Again, this code is probably not the problem. I know it is frustrating
for you, but understand that the code you provided is valid. It should
not crash by itself.

What makes it crash is what happens before. Unfortunately, a lot may be
happening before. You may have a zillion lines of code executing before
that point. What went wrong? You'll have to find out.

Try to skip parts of the program to see if it solves the problem. You
should be able to comment everything except the portion which calls
BaseServices::Add(). If this works, you know the problem is elsewhere.
Go backward until it crashes and you'll have a better idea what could
cause the problem.

I know it's a pain but it doesn't seem you have any other choices.


Jonathan
 
V

Vinu

When I declare the variable (BaseServices gBaseServices) as a local
variable

gBaseServices.Add(ptr); this is working fine.

Is I have to use any switch while compiling the application with gcc.
 
P

pH

Jonathan's right; there's no reason why it shouldn't work by itself. If
it works when gBaseServices is local, that definitely implies that it's
getting damaged beforehand when global.

Does the error occur in isolation - when the only code running is the
class and minimal supporting stuff, and the global variable hasn't been
used beforehand?

You shouldn't need any gcc switch here, as what you're trying to do
should be perfectly standard. I would probably try using a debugger
like gdb to check whether the m_list member is correctly initialised
before the call; you'll probably find it contains some invalid pointer
somewhere (which might be tricky to spot).

You should try and find the minimum amount of code needed to 'break'
it, and perhaps post it here if it's fairly short, if you still can't
find the problem.
 
N

n2xssvv g02gfr12930

Vinu said:
When I declare the variable (BaseServices gBaseServices) as a local
variable

gBaseServices.Add(ptr); this is working fine.

Is I have to use any switch while compiling the application with gcc.

This problem maybe caused by using an invalid index on global array
which is thereby corrupting the global gBaseServices. Look at setting a
breakpoint that activates if gBaseServices is changed. If you're lucky
you'll find the erroneous code without hitting too many debug breaks.

JB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top