|
|
And what's the difference with what I suggested (meaning setting your pointer to NULL when you deleted it) ?
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
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"
|
|
|
|
|
jhwurmbach wrote: It should be. Always initialize your variables! Of course I initialize pointers but not to NULL to some values.
jhwurmbach wrote: Do not let any naughty functions touch your private parts!
Well, I have no problem with girls
@ DavidCrow,
I understood what cedric said but my point is when it has been given a value somewhere in the program and there is a possibility that value dose not exist anymore so the value is wrong then still I need to check it. Yes when I didn't need the pointer anymore I'll assign null to it and then compare it to null before using it.
jhwurmbach wrote: 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.
Actually the naughty functions were also written by me but about 2-3 years ago and it is part of a scary and big project. Now I do not have enough time(as ever) and I have to add some additional functionalities before having time to inspect all the codes. I will check all the codes whenever I had time but now there is a big crowd who are ready to kill me if I do not give this software to them until the deadline(and the deadline already has been passed ) but happily as I said in another post problem solved it was a silly mistake of mine
|
|
|
|
|
Electronic75 wrote: my point is when it has been given a value somewhere in the program and there is a possibility that value dose not exist anymore so the value is wrong then still I need to check it.
Where does this possibility come from?
THAT is the task you need to solve!
Wherever you delete an object, you need to change the pointer value to NULL.
Thats all, folks
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"
|
|
|
|
|
Electronic75 wrote: ...it maybe necessary to check its validity.
Which is what Cedric has been trying to tell you (by comparing the pointer to NULL ).
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
INVALID_HANDLE_VALUE equals to (-1 ).
General approach goes as below.
HANDLE h = NULL;
h = CreateFile(...);
if(INVALID_HANDLE_VALUE != h) {
}
if(!h) {
h = CreateFile(...);
if(INVALID_HANDLE_VALUE == h) {
}
}
if(h) {
CloseHandle(h);
h = NULL;
}
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.
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
AfxIsValidAddress [ ^]?
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]
|
|
|
|
|
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.
AfxIsValidAddress() is a wrapper around IsBadReadPtr() and IsBadWritePtr() .
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
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 !
thanks all you guys, you are really helpful
|
|
|
|
|
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,
|
|
|
|
|
Hi everybody,
i like to display a Loading-Dialog right into the view, not as Modal.
So i call "Create" from the Dialog-Class and also OnInit()
The dialog will be drawn on the view, but the other controls on the view are redrawn over
this dialog.
Which handler or function does i need to override for that the dialog will be drawn at least,
for the reason that the dialog is painted OVER all other controls.
Big thanks for your help
|
|
|
|
|
play around the Z-order of the dialog window (SetWindowPos)
|
|
|
|
|
I'll try it, thanks
|
|
|
|