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. Guarding delete

Guarding delete

Scheduled Pinned Locked Moved C / C++ / MFC
questiondebugginghelptutorialannouncement
6 Posts 5 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.
  • J Offline
    J Offline
    Joe Moldovan
    wrote on last edited by
    #1

    Does anyone know of a way to guard a delete operator from trying to release a non-allocated block, as in the following trivial example. short x[ 100 ]; try { delete []x; } catch( ... ) { return MDERROR; } This does not work as the exception is caught somewhere in the bowels of the system and I get either an assert or a user breakpoint box. I have looked at RTTI but it doesn't help me. How can I prevent a crash if I'm passed a pointer to a global or static block? Thanks all!

    J L J 3 Replies Last reply
    0
    • J Joe Moldovan

      Does anyone know of a way to guard a delete operator from trying to release a non-allocated block, as in the following trivial example. short x[ 100 ]; try { delete []x; } catch( ... ) { return MDERROR; } This does not work as the exception is caught somewhere in the bowels of the system and I get either an assert or a user breakpoint box. I have looked at RTTI but it doesn't help me. How can I prevent a crash if I'm passed a pointer to a global or static block? Thanks all!

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      This function determines whether a given pointer belongs in the stack or not:

      bool is_stack_memory(void * p)
      {
      char check;
      MEMORY_BASIC_INFORMATION m1,m2;
       
      ::ZeroMemory(&m1,sizeof(m1));
      ::ZeroMemory(&m2,sizeof(m2));
      ::VirtualQuery(p,&m1,sizeof(m1));
      ::VirtualQuery(&check,&m2,sizeof(m2));
       
      return m1.BaseAddress==m2.BaseAddress;
      }

      So you can write code like

      if(is_stack_memory(x)){
      printf("x has not been dynamically allocated\n");
      return MDERROR; // or whatever
      }
      else{
      delete [] x;
      }

      Anyway, IMHO designing functions around this feature is not a very good idea. I'd restrict the ability to determine whether a pointer is deleteable to doing error checking in debug mode. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo PS: There's a limitation in is_stack_memory, namely that it fails to identify pointers on other threads' stacks. This could be worked around, however.

      J 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        This function determines whether a given pointer belongs in the stack or not:

        bool is_stack_memory(void * p)
        {
        char check;
        MEMORY_BASIC_INFORMATION m1,m2;
         
        ::ZeroMemory(&m1,sizeof(m1));
        ::ZeroMemory(&m2,sizeof(m2));
        ::VirtualQuery(p,&m1,sizeof(m1));
        ::VirtualQuery(&check,&m2,sizeof(m2));
         
        return m1.BaseAddress==m2.BaseAddress;
        }

        So you can write code like

        if(is_stack_memory(x)){
        printf("x has not been dynamically allocated\n");
        return MDERROR; // or whatever
        }
        else{
        delete [] x;
        }

        Anyway, IMHO designing functions around this feature is not a very good idea. I'd restrict the ability to determine whether a pointer is deleteable to doing error checking in debug mode. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo PS: There's a limitation in is_stack_memory, namely that it fails to identify pointers on other threads' stacks. This could be worked around, however.

        J Offline
        J Offline
        Jon Hulatt
        wrote on last edited by
        #3

        or, if you ALWAYS init pointers to NULL, and ALWAYS reset them to NULL after delete, like I do, then:

        if (pointer)
        delete [] pointer;
        pointer = NULL;

        Sorry to dissapoint you all with my lack of a witty or poignant signature.

        1 Reply Last reply
        0
        • J Joe Moldovan

          Does anyone know of a way to guard a delete operator from trying to release a non-allocated block, as in the following trivial example. short x[ 100 ]; try { delete []x; } catch( ... ) { return MDERROR; } This does not work as the exception is caught somewhere in the bowels of the system and I get either an assert or a user breakpoint box. I have looked at RTTI but it doesn't help me. How can I prevent a crash if I'm passed a pointer to a global or static block? Thanks all!

          L Offline
          L Offline
          Le centriste
          wrote on last edited by
          #4

          If I were you, I would review my design. By this I mean that an object shouldn't deallocate memory it didn't allocate (which is obviously the case here since your object can't figure out if it is stack or heap memory), unless you have a good reason. If I am wrong or said something stupid, I apologize in advance ;) Michel

          1 Reply Last reply
          0
          • J Joe Moldovan

            Does anyone know of a way to guard a delete operator from trying to release a non-allocated block, as in the following trivial example. short x[ 100 ]; try { delete []x; } catch( ... ) { return MDERROR; } This does not work as the exception is caught somewhere in the bowels of the system and I get either an assert or a user breakpoint box. I have looked at RTTI but it doesn't help me. How can I prevent a crash if I'm passed a pointer to a global or static block? Thanks all!

            J Offline
            J Offline
            Joe Moldovan
            wrote on last edited by
            #5

            Thanks for your thoughts. I didn't design or write this stuff. I'm only trying to wrap an audio interface I got pre-cooked. I watch my own new's and delete's like a hawk and always have the leakage detector on. The bit about nulling the pointer is very good (simple and obvious) and will be standard in my code from now on. I do however find it strange that delete doesn't throw a catchable exception. There must be a good reason for it. I will try it on Linux and see if it tells me why.

            M 1 Reply Last reply
            0
            • J Joe Moldovan

              Thanks for your thoughts. I didn't design or write this stuff. I'm only trying to wrap an audio interface I got pre-cooked. I watch my own new's and delete's like a hawk and always have the leakage detector on. The bit about nulling the pointer is very good (simple and obvious) and will be standard in my code from now on. I do however find it strange that delete doesn't throw a catchable exception. There must be a good reason for it. I will try it on Linux and see if it tells me why.

              M Offline
              M Offline
              Mukkie
              wrote on last edited by
              #6

              Hi! Try using: __try { } __except(1) { } Mukkie

              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