C++ pointer question
-
If I have a pointer to an object foo is there anyway to tell if that object actually exists? Say I only want one window open at a time.
void foo::openWindow() { static foo* ptr = NULL; if (ptr) ptr->closeWindow(); ptr = this; // open this window ... }
So, the second time through this code I want to know if ptr is still valid, because it may have been deleted without my knowledge. I know there are other ways to do this, but I want to know if I can figure out if ptr still exists. I've tried adding a
ptr = dynamic_cast<foo*>(ptr);
, but this throws an exception if ptr has been deleted, and I'd rather not use a try block. Thanks in advance -
If I have a pointer to an object foo is there anyway to tell if that object actually exists? Say I only want one window open at a time.
void foo::openWindow() { static foo* ptr = NULL; if (ptr) ptr->closeWindow(); ptr = this; // open this window ... }
So, the second time through this code I want to know if ptr is still valid, because it may have been deleted without my knowledge. I know there are other ways to do this, but I want to know if I can figure out if ptr still exists. I've tried adding a
ptr = dynamic_cast<foo*>(ptr);
, but this throws an exception if ptr has been deleted, and I'd rather not use a try block. Thanks in advanceI completely fail to see the purpose of your code, but that aside, you can simply check whether the 'this' pointer is NULL. Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
If I have a pointer to an object foo is there anyway to tell if that object actually exists? Say I only want one window open at a time.
void foo::openWindow() { static foo* ptr = NULL; if (ptr) ptr->closeWindow(); ptr = this; // open this window ... }
So, the second time through this code I want to know if ptr is still valid, because it may have been deleted without my knowledge. I know there are other ways to do this, but I want to know if I can figure out if ptr still exists. I've tried adding a
ptr = dynamic_cast<foo*>(ptr);
, but this throws an exception if ptr has been deleted, and I'd rather not use a try block. Thanks in advanceIf there's some function call out there that tells you if a spot in memory is valid that'd be a real easy way of doing this. But I don't know of one. My suggestion. Make ptr a static member of the foo class. Then, in your foo destructor, if ptr == this, set ptr = NULL. Tim
-
If there's some function call out there that tells you if a spot in memory is valid that'd be a real easy way of doing this. But I don't know of one. My suggestion. Make ptr a static member of the foo class. Then, in your foo destructor, if ptr == this, set ptr = NULL. Tim
I know I can do this, but I did not want to add a member for this. Thanks though! Jerry
-
I completely fail to see the purpose of your code, but that aside, you can simply check whether the 'this' pointer is NULL. Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
Yeah but if you create
foo *a = new foo(); foo *b = new foo(); a.openWindow(); b.openWindow(); // This should close a. delete b; // ptr in openWindow() still points // to the space b used to occupy. a.openWindow(); // Trys to close b, but b no longer exists (ERROR).
I know I could create a member and set it to
this
in openWindow andNULL
in closeWindow, but I do not want to have a member for this. If you change the code to use a dynamic_cast and put a try-catch(...) around it this will work as is, but the powers that be don't like try-catch, so I'm not allowed to use it. -
Yeah but if you create
foo *a = new foo(); foo *b = new foo(); a.openWindow(); b.openWindow(); // This should close a. delete b; // ptr in openWindow() still points // to the space b used to occupy. a.openWindow(); // Trys to close b, but b no longer exists (ERROR).
I know I could create a member and set it to
this
in openWindow andNULL
in closeWindow, but I do not want to have a member for this. If you change the code to use a dynamic_cast and put a try-catch(...) around it this will work as is, but the powers that be don't like try-catch, so I'm not allowed to use it.patnsnaudy wrote: but the powers that be don't like try-catch, so I'm not allowed to use it. Not being allowed to use try..catch is wrong, but so is a design that throws exceptions based on bad coding, so on aggregate, I'm with the 'powers that be'. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
-
If I have a pointer to an object foo is there anyway to tell if that object actually exists? Say I only want one window open at a time.
void foo::openWindow() { static foo* ptr = NULL; if (ptr) ptr->closeWindow(); ptr = this; // open this window ... }
So, the second time through this code I want to know if ptr is still valid, because it may have been deleted without my knowledge. I know there are other ways to do this, but I want to know if I can figure out if ptr still exists. I've tried adding a
ptr = dynamic_cast<foo*>(ptr);
, but this throws an exception if ptr has been deleted, and I'd rather not use a try block. Thanks in advanceAdd an operator HWND to the class that returns the window handle and call ::IsWindow. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
-
Add an operator HWND to the class that returns the window handle and call ::IsWindow. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
There is already a function to return HWND I think, if this is derived from the window class (I'll have to check tomorrow at work). ::IsWindow is a great suggestion! I was looking for a magical function that would look at a piece of memory and tell me if it had been deleted, but IsWindow should work. Thanks
-
There is already a function to return HWND I think, if this is derived from the window class (I'll have to check tomorrow at work). ::IsWindow is a great suggestion! I was looking for a magical function that would look at a piece of memory and tell me if it had been deleted, but IsWindow should work. Thanks
-
If I have a pointer to an object foo is there anyway to tell if that object actually exists? Say I only want one window open at a time.
void foo::openWindow() { static foo* ptr = NULL; if (ptr) ptr->closeWindow(); ptr = this; // open this window ... }
So, the second time through this code I want to know if ptr is still valid, because it may have been deleted without my knowledge. I know there are other ways to do this, but I want to know if I can figure out if ptr still exists. I've tried adding a
ptr = dynamic_cast<foo*>(ptr);
, but this throws an exception if ptr has been deleted, and I'd rather not use a try block. Thanks in advanceTo determine if a pointer points to readable memory, use the
IsBadReadPtr()
function. If it returns FALSE, then the pointer cannot be read. Hope this helps,Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Yeah but if you create
foo *a = new foo(); foo *b = new foo(); a.openWindow(); b.openWindow(); // This should close a. delete b; // ptr in openWindow() still points // to the space b used to occupy. a.openWindow(); // Trys to close b, but b no longer exists (ERROR).
I know I could create a member and set it to
this
in openWindow andNULL
in closeWindow, but I do not want to have a member for this. If you change the code to use a dynamic_cast and put a try-catch(...) around it this will work as is, but the powers that be don't like try-catch, so I'm not allowed to use it.It's good coding hygiene to set a deleted pointer to
NULL
. This will help prevent run-time errors and could make your code simpler. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com -
If there's some function call out there that tells you if a spot in memory is valid that'd be a real easy way of doing this. But I don't know of one. My suggestion. Make ptr a static member of the foo class. Then, in your foo destructor, if ptr == this, set ptr = NULL. Tim
TFrancis wrote: If there's some function call out there that tells you if a spot in memory is valid that'd be a real easy way of doing this. But I don't know of one. How about:
IsBadReadPtr()
IsBadCodePtr()
IsBadStringPtr()
IsBadWritePtr()
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
By it will not work does that mean that it will crash if the pointer has been deleted?
-
By it will not work does that mean that it will crash if the pointer has been deleted?
yes,it will very unpredictable, it may crash in some cases and it will work in some cases. but then working with deleted pointer is very bad idea.
MSN Messenger. prakashnadar@msn.com
-
If I have a pointer to an object foo is there anyway to tell if that object actually exists? Say I only want one window open at a time.
void foo::openWindow() { static foo* ptr = NULL; if (ptr) ptr->closeWindow(); ptr = this; // open this window ... }
So, the second time through this code I want to know if ptr is still valid, because it may have been deleted without my knowledge. I know there are other ways to do this, but I want to know if I can figure out if ptr still exists. I've tried adding a
ptr = dynamic_cast<foo*>(ptr);
, but this throws an exception if ptr has been deleted, and I'd rather not use a try block. Thanks in advanceI just created a static pointer to the window and set it to this in the openWindow function and NULL in the dtor. Thanks