problem while running a STL CPP application.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vinu

    problem while running a STL CPP application.

    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(T OSer);
    }

    void Remove(SerImp *TOSer)
    {
    m_list.erase(TO Ser);
    }
    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_d ecrement (__x=0xff35356c )
    at /rsft/users/akankari/gcc-3.4.2/libstdc++-v3/src/tree.cc:94

    #1 0xff2839a8 in std::_Rb_tree_i terator<R::Base SerImp*>::opera tor--
    (this=0xffbef3f 0) at stl_tree.h:195

    #2 0xff283434 in std::_Rb_tree<R ::BaseSerImp*, R::BaseSerImp*,
    std::_Identity< R::BaseSerImp*> , std::less<R::Ba seSerImp*>,
    std::allocator< R::BaseSerImp*> >::insert_uniqu e (this=0xff35356 8,
    __v=@0xffbef550 ) at stl_tree.h:877

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

    #4 0xff282bf0 in Services<R::Bas eSerImp>::Add (this=0xff35356 8,
    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

  • Jonathan Mcdougall

    #2
    Re: problem while running a STL CPP application.

    Vinu wrote:[color=blue]
    > 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(T OSer);
    > }
    >
    > void Remove(SerImp *TOSer)
    > {
    > m_list.erase(TO Ser);
    > }
    > protected:
    > typedef std::set<SerImp *> SerL;
    > typedef SerL::iterator SerIter;
    > SerL m_list;
    > };[/color]

    The code is alright.
    [color=blue]
    > Whenever I try to do "m_list.insert( TOSer)" the application is
    > giving Segmentation fault.[/color]

    Looks like something gets corrupted *before* this call. We've got no
    way to know what.
    [color=blue]
    > 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?[/color]

    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

    Comment

    • Vinu

      #3
      Re: problem while running a STL CPP application.


      Jonathan Mcdougall wrote:
      [color=blue]
      > Vinu wrote:[color=green]
      > > 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(T OSer);
      > > }
      > >
      > > void Remove(SerImp *TOSer)
      > > {
      > > m_list.erase(TO Ser);
      > > }
      > > protected:
      > > typedef std::set<SerImp *> SerL;
      > > typedef SerL::iterator SerIter;
      > > SerL m_list;
      > > };[/color]
      >
      > The code is alright.
      >[color=green]
      > > Whenever I try to do "m_list.insert( TOSer)" the application is
      > > giving Segmentation fault.[/color]
      >
      > Looks like something gets corrupted *before* this call. We've got no
      > way to know what.
      >[color=green]
      > > 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?[/color]
      >
      > 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[/color]


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


      BaseServices gBaseServices;

      gBaseServices.A dd(ptr);

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

      Crashing on m_list.insert(T OSer); this line.

      Vinu Warrier

      Comment

      • Jonathan Mcdougall

        #4
        Re: problem while running a STL CPP application.

        Vinu wrote:[color=blue]
        > Jonathan Mcdougall wrote:
        >[color=green]
        > > Vinu wrote:[color=darkred]
        > > > Hi,
        > > >
        > > > I am facing a problem while running a STL CPP application Compiled
        > > > using gcc in Solaris environment.[/color]
        >>
        > > 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.[/color]
        >
        > In another file I created one global object of class BaseServices which
        > is derived from the "class Services"
        >
        > BaseServices gBaseServices;
        > gBaseServices.A dd(ptr);
        >
        > Here ptr is valid pointer . while debugging it is going to th function
        > Add(ptr).
        >
        > Crashing on m_list.insert(T OSer); this line.[/color]

        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::A dd(). 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

        Comment

        • Vinu

          #5
          Re: problem while running a STL CPP application.

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

          gBaseServices.A dd(ptr); this is working fine.

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

          Comment

          • pH

            #6
            Re: problem while running a STL CPP application.

            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.

            Comment

            • n2xssvv g02gfr12930

              #7
              Re: problem while running a STL CPP application.

              Vinu wrote:[color=blue]
              > When I declare the variable (BaseServices gBaseServices) as a local
              > variable
              >
              > gBaseServices.A dd(ptr); this is working fine.
              >
              > Is I have to use any switch while compiling the application with gcc.
              >[/color]

              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

              Comment

              Working...