Solved

TList questions.

Posted on 2003-11-20
8
779 Views
Last Modified: 2007-12-19
I understand that TList will use HEAP memory.
Is it possible to set the heap size during runtime to be as much as possible ?
I need to be able to allocate huge memory in the RAM to store larga data.

Also, this is strange, I put this code in a button click event :

  I := 0;
  Label1.Caption := '';
  done := FALSE;
  while not done do
  begin
      try
         ATlist.Add(Something);
         Inc(I);
      except
         done := TRUE;
      end;
  end;
  Label1.Caption := IntToStr(I);

First click, I = 100 ... and exception occured (Out Of Memory).
But I still can click for the second, third time. Each time, variable I is about 100.
Is the Out of Memory error only temporary ? and then TList will free the memory automatically ?

Please help.
0
Comment
Question by:nova2002
  • 3
  • 2
8 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 28 total points
ID: 9787084
>and then TList will free the memory automatically ?
defintivly no, its just a list of pointers, you have to care to free
the dangled memory-location by self

>Is it possible to set the size during runtime
yes there is a capacity-property

from delphi help:
------------------
Specifies the allocated size of the array of pointers maintained by the TList object.

property Capacity: Integer;

Description

Set Capacity to the number of pointers the list will need to contain. When setting the Capacity property, an EOutOfMemory exception occurs if there is not enough memory to expand the list to its new size.

Read Capacity to learn number of objects the list can hold without reallocating memory. Do not confuse Capacity with the Count property, which is the number of entries in the list that are in use. The value of Capacity is always greater than or equal to the value of Count. When Capacity is greater than Count, the unused memory can be reclaimed by setting Capacity to Count.

When an object is added to a list that is already filled to capacity, the Capacity property is automatically increased. Setting Capacity before adding objects can reduce the number of memory reallocations and thereby improve performance. For example,

Delphi example:

  List.Clear;
  List.Capacity := Count;
  for I := 1 to Count do List.Add(...);


The assignment to Capacity before the for loop ensures that each of the following Add operations doesn't cause the list to be reallocated. Avoiding reallocations on the calls to Add improves performance and ensures that the Add operations never raise an exception.
------------

meikl ;-)
0
 

Author Comment

by:nova2002
ID: 9787368
My big problem currently, I did not know when should I stop adding an item to the Tlist. Because it seems that I can not trap any exception, EOutOfResources or EOutOfMemory.

It always display "Not enough storage is available to process this command."

Because I want to store as much as possible item in the TList, I just want to stop when there is no more memory. But I cant trap it.
I run in the IDE or not, same result. I turn off debugger (dont stop on language exception.).

Any ideas ?

Here are my codes :
=============

  Label1.Caption := '';
  Ok := TRUE;

  try
    New(Something);
    Something^ := TJPEGImage.Create;
  except
    ShowMessage('ERROR1');
    Ok := FALSE;
  end;

  if Ok then
  begin
    I := 0;
    Label1.Caption := '';
    done := FALSE;
    while not done do
    begin
        try
           ATlist.Add(Something);
           Inc(I);
        except
           ShowMessage('ERROR2');
           done := TRUE;
        end;
    end;
    Label1.Caption := IntToStr(I);
  end;
0
 
LVL 2

Assisted Solution

by:Robn
Robn earned 27 total points
ID: 9787473
I'm not sure what this code is doing or if this was just something you put together to get your point across. If this is your code, you have at least 1 memory leak for every item you're adding to the list.

If this list is supposed to store a JPEG image, you could do something like this.

procedure AddImage;
var
  jpg: TJPEGImage;
begin
  jpg := TJPEGImage.Create;
  ATlist.Add(jpg);
end;

Your code is also adding the same image until an exception is raised. I'm assuming you did this to demonstrate your point about catching the exception?
I would like to see the code you have to populate this list if you don't mind.

Thanks,
Rob
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 9787499
robn is right,
a delphi-object itself is not more than a pointer,
you can directly add this object into the list,
without creating a pointer for it

your exception-handling should work,
but i guess you left to less memory for the os,
so that the os becomes unstable

meikl ;-)
0
 

Author Comment

by:nova2002
ID: 9787744
Here is my codes (parts). My goal is to capture flash movie into series of JPEG stored in the memory by using Tlist.
Hope you can find how to achieve this. Strange, because I cant trap out of resources/memory exception.

type
  PJPEG = ^TJPEGImage;

  TMemJPEG = class
    FBuffer     : TList;
    FPath       : string;
    FPrefix     : string;
    FIndex      : integer;  
    FAvailable  : integer;

    constructor Create(Path, Prefix: string);
    destructor Free;

    function Save(Bmp: TBitmap) : boolean;
  end;

// Clicking Capture Button, start capturing flash movie to the memory
procedure TMainForm.btnCaptureClick(Sender: TObject);
var
  w, h: integer;
  GetPoint: TPoint;
  FCanvas : TCanvas;
  R : TRect;
begin
  done := FALSE;

  lbStatus.Caption := 'Capturing ...';

  GetPoint  := Flash.ClientToScreen(Point(0,0));

  w := Flash.Width;
  h := Flash.Height;

  R := Rect(0, 0, w, h);

  bmp := TBitmap.Create;
  bmp.Width  := w;
  bmp.Height := h;

  FCanvas := TCanvas.Create;

  FCanvas.Handle := getDC(Flash.Handle);

  while not done do
  begin
    bmp.Canvas.CopyRect(R, FCanvas, R);

    try
        // This will call TMemJPEG.Save();
        if not mjpg.Save(bmp) then done := TRUE;
    except
        ShowMessage('Error: Blah ');
    end;

    Application.HandleMessage;

    // Debugging
    lbStatus.Caption := IntToStr(mjpg.FBuffer.Count) + ' ' + IntToStr(mjpg.FBuffer.Capacity);
  end;

  ReleaseDC(0, FCanvas.Handle);
  FCanvas.Free;
  lbStatus.Caption := IntToStr(mjpg.FIndex) + ' ' + IntToStr(mjpg.FBuffer.Count);
end;

function TMemJPEG.Save(Bmp: TBitmap) : boolean;
var
  AJPEG: PJPEG;
begin
  Result := TRUE;
  try
     New(AJPEG);
     AJPEG^ := TJPEGImage.Create;
     AJPEG^.CompressionQuality := 10;
     AJPEG^.Assign(Bmp);
  except
      ShowMessage('Error 1');
      Result := FALSE;
  end;

  if Result then
    try
      FBuffer.Add(AJPEG);
      Inc(FIndex);
    except
      on E: Exception do begin
        ShowMessage('Error: '+E.Message);
        Result := FALSE;
      end;
    end;
   //Dispose(AJPEG);    if I dispose this, I cant get the jpeg back from TList (for Playback function)
end;
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9787843
after a fast view, i would change this

function TMemJPEG.Save(Bmp: TBitmap) : boolean;
var
  AJPEG: TJPEGImage;
begin
  Result := TRUE;
  try
     //New(AJPEG); not needed
     AJPEG := TJPEGImage.Create;
     AJPEG.CompressionQuality := 10;
     AJPEG.Assign(Bmp);
  except
      ShowMessage('Error 1');
      Result := FALSE;
  end;

  if Result then
    try
      FBuffer.Add(AJPEG);
      Inc(FIndex);
    except
      on E: Exception do begin
        ShowMessage('Error: '+E.Message);
        Result := FALSE;
      end;
    end;
   //Dispose(AJPEG);    if I dispose this, I cant get the jpeg back from TList (for Playback function)
end;

additional i would at startup set the capacity to a high value,
this may speed up things

meikl ;-)

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now