By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

problem while running a STL CPP application.

P: n/a
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*>::operator--
(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

Dec 28 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Vinu wrote:
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

Dec 28 '05 #2

P: n/a

Jonathan Mcdougall wrote:
Vinu wrote:
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

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

Dec 28 '05 #3

P: n/a
Vinu wrote:
Jonathan Mcdougall wrote:
Vinu wrote:
Hi,

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


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.


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

Dec 28 '05 #4

P: n/a
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.

Dec 28 '05 #5

P: n/a
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.

Dec 28 '05 #6

P: n/a
Vinu wrote:
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
Dec 28 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.