validating handles and pointers
-
Hello board, There is a function that checks if a pointer is in valid memory space in MFC, I have used that before but now I forgot that and MSDN help is so crap and stupid that despite that I've tried many keywords it returns completely unrelated subject. No wonder MSN never can match google. Can anybody give me the name of that function, please to check validity of a pointer. Also is there any function that checks validity of a handle. When I use CloseHandle, in debug mode sometimes an exception occurs that says handle value is invalid. while I usually define handle value as INVALID_HANDLE_VALUE in the beginning and my expression is
if(m_hHandle != INVALID_HANDLE_VALUE) CloseHandle(m_hHandle);
but exception occurs and the value of handle is other than INVALID_HANDLE_VALUE Thanks and have a nice day. -
Hello board, There is a function that checks if a pointer is in valid memory space in MFC, I have used that before but now I forgot that and MSDN help is so crap and stupid that despite that I've tried many keywords it returns completely unrelated subject. No wonder MSN never can match google. Can anybody give me the name of that function, please to check validity of a pointer. Also is there any function that checks validity of a handle. When I use CloseHandle, in debug mode sometimes an exception occurs that says handle value is invalid. while I usually define handle value as INVALID_HANDLE_VALUE in the beginning and my expression is
if(m_hHandle != INVALID_HANDLE_VALUE) CloseHandle(m_hHandle);
but exception occurs and the value of handle is other than INVALID_HANDLE_VALUE Thanks and have a nice day.Why do you need such a function ? A much easier (and cleaner) solution is simply to set the pointer to NULL when you destroyed it. Then you simply check if it is NULL or not. Same with the HANDLE.
Cédric Moonen Software developer
Charting control [v1.2] -
Hello board, There is a function that checks if a pointer is in valid memory space in MFC, I have used that before but now I forgot that and MSDN help is so crap and stupid that despite that I've tried many keywords it returns completely unrelated subject. No wonder MSN never can match google. Can anybody give me the name of that function, please to check validity of a pointer. Also is there any function that checks validity of a handle. When I use CloseHandle, in debug mode sometimes an exception occurs that says handle value is invalid. while I usually define handle value as INVALID_HANDLE_VALUE in the beginning and my expression is
if(m_hHandle != INVALID_HANDLE_VALUE) CloseHandle(m_hHandle);
but exception occurs and the value of handle is other than INVALID_HANDLE_VALUE Thanks and have a nice day.INVALID_HANDLE_VALUE
equals to (-1
). General approach goes as below.HANDLE h = NULL;
// ...
// Now you are going to do something.
//
// Type 1: Result from some Windows APIs.
h = CreateFile(...);
if(INVALID_HANDLE_VALUE != h) {
// ...
}
//
// Type 2: Check validity before doing Type 1.
if(!h) {
h = CreateFile(...);
if(INVALID_HANDLE_VALUE == h) {
// Error.
}
}
if(h) {
CloseHandle(h);
h = NULL; // Set null for your convenience later.
}Maxwell Chen
-
Why do you need such a function ? A much easier (and cleaner) solution is simply to set the pointer to NULL when you destroyed it. Then you simply check if it is NULL or not. Same with the HANDLE.
Cédric Moonen Software developer
Charting control [v1.2]To free memory...
-
INVALID_HANDLE_VALUE
equals to (-1
). General approach goes as below.HANDLE h = NULL;
// ...
// Now you are going to do something.
//
// Type 1: Result from some Windows APIs.
h = CreateFile(...);
if(INVALID_HANDLE_VALUE != h) {
// ...
}
//
// Type 2: Check validity before doing Type 1.
if(!h) {
h = CreateFile(...);
if(INVALID_HANDLE_VALUE == h) {
// Error.
}
}
if(h) {
CloseHandle(h);
h = NULL; // Set null for your convenience later.
}Maxwell Chen
The problem is this handle has to be accessed from different threads. I checked its value before executing CloseHandle() it has some value which is not what I assigned at the beginning and also it is not INVALID_HANDLE_VALUE but still after execution of CloseHandle an exception occurs.
-
The problem is this handle has to be accessed from different threads. I checked its value before executing CloseHandle() it has some value which is not what I assigned at the beginning and also it is not INVALID_HANDLE_VALUE but still after execution of CloseHandle an exception occurs.
Did you set up some synchronization mechanism to assure that
CloseHandle(handle);
handle = INVALID_HANDLE_VALUE;is atomic? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles] -
Hello board, There is a function that checks if a pointer is in valid memory space in MFC, I have used that before but now I forgot that and MSDN help is so crap and stupid that despite that I've tried many keywords it returns completely unrelated subject. No wonder MSN never can match google. Can anybody give me the name of that function, please to check validity of a pointer. Also is there any function that checks validity of a handle. When I use CloseHandle, in debug mode sometimes an exception occurs that says handle value is invalid. while I usually define handle value as INVALID_HANDLE_VALUE in the beginning and my expression is
if(m_hHandle != INVALID_HANDLE_VALUE) CloseHandle(m_hHandle);
but exception occurs and the value of handle is other than INVALID_HANDLE_VALUE Thanks and have a nice day.You need to refer the API documentation, The API that returns HANDLE to system resource has different behaviour on return values, for example CreateFile returns INVALID_HANDLE_VALUE while CreateEvent returns NULL on failure, so checking the handle on CloseHandle depends on the API that created the Handle. Are u looking for _CrtIsValidPointer, _CrtIsValidHeapPointer for pointer validation. AfxIsValidAddress (MFC)
modified on Monday, February 04, 2008 5:52:35 AM
-
To free memory...
And what's the difference with what I suggested (meaning setting your pointer to NULL when you deleted it) ?
Cédric Moonen Software developer
Charting control [v1.2] -
Did you set up some synchronization mechanism to assure that
CloseHandle(handle);
handle = INVALID_HANDLE_VALUE;is atomic? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles]Before running CloseHandle() I terminate those threads. Still I'm sure most of you guys know about the function that checks validity of a pointer to be in valid memory space. I've used it so many times and I now I just forgot that and I can't find it in MSDN crap.
-
Before running CloseHandle() I terminate those threads. Still I'm sure most of you guys know about the function that checks validity of a pointer to be in valid memory space. I've used it so many times and I now I just forgot that and I can't find it in MSDN crap.
Electronic75 wrote:
Still I'm sure most of you guys know about the function that checks validity of a pointer to be in valid memory space.
Never used it myself and I don't think I'll ever need it. If you follow the simple rule I gave you, it's totally unnecessary to use it. I would even say it is bad practice because you don't have a "clean" code.
Cédric Moonen Software developer
Charting control [v1.2] -
And what's the difference with what I suggested (meaning setting your pointer to NULL when you deleted it) ?
Cédric Moonen Software developer
Charting control [v1.2]do you mean if I simply set pointer to null it frees memory no memory leak no resource leak ...??? so the what's the need for delete we can simply assign null to a pointer. can you refer me to a document, please?
-
do you mean if I simply set pointer to null it frees memory no memory leak no resource leak ...??? so the what's the need for delete we can simply assign null to a pointer. can you refer me to a document, please?
No, you need to delete it and then you set it to NULL. What I meant is, once you set it to NULL, there's no need anymore to check if the pointer is valid: simply check it if it is NULL.
Cédric Moonen Software developer
Charting control [v1.2] -
You need to refer the API documentation, The API that returns HANDLE to system resource has different behaviour on return values, for example CreateFile returns INVALID_HANDLE_VALUE while CreateEvent returns NULL on failure, so checking the handle on CloseHandle depends on the API that created the Handle. Are u looking for _CrtIsValidPointer, _CrtIsValidHeapPointer for pointer validation. AfxIsValidAddress (MFC)
modified on Monday, February 04, 2008 5:52:35 AM
bingo! AfxIsValidAddress() thanks alot RR :) :) :) :) :) :) :) Oh Thanks for the point about handles I have to be more careful,
-
No, you need to delete it and then you set it to NULL. What I meant is, once you set it to NULL, there's no need anymore to check if the pointer is valid: simply check it if it is NULL.
Cédric Moonen Software developer
Charting control [v1.2]Good point but still before using it, it maybe necessary to check its validity. thanks
-
Before running CloseHandle() I terminate those threads. Still I'm sure most of you guys know about the function that checks validity of a pointer to be in valid memory space. I've used it so many times and I now I just forgot that and I can't find it in MSDN crap.
-
Good point but still before using it, it maybe necessary to check its validity. thanks
Absolutely not. Why ? If it is NULL, it is NULL and thus invalid. Of course, you have to initialize it to NULL in your constructor too.
Cédric Moonen Software developer
Charting control [v1.2] -
The problem is this handle has to be accessed from different threads. I checked its value before executing CloseHandle() it has some value which is not what I assigned at the beginning and also it is not INVALID_HANDLE_VALUE but still after execution of CloseHandle an exception occurs.
If you're worrying about having an invalid handle between checking for INVALID_HANDL_VALUE and closing it, then why aren't you worrying about the IsValidHandle routine returning TRUE, then the handle becoming invalid? As Cedric has been trying to tell you until he's blue in the face, you NEED some sort of synchronisation (eg Critical Section) if you can open close these from different threads. Also, if you have multiple threads all able to close a handle, then your program is badly tangled already... Good luck, Iain.
Iain Clarke appearing by Special Request of CPallini.
-
Absolutely not. Why ? If it is NULL, it is NULL and thus invalid. Of course, you have to initialize it to NULL in your constructor too.
Cédric Moonen Software developer
Charting control [v1.2]I think I did not made my point clear, I said before using it. When one defines a pointer surely he wants to use it somewhere it is not null then and it should be checked especially if some naughty functions have played with it before. When one wanted to destroy the pointer then he can assign null to it as you said.
-
If you're worrying about having an invalid handle between checking for INVALID_HANDL_VALUE and closing it, then why aren't you worrying about the IsValidHandle routine returning TRUE, then the handle becoming invalid? As Cedric has been trying to tell you until he's blue in the face, you NEED some sort of synchronisation (eg Critical Section) if you can open close these from different threads. Also, if you have multiple threads all able to close a handle, then your program is badly tangled already... Good luck, Iain.
Iain Clarke appearing by Special Request of CPallini.
I think I said in another post that this handle only is closed by one thread and after closing all other threads. Thanks for the effort Cedric ;) . I noticed a mistake of mine. while critical section is not necessary because never two threads use it synchronously but I noticed that mistakenly I terminate the thread and after that I try to close handle that has been created within that thread .No wonder! silly :-O! thanks all you guys, you are really helpful :)
-
I think I did not made my point clear, I said before using it. When one defines a pointer surely he wants to use it somewhere it is not null then and it should be checked especially if some naughty functions have played with it before. When one wanted to destroy the pointer then he can assign null to it as you said.
Electronic75 wrote:
When one defines a pointer [...] it is not null then
It should be. Always initialize your variables!
Electronic75 wrote:
t should be checked especially if some naughty functions have played with it before
Do not let any naughty functions touch your private parts! Do not program naughty functions in the first place. If naughty functions are made by someone else and keep doing horrible things to the objects you gave them pointers to, hand them a copy of the object, and keep the original with yourself.
Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
Douglas Adams, "Dirk Gently's Holistic Detective Agency"