是内存分配和取消分配取决于vc ++中的用户上下文吗? [英] IS memory allocation and de-allocation depends on the user context in vc++?

查看:77
本文介绍了是内存分配和取消分配取决于vc ++中的用户上下文吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


这个问题可能很愚蠢,但是对于用户令牌和模拟具有很多意义.
下面是我在win32服务xp/vista中使用的示例代码.

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 />



在上面的代码中,假装为登录用户后,我返回到服务上下文(会话0上下文)后将内存分配给路径(现在,路径位于登录用户的上下文中),如果某些时候我释放了内存,获取异常,但是如果我在恢复之前将其释放,一切都很好.

IS内存分配和取消分配取决于用户上下文吗?



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?

推荐答案

我将不得不查看代码所处的上下文,以告诉您这是什么原因导致的问题,机器人您应该在释放指针后始终将其设置为NULL:
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;
}




在上下文中,我的意思是控制流:如果free可以被多次调用,如果它处于循环中,是否RevertToSelf也可以访问(释放)路径变量,如果在执行一些有用的工作"中释放了该路径.部分.




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.


嗨tolw,
我必须查看您的代码所在的上下文,才能告诉您这是否是导致问题的原因"

您想要什么上下文,我明确指出此代码是服务.要读取一些当前用户注册表项,我将模拟为当前用户(并分配了内存),读取后,我将恢复为服务上下文并释放内存(将其设置为null).此函数调用仅一次,因此不会出现重入问题.


1. free在完整功能中仅被调用一次,它们没有多重释放.
2.已分配的内存用于从注册表中读取数据,因此不会在路径上进行任何操作.
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.


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.


是否"有时我遇到异常"的意思是"有时效果很好"?如果是这种情况,那么我怀疑它是否真的取决于用户上下文.
:)
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.
:)


这篇关于是内存分配和取消分配取决于vc ++中的用户上下文吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆