Hello, I am wondering if someone could spot a memory leak in my code. I suspect 
there is a problem in the FreeData function, but the data seems ok under the 
watch. What happens is that when I run my test program, I have it save the data 
to a file, re-open it, and then free the data (myobject.free). When the data 
gets freed, I get the CPU window open and it stops on a assembly statement 
called "ret" If I run it through, it repeats this 2 more times then causes a 
access violation. However, if I run the program without the debugger, it goes 
through just fine. On the Destructor I call FreeData before the inherited 
statement. If you want, i can show you the whole unit. I dont expect you to 
understand all of it, such as tsxMalloc, etc which allocates data, and other 
functions. However maybe there is something within my design that is flawed? 
Also understand that I havnt slept in a while LOL I'll explain a bit about the 
memory functions. tsxMalloc allocates memory and fills the data with bogus 
data. tsxCalloc does the same thing but fills the data with nils and 0's. 
tsxFree frees the data allocated by those 2 functions.

procedure TSPEObjs.FRead(fname: string);
var
  f: File;
  i: integer;
begin
  if (fname = '') then exit;
  AssignFile(f, fname);
  try
    Reset(f);
    FreeData;
    BlockRead(f, nbrPEObjs, sizeof(nbrPEObjs));
    if (nbrPEObjs > 0) then begin
      peObjs:= tsxMalloc(nbrPEObjs * sizeof(pointer));
      selDataArray:= tsxMalloc(nbrPEObjs * sizeof(TSelData));
      for i:= 0 to nbrPEObjs - 1 do begin
        BlockRead(f, peObjs[i], sizeof(peObjs[i]));
        BlockRead(f, selDataArray[i], sizeof(selDataArray[i]));
      end;
    end;
  finally
    CloseFile(f);
  end;
end;

procedure TSPEObjs.FWrite(fname: string);
var
  f: File;
  i: integer;
begin
  if (fname = '') then
    exit;
  AssignFile(f, fname);
  try
    Rewrite(f);
    BlockWrite(f, nbrPEObjs, sizeof(nbrPEObjs));
    for i:= 0 to nbrPEObjs - 1 do begin
      BlockWrite(f, peObjs[i], sizeof(peObjs[i]));
      BlockWrite(f, selDataArray[i], sizeof(selDataArray[i]));
    end;
  finally
    CloseFile(f);
  end;
end;

procedure TSPEObjs.FreeSelData(idx: integer);
begin
  try
    with selDataArray[idx] do begin
      if Assigned(vxsIdxArray) then begin
        tsxFree(vxsIdxArray);
        vxsIdxArray:= nil;
      end;
      if Assigned(edgeArray) then begin
        tsxFree(edgeArray);
        edgeArray:= nil;
      end;
      if Assigned(faceIdxArray) then begin
        tsxFree(faceIdxArray);
        faceIdxArray:= nil;
      end;
    end;
  except
    on E: Exception do
      MessageDlg(E.Message, mtError, [mbOK], 0);
  end;
end;

procedure TSPEObjs.FreeData;
var
  i: integer;
begin
  if (selDataArray <> nil) then begin
    for i:= 0 to nbrPEObjs - 1 do
      FreeSelData(i);
    tsxFree(selDataArray);
    selDataArray:= nil;
  end;
  if (peObjs <> nil) then begin
    tsxFree(peObjs);
    peObjs:= nil;
  end;
  nbrPEObjs:= 0;
end;
__________________________________________________
Delphi-Talk mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi-talk

Reply via email to