这种代码更改是否会导致严重的内存泄漏?

时间:2012-10-26 06:24:53

标签: c++ visual-c++ memory-leaks

因为我们需要从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次迭代后冻结了Out of Memory Exception。这可能是由签名的变化引起的吗?对于我们来说,似乎不太可能,因为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;
}

1 个答案:

答案 0 :(得分:2)

CalcDDtable的函数签名不会泄漏内存。函数CalcDDtable中的代码具有一些局部变量,这些变量在调用函数时在堆栈上分配,并在函数返回时从堆栈中弹出。所以你的内存泄漏不在这个功能中。