I have a std::map<std::string, HANDLE> SampleMap
which stores HANDLE
objects as value. After the use of the map I am clearing all the map entries.
SampleMap.clear();
By doing so what happens to the handle objects. Is they get deleted?
I have a std::map<std::string, HANDLE> SampleMap
which stores HANDLE
objects as value. After the use of the map I am clearing all the map entries.
SampleMap.clear();
By doing so what happens to the handle objects. Is they get deleted?
HANDLE are just typedefs or defines to pointers (AFAIK void*
).
When clearing the map they will not get deleted you have to close/release them yourself.
Or write a wrapper class that will do that for you. See this thread How to use C++ standard smart pointers with Windows HANDLEs? for a few starting ideas.
The HANDLE
s are kind of pointers, so deleting them doesn't do much, to properly free up the resources these HANDLE
s are pointing to, you have to explicitly call the corresponding functions (like CloseHandle
).
They don't get deleted, they become dangling pointers/handles.
If you want the to be auto deleted use std::unique_ptr and override the deleter object to delete whatever type of handle is contained.
I am quite sure that it's the same situation as not closing the handle and just removing the local object reference. I am afraid that you have to use CloseHandle
function each time. As you probably know OS keeps track of number of references to an object and deletes it only when it's equal to zero. The only way to inform OS that you don't need any more the object is to use CloseHandle
function. Otherwise the handle stay in some handle table and is not deleted.
By doing so what happens to the handle objects
Nothing.
Is they get deleted?
No.