Click here to Skip to main content
15,849,304 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am facing a memory Leak issue :

This is my code snippet

C++
STRUCTPOINTER mystruct = NULL;
PBYTE myByte = NULL ;

mystruct = FillMyStruct();

myByte = &myStruct->BUFFER[0];

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


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

C++
// STRUCTPOINTER DEFENTION
typedef struct {

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

} MYSTRUCT, *STRUCTPOINTER ;
Posted
Updated 5-Jul-13 2:12am
v2

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.
 
Share this answer
 
Comments
pasztorpisti 6-Jul-13 16:36pm    
+5, the problem is probably the use of VirtualFree(). Since he found out that there is a leak (probably because Visual Studio reported it) it is possible that the struct has been allocated by malloc/new in some way.
nv3 7-Jul-13 4:20am    
Thanks! And yes, that's what I think as well. From a comment to Solution 2 I could see that he allocates the struct with VirtualAlloc. Probably copied that from some misleading example code.
At first glance you should not. However it really depends on the implementation of FillMyStruct.
 
Share this answer
 
Comments
gssajith87 5-Jul-13 7:09am    
My Structure is
typedef struct {
LARGE_INTEGER StartingLcn;
LARGE_INTEGER BitmapSize;
BYTE Buffer[1];
} VOLUME_BITMAP_BUFFER, *PVOLUME_BITMAP_BUFFER;

and FillMyStruct is an IOCTL call, that fills the structure.
CPallini 5-Jul-13 7:52am    
I already know the struct definition (you proved it in the OP). However I still ignore the actual FillMyStruct implementation. 'is an IOCTL call, that fills the structure' is not enough info.
I think, this will be decide by the implementation of FillMyStruct.
And how do you allocate the MYSTRUCT.Buffer.
 
Share this answer
 
Comments
gssajith87 5-Jul-13 7:19am    
allocation of MyStruct.buffer is done by IOCTL CALL., FSCTL_GET_VOLUME_BITMAP
gssajith87 5-Jul-13 7:24am    
This is how i do allocate memory:

mystruct = (PVOLUME_BITMAP_BUFFER) VirtualAlloc(NULL,
dwBitmapBytes + 5120, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);

This is how i fill the structure :

res = DeviceIoControl(hVolumeHandle, FSCTL_GET_VOLUME_BITMAP,
&iLCN, sizeof(iLCN), mystruct , dwinSize,
&dwOutSize, NULL);


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