Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
This question might be silly but has lot of meaning with respect to the user tokens and impersonation.
example code is below which i am using in a win32 service xp/vista.

<br />
<br />
/* get the logged on user token*/<br />
ImpersonateLoggedOnUser(hPToken);<br />
char* path = (char*)malloc(1024*sizeof(char));<br />
<br />
/* do some use full work */<br />
RevertToSelf();<br />
if (path != NULL)<br />
    free(path);<br />


In the above code, after impersonating as a logged on user i allocated memory to path (now path is in context of the logged on user) after reverting back to the service context (session 0 context) if is free the memory some times i am getting exception, but if i free it before reverting back every thing is fine.

IS memory allocation and de-allocation depends on the user context?
Posted

I would have to see the context your code is in to tell you if this is what causes the problem, bot you should always set the pointer to NULL after freeing it:
if (path != NULL)
{
    free(path);
    path = NULL;
}


EDIT:
By context I mean the control flow: if the free can be called more than once, if it was in a loop, if RevertToSelf can also access (free) the path variable, if the path is freed in the "do some usefull work" part.
 
Share this answer
 
v2
hi tolw,
"I would have to see the context your code is in to tell you if this is what causes the problem"

what context you wanted, i clearly stated that this code is a service. To read some Current user registry key i am impersonating as current user (and the memory is allocated) and after reading it i am reverting back to service context and freeing the memory (setting it to null). This function call is only once so no problem of re-entrency.

EDIT:
1. The free is called only once in complete function, their is no multiple freeing.
2. Allocated memory is used to read the data from the registry so no operation will be done on path.
 
Share this answer
 
v2
Does 'some times i am getting exception' mean 'some times it works fine'? If it is the case then I doubt it really depends on the user context.
:)
 
Share this answer
 
v2
hi pallini,
That is the situation which causing me real problem, but after is re organized the code such that path is freed before calling Reverttoself their was no exception raised at all.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900