帮助dll中的内存释放 [英] help with memory deallocation in dll

查看:118
本文介绍了帮助dll中的内存释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我想访问visual studio 2015使用visual studio 2008在qt中开发的应用程序创建的dll函数。当

I would like access to dll function created by visual studio 2015 from a application developed in qt with visual studio 2008. When

我尝试从应用程序vs2008发布vect(在vs2015的dll中分配一个float数组)我在调试模式下崩溃。

I try to release vect (a float array allocate in dll of vs2015) from application vs2008 I get crash in debug mode.

// dll in vs2015

// dll in vs2015

extern" C" __declspec(dllexport)void function(float ** vect)

{

extern "C" __declspec(dllexport) void function(float ** vect)
{

        ...

        ...

        int numberVertex = 1000;

* vect = new float [numberVertex];

//填充顶点

        int numberVertex=1000;
*vect=new float[numberVertex];
// fill vertex

  &NBSP; &NBSP; &NBSP; for(int i = 0; i< numberVertex; i ++)(* vect)[i] = 3;

        for (int i=0;i<numberVertex;i++) (*vect)[i]=3;

  &NBSP; &NBSP;  

       

}

// vs2008中的应用程序使用QLibrary并调用dll函数

// application in vs2008 use QLibrary and call the dll function

float * vect = NULL

float * vect=NULL

function(& vect);

function(&vect);

delete [] vect; //程序崩溃在调试中,我不会在发布时再试一次。

delete[] vect; // program crash in debug, I don't try again in release.

问题是dll端已知数组元素的数量,因为算法确定了关于输出数组vect。

The problem is that the number of element of array is known on the dll side because the algorithms determinate the dimension about output array vect.

你有没有想过解决这个问题?

Have you idea to solve this problem?

推荐答案

如果DLL分配内存,则DLL必须负责删除相同的内存块。这是因为内存分配/释放依赖于 不仅是运行时库(在这种情况下,您混合了2008和
2015 C运行时支持),还有编译类型。例如,在DEBUG模式下,分配与RELEASE模式不同。您可以按照我的第一个声明中的规则来解决这个问题。
If the DLL allocates the memory, then the DLL must be made responsible for deleting the same block of memory. That's because memory allocation/deallocation is dependent on the  not only the runtime library (in which case you are mixing both 2008 and 2015 C runtime support) but also the type of compilation. For example, in DEBUG mode, the allocation is done differently from RELEASE mode. You can solve this by following the rule in my first statement.


这篇关于帮助dll中的内存释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆