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