
Daniel Trebbien wrote:
What kind of HANDLE that can be returned from DuplicateHandle should not be disposed by CloseHandle? The only time I should call `CloseHandle` on the `HANDLE` result from `DuplicateHandle` is if the `DuplicateHandle` call succeeded. This is because the MSDN documentation does not assert that the `HANDLE` resulting from `DuplicateHandle` will never be `NULL` or `INVALID_HANDLE_VALUE`.
What do you think about the following functions? rcpp::win::file_handle clone(rcpp::win::file_handle const & f) { HANDLE h; BOOL r = ::DuplicateHandle(GetCurrentProcess(), f.get(), GetCurrentProcess(), &h, 0, FALSE, DUPLICATE_SAME_ACCESS); if (r == FALSE) return rcpp::win::file_handle(); // or throw an exception return rcpp::win::file_handle(h); } rcpp::win::kernel_handle clone(rcpp::win::kernel_handle const & f) { HANDLE h; BOOL r = ::DuplicateHandle(GetCurrentProcess(), f.get(), GetCurrentProcess(), &h, 0, FALSE, DUPLICATE_SAME_ACCESS); if (r == FALSE) return rcpp::win::kernel_handle(); // or throw an exception return rcpp::win::kernel_handle(h); }
Also, I think that it would be nice to have a `shared_resource` template. I have an application in mind where I would like to create a shared `HANDLE` that is only closed if it is invalid and all references to it have been destroyed (a shared reference count reaches 0). I thought about that. I don't think it has wide application. Moreover it can be emulated with boost::shared_ptr<rcpp::resource>. Is it worth while?
I think so because `boost::shared_ptr` adds a level of indirection that is unnecessary.
I think that I can implement shared_resource if I is asked for that.