1

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?

unkulunkulu
  • 11,576
  • 2
  • 31
  • 49
Aneesh Narayanan
  • 3,220
  • 11
  • 31
  • 48
  • 1
    If by deleted you mean `CloseHandle()`, then no, `CloseHandle()` will not be invoked. – hmjd Jun 26 '12 at 08:44

5 Answers5

3

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.

Community
  • 1
  • 1
RedX
  • 14,749
  • 1
  • 53
  • 76
1

The HANDLEs are kind of pointers, so deleting them doesn't do much, to properly free up the resources these HANDLEs are pointing to, you have to explicitly call the corresponding functions (like CloseHandle).

unkulunkulu
  • 11,576
  • 2
  • 31
  • 49
1

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.

Twifty
  • 3,267
  • 1
  • 29
  • 54
0

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.

Adam Sznajder
  • 9,108
  • 4
  • 39
  • 60
0

By doing so what happens to the handle objects

Nothing.

Is they get deleted?

No.

ta.speot.is
  • 26,914
  • 8
  • 68
  • 96