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