【发布时间】:2012-10-26 06:24:53
【问题描述】:
因为我们需要从 VB6 中调用下面的代码,所以我们不得不更改方法的签名
int STDCALL CalcDDtable(struct ddTableDeal tableDeal, struct ddTableResults * tablep
到
int STDCALL CalcDDtable(struct ddTableDeal * tableDeal, struct ddTableResults * tablep
ddTableDeal 结构体只包含一个 16 字节的数组,而 ddTableResults 结构体只包含一个 20 字节的数组,并填充了 dll 的计算结果。
代码是从 VB6 调用的:
Declare Function CalcDDtable Lib "dds222vb6.dll" (ByRef lngDeal As Long, ByRef lngResult As Long) As Long
Dim Cards(15) As Long
Dim Results(19) As Long
'Some code to populate the Cards array. The Results arrays contains zeroes.
lngErrorCode = CalcDDtable(Cards(0), Results(0))
但是,测试计算机在 150,000 次迭代后因内存不足异常而死机。这可能是由签名更改引起的吗?对我们来说,这似乎不太可能,因为 150,000 乘以 36 字节刚好超过 5MB 的内存。
完整(调整后的)代码。唯一的变化在于签名,ddTableDeal.cards 已在 ddTableDeal->cards 中更改。
int STDCALL CalcDDtable(struct ddTableDeal * tableDeal, struct ddTableResults * tablep) {
int h, s, k, ind, tr, first, res;
struct deal dl;
struct boards bo;
struct solvedBoards solved;
for (h=0; h<=3; h++)
for (s=0; s<=3; s++)
dl.remainCards[h][s]=tableDeal->cards[h][s];
for (k=0; k<=2; k++) {
dl.currentTrickRank[k]=0;
dl.currentTrickSuit[k]=0;
}
ind=0; bo.noOfBoards=20;
for (tr=4; tr>=0; tr--)
for (first=0; first<=3; first++) {
dl.first=first;
dl.trump=tr;
bo.deals[ind]=dl;
bo.target[ind]=-1;
bo.solutions[ind]=1;
bo.mode[ind]=1;
ind++;
}
res=SolveAllBoards4(&bo, &solved);
if (res==1) {
for (ind=0; ind<20; ind++) {
tablep->resTable[bo.deals[ind].trump][rho[bo.deals[ind].first]]=
13-solved.solvedBoard[ind].score[0];
}
return 1;
}
return res;
}
【问题讨论】:
-
显示调用这个函数的代码会很有帮助。
-
这被标记为 C++ 但在我看来像 C
-
或者生成一个仅你不理解的行为的最小示例,没有任何额外的复杂性。
-
SolveAllBoards4 发生了什么?
-
您发布的代码根本没有分配或释放任何内存。所以本身不可能是内存泄漏。但是您所做的重新设计可能会导致内存泄漏,这完全取决于您如何使用
CalcDDtable。
标签: c++ visual-c++ memory-leaks