gpt4 book ai didi

c++ - 删除指针数组会使调用者的应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 03:49:34 24 4
gpt4 key购买 nike

DLL:Visual Studio 2013(VC12)/应用程序:Visual Studio 2010(VC​​10)

我正在创建一个允许客户端访问我们系统的库。然而,当删除从 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

最佳答案

您应该检查客户端应用程序和您的 dll 是否使用相同的设置编译。为了防止此类问题,最好是:

  1. 在客户端分配/释放内存
  2. 在 dll(服务器)端分配/取消分配内存

我会推荐 #1,但我看到您正在使用 #2。所以你现在应该做的是添加 system->FreeDevices(&devices); 这将释放内存 - 但在你的 dll 中。

[编辑]

引用阅读msdn : https://msdn.microsoft.com/en-us/library/ms235460.aspx

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

Also, because each copy of the CRT library has its own heap manager, allocating memory in one CRT library and passing the pointer across a DLL boundary to be freed by a different copy of the CRT library is a potential cause for heap corruption.

关于c++ - 删除指针数组会使调用者的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32479227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com