【问题标题】:Issues with multiple DLL多个 DLL 的问题
【发布时间】:2012-12-04 05:34:58
【问题描述】:

我有一个引用 2 个 COM DLL 的应用程序。应用在启动时调用的 2 个 DLL 中有一个 Init 函数。

这两个 DLL 几乎相似 - 除了它们具有不同的 uid 和不同的接口名称。除此之外,逻辑是一样的......而且使用的变量也是一样的。

这就是这个系统的运作方式 - 1. StartApp() 2. Init DLL1(appVar1,appVar2)..这里应用程序将在这个 DLL 中定义的 var x,y 设置为从应用程序传递的值。假设 x = appVar1 和 y = appVar2。 x 和 y 是全局变量。 3. 初始化 DLL2(appVar1a,appVar2a)...应用程序将 DLL2 中定义的 var x 设置为从应用程序传递的值。 x = appVar1a; y = appVar2。

现在,当我尝试在 DLL1 上运行某些东西时,x 和 y 的值神秘地变成了其他东西。

x 现在变成 appVar2 并且 y 是空白的。

这里x、y以及app在InitDLL函数中传递的appVar1等所有变量都是BSTR的。

我通过代码...x,y 在 InitDLL1 中正确设置为 appVar1,appVar2。但是一旦这个函数返回并且我们正在初始化第二个 DLL (InitDLL2),情况就会改变。

有趣的是,我在 DLL2 中看不到任何此类问题..即使代码/逻辑非常相似....除了它调用的接口。

在 DLL1 和 DLL2 中,在 InitDLL 函数中,我们创建了一个新线程,我们在各种函数中使用 x 和 y。由于上述问题,DLL1 总是失败..DLL2 没有问题。

任何可能出错的线索?

【问题讨论】:

  • 请显示代码(调用者和被调用者都很有趣)。您不能用 = 分配 BSTR,您需要复制它们。

标签: c++ oop dll com


【解决方案1】:

您不能只分配 BSTR,您需要复制它。问题来了:

// caller - by COM memory management rules, for [in] parameters
// caller needs to allocate and free the memory
BSTR str = SysAllocString("..."); // string is allocated
comObjectPtr->Init(str);          // in the function, pointer is copied to global
SysFreeString(str);               // string released, pointer points to garbage

// callee
ComObject::Init(BSTR str)
{
    // wrong code!!! you have no idea about lifetime of str,
    // so you can't just assign it. When the caller function
    // exits, string memory is released and your global is thrashed
    // I suspect you use some BSTR wrapper (bstr_t or CComBSTR) so
    // you don't see the deallocation part and that's why the values
    // are invalidated on function exit: the destructor of BSTR wrapper
    // does its job
    global_str = str;             

    // correct code:
    // global_str = SysAllocString(str);
}

【讨论】:

    猜你喜欢
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    相关资源
    最近更新 更多