karikari
karikari

Reputation: 1

How to resolve error "Run-Time Check Failure #3"?

I am working on MS Visual Studio. I keep on getting this error:

"Run-Time Check Failure #3 - The variable 'test' is being used without being initialized."

I don't have any idea how to solve this. Here is the code that I'm currently tries to modify:

STDMETHODIMP CButtonDemoBHO::Exec(const GUID*, DWORD nCmdID, DWORD d, VARIANTARG*, VARIANTARG* pvaOut)
{

   CRebarHandler *test;

   switch (nCmdID){
   case BUTTON_PRESSED:
      MessageBox(m_hWnd, L"You have pressed the button", L"Button Pressed", MB_OK);
      test->findButton(m_hWnd);
      test->setmenu();
      break;

   case MENU_ITEM_SELECT:
      MessageBox(m_hWnd, L"You have simulated a button press with the menu ", L"Menu Pressed", MB_OK);
      break;

    }
    return S_OK;
}

Upvotes: 0

Views: 6324

Answers (2)

sbi
sbi

Reputation: 224079

CRebarHandler *test;

switch (nCmdID){
  case BUTTON_PRESSED:
   MessageBox(m_hWnd, L"You have pressed the button", L"Button Pressed", MB_OK);
   test->findButton(m_hWnd); // <= using test without initialization
   test->setmenu();
// ...

In those last two lines you're using the uninitialized test pointer. Since it wasn't initialized, it might point just anywhere in memory, and the chunk it accidentally points to will be interpreted as a CRebarHandler object. That's undefined behavior at its best, and could do anything. Be glad it blows up right away.

I don't know what a CRebarHandler is, but can't you use one as an automatic object? Something like:

CRebarHandler test( /`...whatever it takes...*/ ); // no pointer

switch (nCmdID){
  case BUTTON_PRESSED:
   MessageBox(m_hWnd, L"You have pressed the button", L"Button Pressed", MB_OK);
   test.findButton(m_hWnd);
   test.setmenu();
// ...

Upvotes: 2

Puppy
Puppy

Reputation: 146930

You declared test, but never assigned anything to it. You have a pointer to nothing. That thing could be NULL or anything. Using it to call a pointer is not safe.

Upvotes: 1

Related Questions