Thread: Stack corruption ?

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248

    Stack corruption ?

    Hi everyone,

    Now every error seems weird, but this one _really_ does to me ;-)

    I have to classes, A and B. B is an interface for let's say B_a and B_b.
    'A' contains a pointer to 'B', pointing to an allocated object of either 'B_a' or 'B_b' (via some factory), and the 'B-object' contains a pointer to A (some pimpl implementation).
    Class 'A' also has a derived class, therefore some of it's member functions are virtual (and here's the problem).

    Code:
    class A;
    
    class B {
     public:
      B(A* p_a) : a_(p_a) {}
      virtual void read( 'file ') = 0;
     protected:
      A* a_;
    };
    
    class B_a : public B  // class implements 'read'
    class B_b : public B  // class implements 'read' in a similar way as B_a (see below)
    
    
    class A {
     public:
      A() : _b(new B_b(this)) {}  // or new B_a(this) , this is done in some factory method
      void read( 'file' ) { b_->read('file'); }
      virtual void f();
      virtual void g();
     protected:
      B* b_;
    };
    
    
    void B_a :: read( 'file' ) {
      // do a lot of things specific for B_a (or B_b)
      a_->f(); // someting weird happens here
    
      // do a lot of other things specific for B_a (or B_b)
      a_->g();
    }

    Now the weird thing is : the call 'a_->f()' does _not_ call 'A::f()' , but it call 'B::g()' !!

    If I remove the keyword 'virtual' (temporarily) for 'A::f()' it works, but then it produces a similar error for the call 'a_->g()' . Unless I remove the keyword 'virtual' ....

    ** I have verified the adresses of the pointers, that's all correct. I am debugging the correct code, I verified with some 'asserts'.
    ** calling 'a_->f()' from another context just just just before 'B_a::read', does not produce the problem.

    Is there somewhere a stack corruption ? If so, how could I detect it ?

    Thanks a lot !!!
    Last edited by MarkZWEERS; 01-31-2011 at 06:06 AM. Reason: indentation

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There is no function B::g() in what you posted.
    Non-compileable bits like ('file') serve no useful purpose in this example. You may as well just leave out that parameter.
    Please fix your example.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Your example makes baby kittens cry.

    Soma

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Yikes. Clean that code up and use more descriptive variable names, class names, and method names. The answer might jump out at you after you re-fector a bit for readability.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM