Question

Yet again "Pure virtual method called error", I checked other similar question, they do not resolve my issue. I have a crash with the mentioned message. I don't do any fancy staff. The code is big, so only the important extracts:

class cCOLLECT_STR_NODES_HELPER
{
  protected:
    cCOLLECT_STR_NODES_HELPER( const std::string& searchStr,
                           const int attribute,
               const cGTI_SRCH_TREE_NAME_DLG *pDlg )
   : m_compareStr(searchStr), m_attr(attribute), pSrchDlg(pDlg) {}

    virtual ~cCOLLECT_STR_NODES_HELPER() {}

   public:
    virtual bool TreatTheNode( const cGTC_TREE_NODE *curNode ) = 0;
...
}

Then goes derived class:

class cCOLLECT_STR_TOP_NODES_HELPER : public cCOLLECT_STR_NODES_HELPER
{
  public:
   cCOLLECT_STR_TOP_NODES_HELPER( const std::string& searchStr,
                               const int attribute,
                   const cGTI_SRCH_TREE_NAME_DLG *pDlg )
   : cCOLLECT_STR_NODES_HELPER( searchStr, attribute, pDlg ) {}

public:
virtual bool TreatTheNode( const cGTC_TREE_NODE *curNode );
...
}  

TreatTheNode() is implemented separately:

bool cCOLLECT_STR_TOP_NODES_HELPER::TreatTheNode( const cGTC_TREE_NODE *curNode ) {...

Then derived class initialized:

cCOLLECT_STR_NODES_HELPER *pHelper;

  cCOLLECT_STR_TOP_NODES_HELPER helper( searchStr, attribute, this );
  pHelper = &helper;

Then pHelper passed into a function and used there:

TraverseTreeNodes( const cGTC_TREE_NODE *curNode,
               cCOLLECT_STR_NODES_HELPER *pHelper ) const
{
if ( pHelper->TreatTheNode( curNode ) )  => CRASH

The project built successfully. What's wrong?

Was it helpful?

Solution

After I found a mistake from my side and deleted the question, I decided to undelete and partially answer it. I am sure somebody else can benefit from this stupid mistake (when you have a deadline in 3 hours you do make mistakes).

I knew it was something obvious: actually the code which I wrote as:

cCOLLECT_STR_TOP_NODES_HELPER helper( searchStr, attribute, this );
pHelper = &helper;

in reality was:

{
  cCOLLECT_STR_TOP_NODES_HELPER helper( searchStr, attribute, this );
  pHelper = &helper;
}

so helper went out of scope...

The problem is, I did debugging. pHelper was pointing to something meaningful, at least that's what it looked like. I am still not sure how exactly the crash happened, but the reason is clear. When the pointed object went out of scope, it is probably UB. Still, if somebody describes what internally happened, I'll accept the answer.

OTHER TIPS

In this case you are facing a null pointer problem, as you are passing a uninitialized object into the arguments, the pHelper would not have a value, thus will attempt to crash the program.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top