0

I am trying to debug a crash happening after I derived a class and made few functions virtual.

Please see the sample code below:

class Apple   // Existing class
{
 public:
  Apple();
  virtual ~Apple(); // changed destructor to virtual

  void one();
  virtual void two(); // changed this routine to virtual

 private:
  int i;
  Seeds *mSeed;
}

Apple::Apple()
{
 bzero(this, sizeof(Apple);
 mSeed = new Seeds(); // creating object of class Seeds.
}

void Apple::one()
{
 i = 5;
}

void Apple::two()
{
 if(mSeed)
  {
   mSeed->hasDried(i); // does some HW register access
  }
}

class RedApple : public Apple  // newly created class
{
 public:
  void two();
}

main()
{
 Apple* appPtr = new Apple();  // Old code
 appPtr->one();   // ------> No problem
 appPtr->two();   // ------> Crash happens
}

The implementation of both one() and two() accesses the data variable i. And the crash is happening in the legacy code where the Apple object is created and calls the methods. And if I remove 'virtual' from the declaration, it works fine.

I printed out appPtr and its not null. And also, I tried printing 'this' and that also shows good. And the size of the object and the class matches.

The actual code is too big and complicated that I couldnt really figure out whats going on. I checked lot of online forums and couldnt find a good help on this. Even I couldnt find any good debug methods in stackOverflow as well.

Please help me to figure out this problem. How can I debug this ? Appreciate any help !!!

singleX
  • 375
  • 4
  • 13
  • 4
    That code looks fine, it must relate to how `two()` is implemented – billz Jan 21 '14 at 23:06
  • 1
    Did you rebuild all code that refers to that type after the change? There is no reason why that code would fail. Either the implementation is faulty or you did not rebuild the code and you are violating the ODR. – David Rodríguez - dribeas Jan 21 '14 at 23:11
  • "How can I debug this ?" Put a printf or three in one() and two() :) – codah Jan 21 '14 at 23:14
  • please insert the code of one() & two() methods – 4pie0 Jan 21 '14 at 23:18
  • @DavidRodríguez-dribeas: Yes, I did fully build... – singleX Jan 21 '14 at 23:18
  • @billz: let me add some code for one and two – singleX Jan 21 '14 at 23:19
  • @piotruś: let me add the code ... – singleX Jan 21 '14 at 23:19
  • Debug skill is important. build a debug version, let it crash, checkout the stack, that will tell u how it crashes, where it crashes etc. – billz Jan 21 '14 at 23:20
  • and how to correct this – 4pie0 Jan 21 '14 at 23:21
  • @Manoj please insert the code of two() – 4pie0 Jan 21 '14 at 23:22
  • The stack trace only let me find out that its crashing at the place where the two() is getting called. And its not going inside two() function. That call itself is making the crash... – singleX Jan 21 '14 at 23:23
  • please insert the code of two() – 4pie0 Jan 21 '14 at 23:25
  • Sorry for the delay. I have changed the code snippet to add Apple() constructor and two() implementation – singleX Jan 21 '14 at 23:33
  • @piotruś: please let me know whether it makes sense. – singleX Jan 21 '14 at 23:34
  • @Manoj what if you do Apple* rappPtr = new RedApple(); rappPtr->one(); rappPtr->two(); – 4pie0 Jan 21 '14 at 23:45
  • @piotruś: The code mentioned in main() is some legacy code, which I am not supposed to touch. And moreover, that code is supposed to work on Apple and not RedApple. – singleX Jan 21 '14 at 23:49
  • 1
    @Manoj Your code doens't follow `rule of three`, maybe that's the issue. – billz Jan 21 '14 at 23:49
  • @Manoj, as I said to you in my answer, you need to use RedApple – 72DFBF5B A0DF5BE9 Jan 21 '14 at 23:50
  • @billz: I was just reading about _rule of three_. Apple has explicitly defined destructor, copy constructor and assignment operator. I have overridden destructor and made the base class destructor virtual, but not the other two. Does that mean I have to make copy contructor and assignment operator also virtual ? – singleX Jan 21 '14 at 23:56
  • http://stackoverflow.com/questions/390997/when-virtual-doesnt-work – 72DFBF5B A0DF5BE9 Jan 21 '14 at 23:57
  • http://stackoverflow.com/questions/496440/c-virtual-function-from-constructor – 72DFBF5B A0DF5BE9 Jan 21 '14 at 23:58
  • Thanks for pasting the links. But, I am not calling any virtual function from constructor. – singleX Jan 22 '14 at 00:00
  • `Does that mean I have to make copy contructor and assignment operator also virtual ?` NO. read more about `rule of three` and double free ... – billz Jan 22 '14 at 00:09
  • @Manoj, only thing you need is just reading more, learning from examples instead of rejecting all suggestions. Just read more, links, comments, suggestions, read more, do googling and again read more, stop rejecting like "I'm not doing that, I'm not doing this, that's not, this is not" – 72DFBF5B A0DF5BE9 Jan 22 '14 at 00:20
  • @billz: I see from the stack trace that the _vptr.Apple = 0x0 – singleX Jan 22 '14 at 15:55
  • Guys, I should thank for all you guys responses which helped me in narrowing it down. There was a bzero of 'this' in the constructor which was clearing the vPtr, thus causing the issue. I didnt find out the bzero from the hefty constructor. Once again, thank you all – singleX Jan 22 '14 at 16:34

0 Answers0