c++ - 释放 COM 指针的异常
问题描述
我正在进入read access violation exception
类Base
析构函数,因为root_dse
之后超出范围CoUninitialize();
#include <atlbase.h>
#include <ActiveDS.h>
#include <memory>
#pragma comment(lib, "activeds.lib")
#pragma comment(lib, "adsiid.lib")
using namespace std;
class Base {
protected:
CComPtr<IADs> root_dse = nullptr;
public:
Base()
{
CoInitialize(nullptr);
}
virtual ~Base()
{
CoUninitialize();
}
virtual void do_stuff() = 0;
};
class Derived : public Base {
private:
void do_stuff() override {
const auto h_result = ADsOpenObject(L"LDAP://rootDSE",
NULL,
NULL,
ADS_SECURE_AUTHENTICATION,
IID_IADs,
(void**)&root_dse);
}
};
int main() {
std::shared_ptr<Base> base = std::make_shared<Derived>();
base->do_stuff();
return 0;
}
我怎样才能确保这root_dse
是release
之前CoUninitialize();
避免的read access violation exception
?
注意:所有派生类都在使用root_dse
,所以它应该在Base
类中。
解决方案
推荐阅读
- c# - 如何将字符串列表传递给 dapper 以从中选择值
- git - 从服务器自动提交到 bitbucket
- python - Python if 语句让我感到困惑
- angular - 无法在 jasmine 测试用例中覆盖 ngOnInit()
- asp.net - 如何分别使用 AnonymousType 的数据?
- c# - 使用 angular js 和 c# 使用 web api
- ios - Instagram API 登录错误 - 无法加载此页面
- powershell - 在 PowerShell 中为命名空间管理器设置默认命名空间
- javascript - 托管 .ejs 文件和节点 js
- java - Android webview,检测网站是否请求位置权限