在excel中管理内存xll udf /内存泄漏 [英] managing memory in excel xll udf / memory leaks

查看:94
本文介绍了在excel中管理内存xll udf /内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在Visual C ++中编写xll时是否理解管理内存的人可以给我一些教育。我对此很了解。


为了我自己的理解,我一直在尝试做两件事。首先,我编写一个调用C API的导出函数,并通过引用返回一个xloper12。 


其次,我编写了一个执行相同操作的函数,但是返回一个xloper12 (我认为)我已经在DLL中动态分配了。在这个函数的最后,我告诉excel使用xlbitDLLFree位回调DLL。


///// Code 1 ///////



xloper12 * __stdcall matrixMult(xloper12 * arg1,xloper12 * arg2)


{


xloper12 p_ret;


 


Excel12(xlfMmult,& p_ret,2,arg1,arg2);


 


p_ret.xltype | = xlbitXLFree;


 


return& p_ret;


 


}



 


 


//// ////////////////////////////////////////////////// ////////////////////////


 


// //////代码2 /////////////////////



xloper12 * __stdcall matrixMult2(xloper12 * arg1,xloper12 * arg2)


{


< /跨度> XL oper12 * p_ret =(xloper12 *)malloc(sizeof(xloper12));


 


Excel12(xlfMmult,p_ret,2,arg1,arg2);


 


int rows = arg1-> val.array.rows;


int cols = arg1-> val.array.columns;


int array_size = rows * cols;


 


xloper12 * p =(xloper12 *)malloc(sizeof(xloper12) );


p-> val.array.lparray =(xloper12 *)malloc(array_size * sizeof(xloper12) ));


p-> val.array.rows = rows;


p-> val.array.columns = cols;


 


for(int i = 0; I< ARRAY_SIZE; ++ i)


{


p-> val.array.lparray [i] .xltype = xltypeNum;


p->val.array.lparray[i].val.num = p_ret-> val.array.lparray [i] .val.num;



}



// p- > val.array.lparray = p_ret;


 


p-> xltype = xltypeMulti | xlbitDLLFree;


 


返回p;


 


}



///////////////////////////////


 


如果我然后在vba中执行一个简单的循环,只看一下运行第一个函数的任务管理器,excel中使用的内存几乎不会移动(正如我所料,并且类似于ha当我在Excel中使用Excel的本机MMult函数
循环执行10,000次运行时,ppens。


但是,如果我对第二个函数执行相同的操作,它看起来像是excel使用的内存随着循环次数的增加而增加,并且它永远不会释放这个内存..最终我可以获得excel(如果有足够的循环)来显示一个不够的系统
resources错误消息。


我确定我在代码2中有内存泄漏/错误,但我不明白它可能是什么。 


非常感谢任何帮助/建议。


非常感谢


 

解决方案

随着使用更多行和列,Excel工作簿大小会增加。 只需从单元格中删除数据不会减少工作簿的大小。  Excel会跟踪最大使用区域并减少内存大小,必须减少使用区域
删除行和列。


 


您的第一个宏不会将任何数据写入工作簿,因此大小不会增加。 第二个宏正在写入新行,这将增加内存大小和工作簿大小。


如果通过右键单击鼠标使用Windows资源管理器创建新工作簿,您将获得一个真实的小尺寸工作簿。 如果您随后打开工作簿并保存工作簿而不输入任何数据,则工作簿大小将会增长。


我注意到excel 2007工作簿小于等效的2003工作簿。 似乎微软在2007年对文件大小进行了一些改进,但是当我从一个单元格中删除数据
时,我认为在减少文件大小方面没有任何改进。


&NBSP;


Hi, I wonder if someone who understands managing memory when writing xlls in Visual C++ can give me some education. I'm a pretty new to to this.

For my own understanding i have been trying to do two things. First, i write an exported function which calls the C API, and returns an xloper12 by reference. 

Second, I write a function which does the same thing, but returns an xloper12 which (I think) I have dynamically allocated inside the DLL. At the end of this function, I tell excel to call back into the DLL with the use of the xlbitDLLFree bit.

/////Code 1///////

xloper12 * __stdcall matrixMult(xloper12 *arg1, xloper12 *arg2)

{

xloper12 p_ret;

 

Excel12(xlfMmult, &p_ret, 2, arg1, arg2);

 

p_ret.xltype |= xlbitXLFree;

 

return &p_ret;

 

}

 

 

//////////////////////////////////////////////////////////////////////////////

 

////////Code 2/////////////////////

xloper12 * __stdcall matrixMult2(xloper12 *arg1, xloper12 *arg2)

{

xloper12* p_ret = (xloper12 *)malloc(sizeof(xloper12));

 

Excel12(xlfMmult, p_ret, 2, arg1, arg2);

 

int rows = arg1->val.array.rows;

int cols = arg1->val.array.columns;

int array_size = rows*cols;

 

xloper12* p = (xloper12 *)malloc(sizeof(xloper12));

p->val.array.lparray = (xloper12 *)malloc(array_size*sizeof(xloper12));

p->val.array.rows = rows;

p->val.array.columns = cols;

 

for(int i = 0; i<array_size; ++i)

{

p->val.array.lparray[i].xltype = xltypeNum;

p->val.array.lparray[i].val.num = p_ret->val.array.lparray[i].val.num;

}

//p->val.array.lparray = p_ret;

 

p->xltype = xltypeMulti|xlbitDLLFree;

 

return p;

 

}

///////////////////////////////

 

if I then do a simple loop in vba, and just look at task manager running the first function, the memory being used in excel barely moves (as I would expect, and similar to what happens when I loop through for eg, 10,000 runs using Excel's native MMult function in vba.

However, if I do the same for the second function, it looks like that the memory being used by excel increases as the number of loops increase, and it never releases this memory back.. eventually I can get excel (if enough loops) to show a not enough system resources error message.

Im certain that I have a memory leak/mistake in Code 2, but I dont understand what it might be. 

Really appreciate any help/advice.

thanks v much

 

解决方案

Excel workbooks size grows as more rows and columns are used.  Simply removing the data from cells doesn't reduce the workbook size.  Excel keeps track of the maximum used area and to reduce the memory size you must reduce the used area by deleting rows and columns.

 

You first macro isn't wrint any data to the workbook so the size will not increase.  the 2nd macro is writing to new rows which will increase the memory size and the workbook size.

If you create a new workbook using the windows explorer by right clicking the mouse you will get a real small size workbook.  If you then open the workbook and save the workbook without entering any data the workbook size will grow.

I have noticed that excel 2007 workbooks are smaller than the equivalent 2003 workbooks.  It appears that microsoft made some improvements in file size in 2007, but I don't think any imporovements were made in rreducing files sizes when data is deleted from a cell.

 


这篇关于在excel中管理内存xll udf /内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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