P
Pat
Hi all,
I have a really awkward situation that is causing memory leak problems.
I'm passing data to a driver, and unfortunately, the driver code is not
something I can change, and it is written in C, so it deals with the data
as a big BYTE array. Basically, the driver expects a struct, followed
immediately in memory by a big chunk of raw BYTE data.
The size of the array of BYTEs is determined by certain members of the
struct. So here is how I allocate the data:
typedef struct
{
WORD data_size;
// (other fields here)
} data_header;
...
data_header *allocate_data_block(WORD data_size)
{
BYTE *p = new BYTE[sizeof(data_header) + data_size];
if (p)
{
data_header *dh = (data_header *)p;
dh->data_size = data_size;
return dh;
}
return NULL;
}
...
void some_function_in_my_code()
{
// Allocate a data block with 20 extra bytes
data_header *my_header = allocate_data_block(20);
// Fill in the other fields in the header
data_header->x = 1;
data_header->y = 2;
data_header->z = 3;
// Fill in the BYTE data
BYTE *data = (BYTE *)(data_header) + sizeof(data_header);
data[0] = 100;
data[1] = 101;
data[2] = 102;
// ...
// Send the data to the driver
some_driver_function(data_header, sizeof(data_header) + 20);
// Delete the data
// ???
}
Everything up to the call to the driver is working fine. But how do I go
about safely deleting the allocated memory? I guess the real question
is, how does "delete" know how much to delete?
For example, if I just say "delete my_header;" will that delete only
sizeof(data_header) and leave the remaining 20 bytes as a memory leak?
Or, is it safe to cast it back to a byte array, like this:
BYTE *delete_it = (BYTE *)my_header;
delete[] delete_it;
?
Thanks,
Pat
I have a really awkward situation that is causing memory leak problems.
I'm passing data to a driver, and unfortunately, the driver code is not
something I can change, and it is written in C, so it deals with the data
as a big BYTE array. Basically, the driver expects a struct, followed
immediately in memory by a big chunk of raw BYTE data.
The size of the array of BYTEs is determined by certain members of the
struct. So here is how I allocate the data:
typedef struct
{
WORD data_size;
// (other fields here)
} data_header;
...
data_header *allocate_data_block(WORD data_size)
{
BYTE *p = new BYTE[sizeof(data_header) + data_size];
if (p)
{
data_header *dh = (data_header *)p;
dh->data_size = data_size;
return dh;
}
return NULL;
}
...
void some_function_in_my_code()
{
// Allocate a data block with 20 extra bytes
data_header *my_header = allocate_data_block(20);
// Fill in the other fields in the header
data_header->x = 1;
data_header->y = 2;
data_header->z = 3;
// Fill in the BYTE data
BYTE *data = (BYTE *)(data_header) + sizeof(data_header);
data[0] = 100;
data[1] = 101;
data[2] = 102;
// ...
// Send the data to the driver
some_driver_function(data_header, sizeof(data_header) + 20);
// Delete the data
// ???
}
Everything up to the call to the driver is working fine. But how do I go
about safely deleting the allocated memory? I guess the real question
is, how does "delete" know how much to delete?
For example, if I just say "delete my_header;" will that delete only
sizeof(data_header) and leave the remaining 20 bytes as a memory leak?
Or, is it safe to cast it back to a byte array, like this:
BYTE *delete_it = (BYTE *)my_header;
delete[] delete_it;
?
Thanks,
Pat