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

delete

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
4 Posts 4 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi: I´ve create a pointer, don´t mind the type object, and sometime when you want to delete it, this can produce an exception, example when you haven´t initialized the pointer(the error, in this case, will be "Access Violation").How could I catch this error? the problem is that the delete instruction doesn´t catch any exception and doesn´t return any value. Thanks. :rose:

    M M 2 Replies Last reply
    0
    • L Lost User

      Hi: I´ve create a pointer, don´t mind the type object, and sometime when you want to delete it, this can produce an exception, example when you haven´t initialized the pointer(the error, in this case, will be "Access Violation").How could I catch this error? the problem is that the delete instruction doesn´t catch any exception and doesn´t return any value. Thanks. :rose:

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

      Always initialize pointer variables to zero, with new or with some other legal pointer value. Use ASSERT_VALID for pointers to MFC objects. Delete will ignore zero pointers so if you take care that any pointer you delete is either zero or points to memory allocated with new you won't have problems. If you want to reuse the pointer variable, zero it after delete.

      1 Reply Last reply
      0
      • L Lost User

        Hi: I´ve create a pointer, don´t mind the type object, and sometime when you want to delete it, this can produce an exception, example when you haven´t initialized the pointer(the error, in this case, will be "Access Violation").How could I catch this error? the problem is that the delete instruction doesn´t catch any exception and doesn´t return any value. Thanks. :rose:

        M Offline
        M Offline
        MikeG 0
        wrote on last edited by
        #3

        I generally use the following macro (the idea originally came from a COM/OLE book 3-4yrs ago): #define DELETE_POINTER( p ) \ { \ if( p != NULL ) { \ delete p; \ } \ p = NULL; \ } So my code is: fx() { CSomeObj pSomeObj = new CSomePbj(); ... DELETE_POINTER( pSomeObj ); } The macro could easily be updated to include a try,catch block around the delete. If your using MFC you can throw in ASSERT_VALID like the other poster suggested. Or if your not using MFC you can lift the code from the MFC Source to determine if the pointer is valid (validadd.cpp, function AfxIsValidAddress) Mike

        C 1 Reply Last reply
        0
        • M MikeG 0

          I generally use the following macro (the idea originally came from a COM/OLE book 3-4yrs ago): #define DELETE_POINTER( p ) \ { \ if( p != NULL ) { \ delete p; \ } \ p = NULL; \ } So my code is: fx() { CSomeObj pSomeObj = new CSomePbj(); ... DELETE_POINTER( pSomeObj ); } The macro could easily be updated to include a try,catch block around the delete. If your using MFC you can throw in ASSERT_VALID like the other poster suggested. Or if your not using MFC you can lift the code from the MFC Source to determine if the pointer is valid (validadd.cpp, function AfxIsValidAddress) Mike

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          delete NULL is perfectly valid and does nothing, which is why all pointers should be initialised to NULL, apart from being able to check them for validity. So the above macro only helps if the pointer is NULL, in which case it would do no harm to delete it. Christian #include "std_disclaimer.h" People who love sausage and respect the law should never watch either one being made. The things that come to those who wait are usually the things left by those who got there first.

          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