Link to home
Start Free TrialLog in
Avatar of oalawna
oalawna

asked on

Stack overflow

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
Avatar of paulb1989
paulb1989

Maybe if at the end of your function you add SetLength(value, 0); to empty the array.
Avatar of oalawna

ASKER

Does doing the same things for the 78 constant arrays produce same effect?
Avatar of oalawna

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of esoftbg
esoftbg
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of oalawna

ASKER

thank you
oalawna, you are welcome.

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

Best regards,
Emil
Avatar of oalawna

ASKER

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
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
Avatar of oalawna

ASKER

thanks emil. Your help appreciated
oalawna, you are welcome