Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Does "delete" operator work for PVOID pointers?

Does "delete" operator work for PVOID pointers?

Scheduled Pinned Locked Moved C / C++ / MFC
question
12 Posts 8 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cold_Fearing_Bird
    wrote on last edited by
    #1

    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.

    M R C 3 Replies Last reply
    0
    • C Cold_Fearing_Bird

      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.

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • C Cold_Fearing_Bird

        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.

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #3

        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.

        C 1 Reply Last reply
        0
        • M Maximilien

          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.

          A Offline
          A Offline
          Albert Holguin
          wrote on last edited by
          #4

          I'm pretty sure it'll work with standard types, and for classes, it's undefined behavior. So it should be avoided anyway.

          1 Reply Last reply
          0
          • R Rajesh R Subramanian

            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.

            C Offline
            C Offline
            Cold_Fearing_Bird
            wrote on last edited by
            #5

            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.

            C D S 3 Replies Last reply
            0
            • C Cold_Fearing_Bird

              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.

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #6

              no does that even compile?

              image processing toolkits | batch image processing

              M 1 Reply Last reply
              0
              • C Cold_Fearing_Bird

                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.

                C Offline
                C Offline
                Chris Losinger
                wrote on last edited by
                #7

                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.

                image processing toolkits | batch image processing

                1 Reply Last reply
                0
                • C Chris Losinger

                  no does that even compile?

                  image processing toolkits | batch image processing

                  M Offline
                  M Offline
                  Maximilien
                  wrote on last edited by
                  #8

                  it does compiles on VS2008.

                  Watched code never compiles.

                  1 Reply Last reply
                  0
                  • C Cold_Fearing_Bird

                    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.

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    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

                    Richard Andrew x64R 1 Reply Last reply
                    0
                    • D David Crow

                      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

                      Richard Andrew x64R Offline
                      Richard Andrew x64R Offline
                      Richard Andrew x64
                      wrote on last edited by
                      #10

                      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.

                      D 1 Reply Last reply
                      0
                      • Richard Andrew x64R Richard Andrew x64

                        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.

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        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

                        1 Reply Last reply
                        0
                        • C Cold_Fearing_Bird

                          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.

                          S Offline
                          S Offline
                          Stefan_Lang
                          wrote on last edited by
                          #12

                          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() or delete (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 a release() 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.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups