Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problem freeing memory

Posted on 2002-05-01
3
Medium Priority
?
201 Views
Last Modified: 2010-04-04
Hello.
I have problems freeing resources on my application. I don't know if I'm
doing the things well, so please tell me where could be my mistake.
I have some pointers (structures) that I store on a Tlist. Some of that
structures have lists that store more pointers.
So I have something like this:

  TAAmostra=^TAmostra;
  TAmostra = record
    NumAmostra:integer;
    N:integer;
    hora:TDateTime;
    ListaPesagens:TList;
    ...
    prod:TPProduto;
    ...
  end;

  TPPesagem=^TPesagem;
  TPesagem = record
    N:integer;
    MBRUTA:single;
    Tara:single;
    MLiquida:single;
    VLiquido:single;
  end;

In this situation, the structure TAAmostra has a list (ListaPesagens) that
is going to store the TPPesagem structure.
So then I have this list:

listaAmostras:Tlist;

and the variables

amo:TAAmostra;
pes:TPPesagem

I then create a new dynamic variable and place it in the TList:

        new(amo);
        amo.numamostra:=b;
        ...
        for a:=1 to 5 do
        begin
            new(pes);
            pes.n:=a;
            ...
        end;

        amo.listapesagens.add(pes);

        listaamostras.add(amo);

So, in this way I can easily access the info I want like this:

procedure search(amo:TAAmostra);
var
    a:integer;
    pes:TPPesagem;
begin
    for a:=0 to amo.listapesagens.count-1 do
    begin
        pes:=amo.listapesagens.items[a];
        ...
        ...
    end;
end;

So, this is how I manipulate the structures.
To free them I do something like this:

        for a:=0 to listaAmostras.count-1 do
        begin
            amo:=listaAmostras.Items[a];
            for b:=0 to amo.ListaPesagens.count-1 do
            begin
                pes:=amo.ListaPesagens.items[b];
                dispose(pes);
            end;
            amo.ListaPesagens.Clear;
            amo.ListaPesagens.capacity:=0;
            dispose(amo);
        end;
        listaamostras.clear;
        listaamostras.capacity:=0;

My problem is this: I have a procedure that "refreshes" the data in the
structures. To do so, I want to free the memory allocated to the TAAmostra
structure and then create a new one with the new values. Well, it seems it
isn't freeing the memory, because if I run that procedure 20 times in a row
I get an EoutofMemory exception. But if I'm freeing the memory and then
allocating space for exactly the same data, the occupied memory should
always be the same!!!
I know this description is a little long, but this is very important. My
deadline is next wednesday and I must have this problem solved. Anyone can
help?
Thanks for your time.
Best Regards

Goncalo Martins


0
Comment
Question by:reina
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 7

Accepted Solution

by:
God_Ares earned 220 total points
ID: 6985409
This is just a little test case wit a data structure simmelair to yours...

it's just a form with one button...

look in to it  and i'm shure you'll find your awnser.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


  PTHolder = ^Tholder; //Start's with a 'P' because it's a pointer
  THolder = record
              random_number : Integer;
              some_string : String;
              A_List : Tlist;
            end;

  PTSubData = ^TSubData;
  TSubData = record
                jadajada:Integer;
                bla : string;
             end;


var
  Form1: TForm1;

implementation

{$R *.DFM}

var List_of_holders : TList;

procedure TForm1.Button1Click(Sender: TObject);
var Holder : PTHolder;
    Sub:PTSubData;
    j,i:Integer;
    before,after : Integer;

begin
  before := GetHeapStatus.TotalAllocated;
  List_of_holders := Tlist.Create;

  For j:=1 to 10 Do  //create some data
  Begin

    new(Holder); //make a new holder
    Holder^.random_number := Random(1000);
    Holder^.some_string := 'BlaBla';
    Holder^.A_List := Tlist.Create; //create a list
    For i:=1 to 5 do
    Begin
      new(Sub); //create new sub data
      Sub^.jadajada := (j*10)+i;
      Sub^.bla := 'ad';

      Holder^.A_List.Add(sub); //store in holder list.
    End;

    List_of_holders.Add(Holder); //store holder in holders list
  end;

  For j:=0 to List_of_holders.Count-1 Do  //now we'll free it all.
  Begin
    Holder := List_of_holders.Items[j];

    For i:=0 to Holder^.A_List.Count-1 Do
    Begin
      Sub := Holder^.A_List.Items[i];
      //ShowMessage(IntToStr(Sub^.jadajada));  //if you uncomment't this line a
                                               //memory leak will occur.
                                               //due to showmessage.
      Dispose(Sub);  // free
    End;
    Holder^.A_List.free; //Free's memmory for this object.

    Dispose(Holder); //free's this one.
  End;

  List_of_holders.free; //don't forget to free this one too.

  after := GetHeapStatus.TotalAllocated;

  if after = before then showmessage('it''s ok') else showmessage(IntToStr(Abs(before-after)) + ' bytes lost' );




end;

end.
0
 
LVL 9

Assisted Solution

by:ITugay
ITugay earned 220 total points
ID: 6985476
Hi reina,

there is another way to keep list of records. Declare your record as class of TObject and use TObjectList. It able to be responsible for automatic free objects inside. Take a look at Delphi's help. It is really easy and comforatble list.

-----
Igor.

   
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9004460
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split Points between ITugay and God_Ares

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question