How to verify a pointer is valid? [modified]
-
class CSomeObject : public CObject { //... }; CSomeObject *pObject = new CSomeObject; CSomeObject *pSave = pObject; //.. //some other operation, the pObject may be deleted or not //but pSave don't know what happened
But now I want to use the pointer of pSave, can I verify if pSave is valid or not before I use it? try/catch? or any method else? -- modified at 23:26 Tuesday 13th June, 2006 -
class CSomeObject : public CObject { //... }; CSomeObject *pObject = new CSomeObject; CSomeObject *pSave = pObject; //.. //some other operation, the pObject may be deleted or not //but pSave don't know what happened
But now I want to use the pointer of pSave, can I verify if pSave is valid or not before I use it? try/catch? or any method else? -- modified at 23:26 Tuesday 13th June, 2006Actually new returns '0' if it is unsuccessful. you can use
IsBadWritePtr
orIsBadReadPtr
to validate the pointer. new will throw exception if it fails, u can alsocatch
for that exception See MSDN for more details SaRath.
"Don't Do Different things... Do Things Differently..." Understanding State Pattern in C++ -
class CSomeObject : public CObject { //... }; CSomeObject *pObject = new CSomeObject; CSomeObject *pSave = pObject; //.. //some other operation, the pObject may be deleted or not //but pSave don't know what happened
But now I want to use the pointer of pSave, can I verify if pSave is valid or not before I use it? try/catch? or any method else? -- modified at 23:26 Tuesday 13th June, 2006flyingxu wrote:
But now I want to use the pointer of pSave, can I verify if pSave is valid or not before I use it? try/catch? or any method else?
Look up
_set_new_handler
,_set_new_mode
,_query_new_handler
,AfxSetNewHandler (this is not documented)
. The above functions transfers control to your error-handling mechanism if the new operator fails to allocate memory. These functions (except for_set_new_mode
and_query_new_handler
) take a function pointer as an argument and returns the old one. The_query_new_handler
function returns the address of the current new handler. Make sure you set the old one back before exiting.PNH old_handler = _set_new_handler( my_handler );
// Code that requires my_handler
_set_new_handler( old_handler )
// Code that requires old_handler
Nibu thomas A Developer Programming tips[^] My site[^]
-
class CSomeObject : public CObject { //... }; CSomeObject *pObject = new CSomeObject; CSomeObject *pSave = pObject; //.. //some other operation, the pObject may be deleted or not //but pSave don't know what happened
But now I want to use the pointer of pSave, can I verify if pSave is valid or not before I use it? try/catch? or any method else? -- modified at 23:26 Tuesday 13th June, 2006Of course, it is offtopic, but I would say that requirement to verify if pointer is good or not is a result of bad objects intraction design. There never should be invalid pointers, because you will forget to verify it once - either now or when you will have to modify your code one year later. Igor Green http://www.grigsoft.com/ - files and folders comparison tools
-
Actually new returns '0' if it is unsuccessful. you can use
IsBadWritePtr
orIsBadReadPtr
to validate the pointer. new will throw exception if it fails, u can alsocatch
for that exception See MSDN for more details SaRath.
"Don't Do Different things... Do Things Differently..." Understanding State Pattern in C++Using
IsBadWritePtr
and friends will not solve the OP's problem.IsBadWritePtr
can returnFALSE
even after an object as been deleted as heap blocks can be marked as free but the memory still be allocated. On top of this the CRT can add another level of caching. In short, ifIsBadWritePtr
returnsTRUE
you know something is wrong and it's not safe to use the memory, but if returnsFALSE
it does not necessarily mean all is well. Steve -
class CSomeObject : public CObject { //... }; CSomeObject *pObject = new CSomeObject; CSomeObject *pSave = pObject; //.. //some other operation, the pObject may be deleted or not //but pSave don't know what happened
But now I want to use the pointer of pSave, can I verify if pSave is valid or not before I use it? try/catch? or any method else? -- modified at 23:26 Tuesday 13th June, 2006 -
class CSomeObject : public CObject { //... }; CSomeObject *pObject = new CSomeObject; CSomeObject *pSave = pObject; //.. //some other operation, the pObject may be deleted or not //but pSave don't know what happened
But now I want to use the pointer of pSave, can I verify if pSave is valid or not before I use it? try/catch? or any method else? -- modified at 23:26 Tuesday 13th June, 2006flyingxu wrote:
But now I want to use the pointer of pSave, can I verify if pSave is valid or not before I use it? try/catch?
Use ASSERT in Debug mode B'coz it works in Debug mode only. Knock out 't' from can't, You can if you think you can :cool:
-
class CSomeObject : public CObject { //... }; CSomeObject *pObject = new CSomeObject; CSomeObject *pSave = pObject; //.. //some other operation, the pObject may be deleted or not //but pSave don't know what happened
But now I want to use the pointer of pSave, can I verify if pSave is valid or not before I use it? try/catch? or any method else? -- modified at 23:26 Tuesday 13th June, 2006You may use a smart pointer that implements reference counting, e.g. boost::shared_ptr, instead of raw pointer.