我正在开发以下课程:
class Handle
{
public:
inline Handle()
{
handle = INVALID_HANDLE_VALUE;
}
inline Handle(HANDLE handle)
{
this->handle = copyHandle(handle);
}
inline Handle(const Handle& rhs)
{
this->handle = copyHandle(rhs.handle);
}
inline bool isValid()
{
return handle != INVALID_HANDLE_VALUE;
}
inline HANDLE getNativeHandle()
{
return copyHandle(this->handle);
}
inline void close()
{
if(handle != INVALID_HANDLE_VALUE)
{
CloseHandle(handle);
handle = INVALID_HANDLE_VALUE;
}
}
inline virtual ~Handle()
{
if(handle != INVALID_HANDLE_VALUE)
CloseHandle(handle);
}
protected:
HANDLE handle;
HANDLE copyHandle(HANDLE copyable);
};
.cpp文件:
HANDLE Handle::copyHandle(HANDLE copyable)
{
HANDLE ret;
HANDLE current = GetCurrentProcess();
if(copyable == INVALID_HANDLE_VALUE)
ret = copyable;
else if(DuplicateHandle(current, copyable, current, &ret, 0, TRUE , DUPLICATE_SAME_ACCESS) == 0)
{
if(GetLastError() == ERROR_ACCESS_DENIED)
throw SecurityException("The handle duplication was denied!");
else
throw InvalidHandleException("The handle could not be duplicated!");
}
return ret;
}
该类似乎正常工作,但复制句柄,然后关闭原始句柄然后复制新句柄将抛出异常或Windows错误代码6,即“无效句柄值”。
此刻,我认为关闭原始句柄也会导致副本完全被破坏,导致我之后无法使用它们。
Handle test = CreateMutex(NULL, FALSE, NULL);
Handle copy = test;
test.close();
std::cout << copy.getNativeHandle() << std::endl; // throws an exception, but uses the same function as above
return 0;
是否有可能复制句柄,因此它不依赖于原始句柄的存在?
答案 0 :(得分:2)
尝试此实施:
class Handle
{
public:
Handle(HANDLE ahandle = INVALID_HANDLE_VALUE)
{
handle = ahandle; // <- take ownership of the original, not a copy
}
Handle(const Handle& src)
{
handle = src.duplicate(); // <-- take ownership of a copy
}
~Handle()
{
close();
}
void close()
{
if (handle != INVALID_HANDLE_VALUE)
{
CloseHandle(handle);
handle = INVALID_HANDLE_VALUE;
}
}
HANDLE getNativeHandle() const
{
return handle;
}
bool isValid() const
{
return (handle != INVALID_HANDLE_VALUE);
}
HANDLE duplicate()
{
if (handle == INVALID_HANDLE_VALUE)
return handle;
HANDLE ret, current = GetCurrentProcess();
if (!DuplicateHandle(current, handle, current, &ret, 0, TRUE, DUPLICATE_SAME_ACCESS))
{
if (GetLastError() == ERROR_ACCESS_DENIED)
throw SecurityException("The handle duplication was denied!");
else
throw InvalidHandleException("The handle could not be duplicated!");
}
return ret;
}
Handle& operator=(HANDLE &rhs)
{
close();
handle = rhs; // <-- take ownership of the original, not a copy
return *this;
}
Handle& operator=(const Handle &rhs)
{
close();
handle = rhs.duplicate(); // <-- take ownership of a copy
return *this;
}
protected:
HANDLE handle;
};
另外,一些API函数使用NULL
而不是INVALID_HANDLE_VALUE
,有些则不使用CloseHandle()
。您应该考虑对这些差异进行核算。我建议更新Handle
类以使用C ++模板,这样您就可以基于每个实例专门化行为,例如:
struct InvalidHandleTrait
{
static const HANDLE InvalidValue = INVALID_HANDLE_VALUE;
};
struct NullHandleTrait
{
static const HANDLE InvalidValue = NULL;
};
struct CloseHandleTrait
{
static bool close(HANDLE handle)
{
return CloseHandle(handle);
}
};
template< typename HandleTrait = InvalidHandleTrait, typename CloseTrait = CloseHandleTrait >
class Handle
{
public:
Handle(HANDLE ahandle = HandleTrait::InvalidValue)
{
handle = ahandle; // <- take ownership of the original, not a copy
}
Handle(const Handle& src)
{
handle = src.duplicate(); // <-- take ownership of a copy
}
~Handle()
{
close();
}
void close()
{
if (handle != HandleTrait::InvalidValue)
{
CloseTrait::close(handle);
handle = HandleTrait::InvalidValue;
}
}
HANDLE getNativeHandle() const
{
return handle;
}
bool isValid() const
{
return (handle != HandleTrait::InvalidValue);
}
HANDLE duplicate()
{
if (handle == HandleTrait::InvalidValue)
return handle;
HANDLE ret, current = GetCurrentProcess();
if (!DuplicateHandle(current, handle, current, &ret, 0, TRUE, DUPLICATE_SAME_ACCESS))
{
if (GetLastError() == ERROR_ACCESS_DENIED)
throw SecurityException("The handle duplication was denied!");
else
throw InvalidHandleException("The handle could not be duplicated!");
}
return ret;
}
Handle& operator=(HANDLE &rhs)
{
close();
handle = rhs; // <-- take ownership of the original, not a copy
return *this;
}
Handle& operator=(const Handle &rhs)
{
close();
handle = rhs.duplicate(); // <-- take ownership of a copy
return *this;
}
protected:
HANDLE handle;
};
答案 1 :(得分:0)
您还需要为Handle
定义赋值运算符。我怀疑实际崩溃的代码看起来像这样:
Handle test = CreateMutex(NULL, FALSE, NULL);
Handle copy;
copy= test; // assigned instead of using copy constructor
test.close();
std::cout << copy.getNativeHandle() << std::endl;
return 0;
如果没有赋值运算符,则不能正确复制句柄。