内存泄漏superobject

这几天在移植代码,将之前的项目移植到插件框架里面,因为之前是单EXE的。对内存泄漏问题不是很在意。在DLL下面就不一样了,所以加入FastMM4来做监控调试。发现之前的ScripParser解析SQL时存在内存泄漏。

 

其中有段代码

 

var
  lvItem            : TSuperObjectIter;
begin
  if ObjectFindFirst(vParam, lvItem) then
  try
    repeat
      FScript := FastReplace(FScript, lvItem.key, FParamSetValue.S[LowerCase(lvItem.key)]);
    until not ObjectFindNext(lvItem);
  finally
    ObjectFindClose(lvItem);
  end;
 
存在内存泄漏,后来查看ObjectFindFirst的时候才发现了问题

function ObjectFindFirst(const obj: ISuperObject; var F: TSuperObjectIter): boolean;
var
  i: TSuperAvlEntry;
begin
  if ObjectIsType(obj, stObject) then
  begin
    F.Ite := TSuperAvlIterator.Create(obj.AsObject);
    F.Ite.First;
    i := F.Ite.GetIter;
    if i <> nil then
    begin
      f.key := i.Name;
      f.val := i.Value;
      Result := true;
    end else
      Result := False;   

     //原来返回False也有可能创建了

    //F.Ite := TSuperAvlIterator.Create(obj.AsObject);
  end else
    Result := False;
end;

 

///最后修改代码,没有了内存泄漏

var
  lvItem            : TSuperObjectIter;
begin
  try 
    if ObjectFindFirst(vParam, lvItem) then
    repeat
      ……
    until not ObjectFindNext(lvItem);
  finally
    if lvItem.Ite <> nil then ObjectFindClose(lvItem);
  end;
posted @ 2011-04-24 15:35  D10.天地弦  阅读(656)  评论(0编辑  收藏  举报