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 !!!

26
  • 4
    That code looks fine, it must relate to how two() is implemented
    – billz
    Jan 21, 2014 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. Jan 21, 2014 at 23:11
  • "How can I debug this ?" Put a printf or three in one() and two() :)
    – codah
    Jan 21, 2014 at 23:14
  • please insert the code of one() & two() methods
    – 4pie0
    Jan 21, 2014 at 23:18
  • 1
    @Manoj Your code doens't follow rule of three, maybe that's the issue.
    – billz
    Jan 21, 2014 at 23:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.