2012-10-26 49 views
1

因为我们需要从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)) 

然而,测试计算机后150000次迭代与内存溢出异常冻结。这可能是由签名的变化引起的吗?对我们来说,看起来不太可能,因为36万字节的15万次相当于超过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; 
} 
+4

显示正在调用此函数的代码会很有帮助。 – Amber

+0

这被标记为C++,但看起来像C对我 – CashCow

+0

或产生一个最小的例子,包含* only *你不明白的行为,没有任何额外的复杂性。 – Mankarse

回答

2

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

相关问题