tags:

views:

26

answers:

2

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;
}
+1  A: 

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.

DeadMG
On a 32bit machine, `NULL` is just one of 2^32 possible values. Depending on what previously used the memory now occupied by `test`, the probability of it being `NULL` might be less than 1/1^32, but it's still very low.
sbi
That's what the "or anything" was for. What is certain is that the probability that it points to a valid object is vanishingly unlikely.
DeadMG
+1  A: 
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();
// ...
sbi