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. ATL / WTL / STL
  4. function to delete a class (resolved)

function to delete a class (resolved)

Scheduled Pinned Locked Moved ATL / WTL / STL
comdebuggingperformance
9 Posts 2 Posters 0 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.
  • B Offline
    B Offline
    bkelly13
    wrote on last edited by
    #1

    This application has several pointers to be deleted in the destructor. Each is logged when created and again when deleted to reduce the possibility of an undetected memory leak. As there are several, a function is needed to do the deletes. The goal is to avoid writing all that logging code multiple times. Below is the concept. The first time it was tried the subject was an instance of a class with its own destructor. A breakpoint was placed in that destructor.

    Void Delete_Stuff( WCHAR * name, void * item )
    {
    If( item != NULL )
    {
    Write_Log_Entry( L”Preparing to delete <name> at address <*item>”); // please see note
    delete ( item ); // does not call the destructor
    delete ( *item ); // illegal indirection
    }
    }

    EDIT: I wrote the text up in MS word and did not notice that the leading character of "delete" had been capitalized. Apologies for that. Note: An swprintf_s(…) is used to build the string and the named function does the logging. Please accept the gross simplification used to make the main point obvious. The destructor for the object is not called. I have tried a few combinations including * *. I managed to corrupt the heap at least once. Can this be fixed and work as desired or must each item be deleted one at a time without using the Delete_Stuff( … ) function.

    Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

    L 1 Reply Last reply
    0
    • B bkelly13

      This application has several pointers to be deleted in the destructor. Each is logged when created and again when deleted to reduce the possibility of an undetected memory leak. As there are several, a function is needed to do the deletes. The goal is to avoid writing all that logging code multiple times. Below is the concept. The first time it was tried the subject was an instance of a class with its own destructor. A breakpoint was placed in that destructor.

      Void Delete_Stuff( WCHAR * name, void * item )
      {
      If( item != NULL )
      {
      Write_Log_Entry( L”Preparing to delete <name> at address <*item>”); // please see note
      delete ( item ); // does not call the destructor
      delete ( *item ); // illegal indirection
      }
      }

      EDIT: I wrote the text up in MS word and did not notice that the leading character of "delete" had been capitalized. Apologies for that. Note: An swprintf_s(…) is used to build the string and the named function does the logging. Please accept the gross simplification used to make the main point obvious. The destructor for the object is not called. I have tried a few combinations including * *. I managed to corrupt the heap at least once. Can this be fixed and work as desired or must each item be deleted one at a time without using the Delete_Stuff( … ) function.

      Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      That is not the C++ delete Operator[^]. What is happening in your Delete function?

      B 1 Reply Last reply
      0
      • L Lost User

        That is not the C++ delete Operator[^]. What is happening in your Delete function?

        B Offline
        B Offline
        bkelly13
        wrote on last edited by
        #3

        It stepped through the delete but the destructor for that class/object was not called. Note: I wrote the text up in MS word and did not notice that the leading character of "delete" had been capitalized. Apologies for that.

        Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

        L 1 Reply Last reply
        0
        • B bkelly13

          It stepped through the delete but the destructor for that class/object was not called. Note: I wrote the text up in MS word and did not notice that the leading character of "delete" had been capitalized. Apologies for that.

          Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          OK, but what is your Delete method actually doing?

          B 1 Reply Last reply
          0
          • L Lost User

            OK, but what is your Delete method actually doing?

            B Offline
            B Offline
            bkelly13
            wrote on last edited by
            #5

            I do not have a delete function. The parens were used in an attempt to tell the standard delete operation that I want to delete the object pointed to by the pointer. An indirect delete rather than a direct delete.

            class widget{ ... }

            p_widget = new widget;
            ...
            delete_stuff( L"widget", p_widget );
            ...
            void delete_stuff( WCHAR * name, void * item )
            {
            delete *item;
            ... logging code not shown.
            }

            The destructor of widget has a breakpoint that was never hit. I presume that widget was not deleted. Because that did not work, I tried dereferencing and grouping the pointer as in:

            delete *item;
            and
            delete (*item);

            I am unable to get the breakpoint in the destructor so presume I am doing something wrong.

            Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

            L 2 Replies Last reply
            0
            • B bkelly13

              I do not have a delete function. The parens were used in an attempt to tell the standard delete operation that I want to delete the object pointed to by the pointer. An indirect delete rather than a direct delete.

              class widget{ ... }

              p_widget = new widget;
              ...
              delete_stuff( L"widget", p_widget );
              ...
              void delete_stuff( WCHAR * name, void * item )
              {
              delete *item;
              ... logging code not shown.
              }

              The destructor of widget has a breakpoint that was never hit. I presume that widget was not deleted. Because that did not work, I tried dereferencing and grouping the pointer as in:

              delete *item;
              and
              delete (*item);

              I am unable to get the breakpoint in the destructor so presume I am doing something wrong.

              Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I see the problem; my bad for missing it in your original question. Your function has the signature:

              Void Delete_Stuff( WCHAR * name, void * item )

              so when you call delete item the compiler has no idea what type item is, so just throws it away. You can only call delete on an object, when it is specifically typed so the compiler knows how to deal with it. You may try casting it to the relevant class type, but you would need some way of telling your function what type each pointer is.

              B 1 Reply Last reply
              0
              • B bkelly13

                I do not have a delete function. The parens were used in an attempt to tell the standard delete operation that I want to delete the object pointed to by the pointer. An indirect delete rather than a direct delete.

                class widget{ ... }

                p_widget = new widget;
                ...
                delete_stuff( L"widget", p_widget );
                ...
                void delete_stuff( WCHAR * name, void * item )
                {
                delete *item;
                ... logging code not shown.
                }

                The destructor of widget has a breakpoint that was never hit. I presume that widget was not deleted. Because that did not work, I tried dereferencing and grouping the pointer as in:

                delete *item;
                and
                delete (*item);

                I am unable to get the breakpoint in the destructor so presume I am doing something wrong.

                Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                This is how it needs to be done:

                // Object is a base class (that does not need to do anything)
                class Object
                {
                public:
                Object()
                {
                cout << "Object constructor" << endl;
                };
                virtual ~Object()
                {
                cout << "Object destructor" << endl;
                };
                };

                // Widget, and all other classes need to inherit Object
                class Widget : Object
                {
                public:
                Widget()
                {
                cout << "Widget constructor" << endl;
                };
                ~Widget()
                {
                cout << "Widget destructor" << endl;
                };
                };

                // Delete_Stuff takes an Object pointer as its second parameter
                static void Delete_Stuff(char* name, Object* object)
                {
                cout << "cleanup " << name << endl;

                // delete will now correctly delete any object
                delete object;
                

                }

                1 Reply Last reply
                0
                • L Lost User

                  I see the problem; my bad for missing it in your original question. Your function has the signature:

                  Void Delete_Stuff( WCHAR * name, void * item )

                  so when you call delete item the compiler has no idea what type item is, so just throws it away. You can only call delete on an object, when it is specifically typed so the compiler knows how to deal with it. You may try casting it to the relevant class type, but you would need some way of telling your function what type each pointer is.

                  B Offline
                  B Offline
                  bkelly13
                  wrote on last edited by
                  #8

                  Then I cannot do that. Ok. thanks for taking the time to explain.

                  Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

                  L 1 Reply Last reply
                  0
                  • B bkelly13

                    Then I cannot do that. Ok. thanks for taking the time to explain.

                    Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    See my better explanation below.

                    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