释放COM指针异常

时间:2018-09-18 09:49:14

标签: c++ com

我在read access violation exception类析构函数中得到Base,因为root_dseCoUninitialize();之后超出范围

#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之前的releaseCoUninitialize();以避免read access violation exception
注意:所有派生类都使用root_dse,因此它应该在Base类中。

0 个答案:

没有答案
相关问题