Link to home
Start Free TrialLog in
Avatar of flynny
flynnyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Help using pointers? access violation

Hi,

i'm probably doing something extremely basic wrong but i'm having problems with pointers.

i have a method


Code:

BUTTON* getButton(int i, int j){ return hookFile.hooks[i]->buttons[j]; }

this returns a button ctruscture i have created. Now the hook[i] has been created i know this but the button hasn't when i get this message. i have tried


Code:

BUTTON* rBut = getButton(i,j);
if(rBut == NULL)
 break;

to stop it running to the code causing the error, however this isn't working when i add my hook i set all the buttons to be NULL. but if i put a break point here rBut equals 409e62.


Code:

void addHook(LPTSTR window)
{
      int i;
      for(i=0; i<maxHooks; i++)
            if(hookFile.hooks[i] == NULL)
                  break;

      if(i < maxHooks)
      {
            HOOK nHook;

            HGLOBAL memPtr = GlobalAlloc(GMEM_FIXED, strlen(window)+1); //pointer to mem block

            if( memPtr && (nHook.window = (LPTSTR)GlobalLock(memPtr)) != NULL )
                  RtlMoveMemory(nHook.window, window, strlen(window)+1);
            else
            {
                  char buf[128];
                  sprintf(buf, "Error Unable to allocate Memory for hook file hook:window.");
                  MessageBox(NULL, buf, NULL, 0);
            }

            hookFile.hooks[i] = &nHook;

            int j; //make all NULL
            for(j=0; j<maxButtons; j++)
                  hookFile.hooks[i]->buttons[j] = NULL;
      }
}

Now the structures and add methods i keep in a struct.cpp the method which i read the structure and display it is held in a dialog.cpp. I have noticed that the hooks[i]->window is also returning something which isn't correct.

what am i doing wrong please?

many thanks,


Matt.
Avatar of Infinity08
Infinity08
Flag of Belgium image

Has the buttons array within the HOOK struct hookFile.hooks[i] been allocated ?

At which line does the code crash ?
SOLUTION
Avatar of jasonclarke
jasonclarke

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of flynny

ASKER

hi thanks for the replies the code crashes when i try to insert into a CTreeCtrl, in which i display the structure.

the program crashes when trying to access a variable of the structure, i assumed this is because i it didn't exist although it was returning something.  this was why i was testing to see if it was null.

i think jason is on the right lines here. is there anyway i can make the whole structure globally available?

matt.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Don't forget to free the memory again once you don't need it any more !!! Otherwise you'll have a memory leak.
Avatar of flynny

ASKER

brilliant thank you so much for your help
Avatar of flynny

ASKER

ok so to free the memory all i have to do is free(pHook) for example? i've always had problems with the free function.
Avatar of jasonclarke
jasonclarke

To free all of the memory you would do something like this:

for(i=0; i<maxHooks; i++)
{
    if(hookFile.hooks[i] == NULL)
        break;
    GlobalFree(hookFile.hooks[i] ->window);
    GlobalFree(hookFile.hooks[i]);
    hookFile.hooks[i] = NULL;
}