内存泄漏使用PBYTE [英] Memory Leak using PBYTE

查看:208
本文介绍了内存泄漏使用PBYTE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临记忆泄漏问题:



这是我的代码片段



I am facing a memory Leak issue :

This is my code snippet

STRUCTPOINTER mystruct = NULL;
PBYTE myByte = NULL ;

mystruct = FillMyStruct();

myByte = &myStruct->BUFFER[0];

if (mystruct)
{
     VirtualFree(mystruct , 0, MEM_RELEASE);
}





我应该清除myByte Memory。,我应该怎么做以及在哪里做。我在内存泄漏上面的代码。





Should i clear myByte Memory., how and where i should do it., i am getting memory leak in the above piece of code.

// STRUCTPOINTER DEFENTION
typedef struct {

    LARGE_INTEGER myint1;
    LARGE_INTEGER myint2;
    BYTE  Buffer[1];

} MYSTRUCT, *STRUCTPOINTER ;

推荐答案

你绝对不能释放缓冲区!它是MyStruct的一部分,因此在MyStruct被释放时被释放。



为什么要使用VirtualAlloc / VirtualFree分配和释放该结构?您可能想要使用new / delete。在我看来,这是你的代码的主要问题。
You must absolutely NOT free the buffer! It is a part of MyStruct and hence is freed when MyStruct is freed.

Why do you allocate and free that structure with VirtualAlloc / VirtualFree? You probably want to use new / delete instead. And that seems to me the major problem of your code.


乍一看你不应该。但是它实际上取决于 FillMyStruct 的实现。
At first glance you should not. However it really depends on the implementation of FillMyStruct.


我认为,这将由FillMyStruct的实现决定。

如何分配MYSTRUCT.Buffer。
I think, this will be decide by the implementation of FillMyStruct.
And how do you allocate the MYSTRUCT.Buffer.


这篇关于内存泄漏使用PBYTE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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