删除指针数组会导致调用者的应用程序崩溃

时间:2015-09-09 12:11:10

标签: c++ memory-management visual-studio-2013 dllimport assertion

DLL:Visual Studio 2013(VC12)/ App:Visual Studio 2010(VC10)

我正在创建一个允许客户端访问我们系统的库。但是,当删除从dll返回的指针数组时,客户端应用程序崩溃了。删除正常指针(非数组)时,一切似乎都正常。想法?我注意到当使用VC12作为客户端应用程序时,数组被删除了,但是当使用VC10时我得到了崩溃。

所以我的问题:为什么在从dll返回时删除指针数组时客户端应用程序崩溃,如果删除dll分配的内存时出现问题则不会删除系统;也是一个问题?

struct IDevice {
    virtual ~IDevice() {}
    char name[64];
};

class Device : public IDevice {
public:
    Device() {}
    ~Device() {}
};

struct ISystem {
    virtual ~IDevice() {}
    virtual Result::Value GetDevices(Collection<IDevice**>* deviceCollection) = 0;
};

class System : public ISystem {
public:
    Result::Value GetDevices(Collection<IDevice**>* deviceCollection) {
        Result::Value result = Result::Success;
        deviceCollection->collectionSize = 1;
        deviceCollection->collection = new IDevice*[1];

        IDevice* device = new Device();
        Utilities::strcpy_safe(device->name, "blackey");
        deviceCollection->collection[0] = device;
        return result;
    }
};

template<typename T>
struct Collection {
    T collection;
    int collectionSize;
};

///////// Main App ///////////

LoginInfo loginInfo;
string ip("192.168.1.2");
string u("admin");
string password("admin");
loginInfo.port = 443;
loginInfo.ssl = true;
strncpy_s(loginInfo.ipAddress, ip.c_str(), sizeof(loginInfo.ipAddress)-1);
strncpy_s(loginInfo.uname, u.c_str(), sizeof(loginInfo.uname)-1);
strncpy_s(loginInfo.password, password.c_str(), sizeof(loginInfo.password)-1);

ISystem* system = nullptr;
// SystemLogin calls into my dll which allocates memory for system OK
Result::Value result = SystemLogin(&loginInfo, &system);

Collection<IDevice**> devices;
// If the line below is uncommented, and the dll is modified to not allocate memory, but instead use this memory
// then below the delete[] devices.collection; works.
//devices.collection = new IDevice*[50];

// Makes a call into the library which populates devices
result = system->GetDevices(&devices);

for (int i = 0; i < devices.collectionSize; i++) {
    // Each item in the array is deleted OK
    delete devices.collection[i];
}

// Deleting this array of pointers crashes (see screenshots) when it's memory was allocated in the dll.
delete[] devices.collection;

// system deletes OK
delete system;

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:2)

您应该检查客户端应用程序和您的dll是否使用相同的设置进行编译。为了防止这样的问题,它更好:

  1. 在客户端分配/取消分配内存
  2. 在dll(服务器)端分配/取消分配内存
  3. 我会推荐#1,但我看到你正在使用#2。所以你现在应该做的是添加system->FreeDevices(&devices);,这将释放内存 - 但在你的dll内。

    [编辑]

    参考阅读msdn:https://msdn.microsoft.com/en-us/library/ms235460.aspx

    重要的部分是关于可以构建DLL和客户端应用程序的不同CRT。这可能会导致您遇到的确切问题:

      

    此外,因为CRT库的每个副本都有自己的堆管理器,所以在一个CRT库中分配内存并将指针传递到DLL边界以便由CRT库的不同副本释放是堆损坏的潜在原因

答案 1 :(得分:0)

请注意,一般来说,最好从DLL创建和删除从1 DLL创建的东西(只是一个更好的设计,如果你想避免调用自定义删除器,那么你必须将自己的分配器传递给DLL打电话给#34; new&#34;儿子你可以打电话&#34;删除&#34;返回的对象。)。

但是,您发布的代码无法重现问题,因为我们不知道如何在DLL中创建对象,因此我们无法提供帮助。

然而,因为意图似乎使用指向指针(非常糟糕,std::vector< std::unique_ptr< IDevice> >怎么样?具有相同的性能并允许编写更少的代码。)

我假设您在GetDevices电话

中执行了以下操作
typdef IDevice * IDevicePtr;
//using IDevicePtr = IDevice *;
void GetDevices( Collection<IDevicePtr*> * devices)
{
    devices->collectionSize = 10;
    devices->collection = new IDevicePtr[10];
    for(int i =0; i<10;i++)
        devices->collection[i] = new CDevice();
}

在这种情况下,您用于删除的代码是正常的,错误是其他的。

请注意

  • IDevice必须实现虚拟析构函数
  • ISystem必须实现虚拟析构函数
相关问题