flynny
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.
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
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)
RtlMoveMemory(nHook.window
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
}
}
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Don't forget to free the memory again once you don't need it any more !!! Otherwise you'll have a memory leak.
ASKER
brilliant thank you so much for your help
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.
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;
}
for(i=0; i<maxHooks; i++)
{
if(hookFile.hooks[i] == NULL)
break;
GlobalFree(hookFile.hooks[
GlobalFree(hookFile.hooks[
hookFile.hooks[i] = NULL;
}
At which line does the code crash ?