[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Stack overflow

Posted on 2005-04-24
10
Medium Priority
?
386 Views
Last Modified: 2010-04-05
I have a function that gets 39 pixels and stores them in an array. Then sends this array to the below function to compare to other 78 constant arrays. If the array of pixels captured from the screen does not match any of the 78 constant arrays the function returns false and the calling function will delay for 2 seconds and repeat capturing and calling.. Now I have stack overflow message 15 minutes after running the program.

function DoesArrayMatch(value:array of integer):boolean;
var
s,i:integer;
const
  Array1: TMyArrays = (0, 16777215, 16777215,
    15658734, 0, 0,
    6710886, 12303291, 10066329, 2236962, 0, 5592405,

    16777215, 16777215, 10066329, 0, 0, 12303291, 16777215,
    16777215, 8489089,
    2305827, 990223, 1253395, 1253395, 1253395,

    1253395, 1121809, 1121809, 1253395, 1253395, 1253395,
    1056016, 1976862,
    7304815, 9936023, 5397330, 1845532, 5857881);

  Array2: TMyArrays = (0, 16777215, 16777215,
    14607838, 26112,
    26112, 6726502, 12310203, 10076825, 2259490,

    26112, 5609813, 16777215, 16777215, 10076825, 26112,
    26112, 12310203,
    16777215, 6135389, 354565, 25856, 26112, 26112, 26112,

    26112, 26112, 26112, 26112, 26112, 26112, 26112, 1208594,
    6792295, 9025673,
    3967548, 748555, 3113519, 16777215);

  Array3: TMyArrays = (0, 16777215, 16777215,
    15658751, 255, 255,    6711039, 12303359, 10066431, 2237183, 255,    5592575, 16777215,
.....................................................................................etc(78  such const arrays)
begin
{comparing here}
end;

I read about memory management in delphi help but could not figure out how to use getmem and freemem with these constant arrays.
Any help appreciated
thanks
0
Comment
Question by:oalawna
  • 5
  • 4
10 Comments
 
LVL 5

Expert Comment

by:paulb1989
ID: 13853004
Maybe if at the end of your function you add SetLength(value, 0); to empty the array.
0
 

Author Comment

by:oalawna
ID: 13853013
Does doing the same things for the 78 constant arrays produce same effect?
0
 

Author Comment

by:oalawna
ID: 13853016
I think the answer is no since they are not dynamic
I actually want to know how to free memory of the constant arrays.
Can i create pointers to them and then set them to nil when i need so? let me try this.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Accepted Solution

by:
esoftbg earned 2000 total points
ID: 13853018
You don't need to use GetMem and FreeMem for the constant arrays ....
There is a working example with 2 constant arrays. It works fine for me. It may be will work fine with 78 constant arrays too ....

unit Unit1;

interface

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

type
  TMyArrays = array[0..38] of Integer;
  TCompareArr = array of Integer;
  TForm1 = class(TForm)
      Button1: TButton;
      procedure Button1Click(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function DoesArrayMatch(Value: TCompareArr): Boolean;
var
  B:     Boolean;
  s,i:   Integer;
const
  Array1: TMyArrays = (0, 16777215, 16777215,
    15658734, 0, 0,
    6710886, 12303291, 10066329, 2236962, 0, 5592405,

    16777215, 16777215, 10066329, 0, 0, 12303291, 16777215,
    16777215, 8489089,
    2305827, 990223, 1253395, 1253395, 1253395,

    1253395, 1121809, 1121809, 1253395, 1253395, 1253395,
    1056016, 1976862,
    7304815, 9936023, 5397330, 1845532, 5857881);

  Array2: TMyArrays = (0, 16777215, 16777215,
    14607838, 26112,
    26112, 6726502, 12310203, 10076825, 2259490,

    26112, 5609813, 16777215, 16777215, 10076825, 26112,
    26112, 12310203,
    16777215, 6135389, 354565, 25856, 26112, 26112, 26112,

    26112, 26112, 26112, 26112, 26112, 26112, 26112, 1208594,
    6792295, 9025673,
    3967548, 748555, 3113519, 16777215);
  function  Compare(Arr: TMyArrays): Boolean;
  var
    Ok:     Boolean;
    I:      Integer;
  begin
    Ok := True;
    for I := 0 to 38 do
    begin
      if (Value[I] <> Arr[I]) then
      begin
        Ok := False;
        Break;
      end;
    end;
    Result := Ok;
  end;
begin
  {comparing here}
  B := True;
  B := Compare(Array1);
  if not B then
    B := Compare(Array2);
  {  
  if B then
    B := Compare(Array3);
  }
  Result := B;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  CA:     TCompareArr;
  S:      string;
begin
  SetLength(CA, 78);
  CA[00] := 0;
  CA[01] := 16777215;
  CA[02] := 16777215;
  CA[03] := 14607838;
  CA[04] := 26112;
  CA[05] := 26112;
  CA[06] := 6726502;
  CA[07] := 12310203;
  CA[08] := 10076825;
  CA[09] := 2259490;
  CA[10] := 26112;
  CA[11] := 5609813;
  CA[12] := 16777215;
  CA[13] := 16777215;
  CA[14] := 10076825;
  CA[15] := 26112;
  CA[16] := 26112;
  CA[17] := 12310203;
  CA[18] := 16777215;
  CA[19] := 6135389;
  CA[20] := 354565;
  CA[21] := 25856;
  CA[22] := 26112;
  CA[23] := 26112;
  CA[24] := 26112;
  CA[25] := 26112;
  CA[26] := 26112;
  CA[27] := 26112;
  CA[28] := 26112;
  CA[29] := 26112;
  CA[30] := 26112;
  CA[31] := 26112;
  CA[32] := 1208594;
  CA[33] := 6792295;
  CA[34] := 9025673;
  CA[35] := 3967548;
  CA[36] := 748555;
  CA[37] := 3113519;
  CA[38] := 16777215;
  try
    S := 'The array does not match';
    if DoesArrayMatch(CA) then
      S := 'The array does match';
  finally
    SetLength(CA, 0);
    ShowMessage(S);
  end;
end;

end.
0
 

Author Comment

by:oalawna
ID: 13853031
thank you
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 13853048
oalawna, you are welcome.

I tested with 78 constant arrays: it works fine too.

Best regards,
Emil
0
 

Author Comment

by:oalawna
ID: 13853204
Emil than you. Just one more small related question.
This function above is acutally part of a class. The object created is destroyed every minute at most. Are local variables in functions included in classes also freed when we destroy that object.
I did not override the create or destroy methods.
Thanks
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 13854062
oalawna excuse me,
I was out of this question for some hours....
When we use some local variables in functions and procedures as:

function DoesArrayMatch(Value: TCompareArr): Boolean;
var
  B:     Boolean;
  s,i:   Integer;
const
  Array1: TMyArrays = (0, 16777215, 16777215, //....
//........
begin
//........
end;

The procedure or function works using the stack and they are freed automatically immediately after procedure's/function's conclusion. You don't need to free them. May be only if you have some dynamicaly created arrays, it is a good style of programming to set them:
  SetLength(DynamicArrayName, 0);
at the end of procedure/function.

I would say the same about constants, variables and dynamicaly created arrays that are declared as a part of a class:
it is a good style of programming to set only the dynamicaly created arrays:

destructor  TClassName.Destroy;
begin
  SetLength(DynamicArrayName, 0);
  ingerited Destroy;
end;

So may be you need to override the Destroy method if you have included another components and/or dynamicaly created arrays as a part of a class....

Emil
0
 

Author Comment

by:oalawna
ID: 13854078
thanks emil. Your help appreciated
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 13854137
oalawna, you are welcome
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month20 days, 12 hours left to enroll

864 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