Does "delete" operator work for PVOID pointers?
-
INT* pInt = new int ; LPVOID pVoid = pInt ; delete pVoid ; will pVoid(object) be deleted cleanly as "delete pInt" does? I am a little worried since there's no way to indicate the object size.
-
INT* pInt = new int ; LPVOID pVoid = pInt ; delete pVoid ; will pVoid(object) be deleted cleanly as "delete pInt" does? I am a little worried since there's no way to indicate the object size.
With "basic" types it might work. With doing this with a "real" classes it will not call the class destructor and will lead to memory leaks
Watched code never compiles.
-
INT* pInt = new int ; LPVOID pVoid = pInt ; delete pVoid ; will pVoid(object) be deleted cleanly as "delete pInt" does? I am a little worried since there's no way to indicate the object size.
delete
will try to call the destructor of the object that the pointer is pointing to, and then deallocate the memory associated (by calling operator delete for that object). If you cast the type away to void, then there's no destructor associated, and as a result, all the memory will not be de-allocated properly. I also do not understand why would you need to do such a thing."Real men drive manual transmission" - Rajesh.
-
With "basic" types it might work. With doing this with a "real" classes it will not call the class destructor and will lead to memory leaks
Watched code never compiles.
I'm pretty sure it'll work with standard types, and for classes, it's undefined behavior. So it should be avoided anyway.
-
delete
will try to call the destructor of the object that the pointer is pointing to, and then deallocate the memory associated (by calling operator delete for that object). If you cast the type away to void, then there's no destructor associated, and as a result, all the memory will not be de-allocated properly. I also do not understand why would you need to do such a thing."Real men drive manual transmission" - Rajesh.
I use lParam of LVITEM to hold some struct address like CREATESTRUCT, PAINTSTRUCT, I new/malloc blocks of memory for them. When the listview control is about to destroy. I want to delete/free the lParam which points to a struct object. Because the object is dynamically allocated, I cannot determine its exact type. so I wish I could do the lazy job, simply delete/free the pVoid expecting that wouldn't cause too much trouble.All of windows structures don't have constructors.
-
INT* pInt = new int ; LPVOID pVoid = pInt ; delete pVoid ; will pVoid(object) be deleted cleanly as "delete pInt" does? I am a little worried since there's no way to indicate the object size.
no does that even compile?
-
I use lParam of LVITEM to hold some struct address like CREATESTRUCT, PAINTSTRUCT, I new/malloc blocks of memory for them. When the listview control is about to destroy. I want to delete/free the lParam which points to a struct object. Because the object is dynamically allocated, I cannot determine its exact type. so I wish I could do the lazy job, simply delete/free the pVoid expecting that wouldn't cause too much trouble.All of windows structures don't have constructors.
this seems like a good place for an abstract base class with a virtual destructor and a derived class for each of the types you want to pass around. or, you could do it with a simple struct:
struct cMyStruct
{
typedef enum {eInt=0, eDouble, eCreateStruct, ePaintStruct} eTypeID;
void *pData;
eTypeID typeID;cleanUp()
{
switch (typeID)
{
case eInt:
{
int *p = (int *)pData;
delete p;
pData = NULL;
}
break;
etc...
}
}
};just pass that around, initialized properly, of course.
-
no does that even compile?
it does compiles on VS2008.
Watched code never compiles.
-
I use lParam of LVITEM to hold some struct address like CREATESTRUCT, PAINTSTRUCT, I new/malloc blocks of memory for them. When the listview control is about to destroy. I want to delete/free the lParam which points to a struct object. Because the object is dynamically allocated, I cannot determine its exact type. so I wish I could do the lazy job, simply delete/free the pVoid expecting that wouldn't cause too much trouble.All of windows structures don't have constructors.
Cold_Fearing_Bird wrote:
Because the object is dynamically allocated, I cannot determine its exact type.
So the items in the listview "point to" different types of data? If that's the case, wrap the data and the type in a struct like:
struct data_and_type
{
int type; // 0=CREATESTRUCT, 1=PAINTSTRUCT
PVOID data;
};data_and_type *dt = new data_and_type;
dt->type = 0;
dt->data = (CREATESTRUCT *) new CREATESTRUCT;SetItemData(..., (DWORD) dt);
Now when you are ready to delete:
dt = (data_and_type *) GetItemData();
if (dt->type == 0)
{
CREATESTRUCT *cs = (CREATESTRUCT *) dt->data;
delete cs;
}
else if (dt->type == 1)
...Clear as mud?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
Cold_Fearing_Bird wrote:
Because the object is dynamically allocated, I cannot determine its exact type.
So the items in the listview "point to" different types of data? If that's the case, wrap the data and the type in a struct like:
struct data_and_type
{
int type; // 0=CREATESTRUCT, 1=PAINTSTRUCT
PVOID data;
};data_and_type *dt = new data_and_type;
dt->type = 0;
dt->data = (CREATESTRUCT *) new CREATESTRUCT;SetItemData(..., (DWORD) dt);
Now when you are ready to delete:
dt = (data_and_type *) GetItemData();
if (dt->type == 0)
{
CREATESTRUCT *cs = (CREATESTRUCT *) dt->data;
delete cs;
}
else if (dt->type == 1)
...Clear as mud?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
DavidCrow wrote:
dt->data = (CREATESTRUCT *) new CREATESTRUCT;
Any particular reason for the explicit cast?
The difficult we do right away... ...the impossible takes slightly longer.
-
DavidCrow wrote:
dt->data = (CREATESTRUCT *) new CREATESTRUCT;
Any particular reason for the explicit cast?
The difficult we do right away... ...the impossible takes slightly longer.
Richard Andrew x64 wrote:
Any particular reason for the explicit cast?
Yes. I had not typed enough this morning and was simply trying to meet my quota. :-O
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
I use lParam of LVITEM to hold some struct address like CREATESTRUCT, PAINTSTRUCT, I new/malloc blocks of memory for them. When the listview control is about to destroy. I want to delete/free the lParam which points to a struct object. Because the object is dynamically allocated, I cannot determine its exact type. so I wish I could do the lazy job, simply delete/free the pVoid expecting that wouldn't cause too much trouble.All of windows structures don't have constructors.
Cold_Fearing_Bird wrote:
I new/malloc blocks
Are you sayiny you can't even tell what method you used to allocate memory? In that case you must make sure that you are calling the proper destruction method, i. e. either
free()
ordelete
(or,maybe delete []
?). I fyou don't take care of that and use the wrong method, your program will crash at runtime. Wherever you get the pointer from, you must add arelease()
method there that takes care of the proper destruction and deallocation. This most probably will involve virtual destructors in case of classes, or some other method of indirection.