1

I'm pretty newbie to Delphi, so be kind please. I'm working on a software that allows users to customize its interface ( button's location, appearance, wtv) and some other stuff. The problem is, i have a grid and i store a representation of its cells on a dynamic array of Boolean which represents which cells are occupied. But when i try to free that matrix Sometimes i get an invalid pointer operation. Sometimes there isnt any error, but other times i get that invalid pointer stuff.

Definition:

type
  TMatrix = array of array of Boolean;

var
  Matrix: TMatrix;

Initialization:

SetLength(Matrix, MyGrid.ColumnCollection.Count, MyGrid.RowCollection.Count);

Usage: Mostly, these kind of operations are the only ones that i use with the matrix, and i'm sure that those variables col,row,colspan,rowspan never have values greater than the array boundary

//Checks if a set of cells are occupied
for i := column to (column + columnspan)-1 do
          begin
            for j := row to (row + rowspan)-1 do
            begin
              if Matrix[i, j] then
              begin
                valido := False;
              end;
            end;
          end;
     // if there isnt any cell occupied i can move a component to that location and i set the cells to true ( occupied)
    if (valido) then
          begin
              for i := column to (column + colspan)-1 do
                begin
                  for j := row to (row + rowspan)-1 do
                  begin
                    Matrix[i,j]:= True;
                  end;
                end;
          end

Free:

try
            begin
               SetLength(Matrix,0,0);
            end;
          except
            on E : Exception do
            begin
              //todo
              ShowMessage('Free: ' + E.Message);
            end;
          end;

I'm using FASTMM4 and i do get the memory leak warning, but i cant understand its contents..

What can possibly be causing this error? Im using Delphi Xe6 Firemonkey Any help will be appreciated. Thanks

3
  • There's a bug in your code, but it's in the code that we cannot see. Please provide a complete program that demonstrates the fault. Commented Feb 19, 2015 at 15:46
  • The code you have shown would never expose this issue. I suspect you are using wrong indexes causing memory corruption. Do you have range checking enabled on your project? Commented Feb 19, 2015 at 15:49
  • My code only crashes when i call the procedure that free's the matrix. Sometimes, the exception is called,sometimes the exception doesnt fire but i get fastmm4 error and the invalid pointer, but most of the time the code executes well. Either way, i will edit my question with my initialization of the matrix and a procedure that uses it.. Commented Feb 19, 2015 at 15:59

1 Answer 1

1

The most likely explanation for this is that you are writing outside the bounds of the array. There's not enough code for us to be sure that is the case, but the symptoms you report are invariably caused by out-of-bounds access.

Your next step is to get the compiler to write code that checks for our-of-bounds access. In your project options, in the compiler section, find the range checking option. Enable this option. Now the compiler will emit code that checks that your array indices are valid whenever you access the array. If your code fails the test, at runtime, an exception will be raised. This will make it blatantly obvious which part of your code is defective.

Sign up to request clarification or add additional context in comments.

4 Comments

I was so sure that i wasnt messing up the variables that could jump the bounds of the array that i forgot to check if i wasnt swapping columns with rows.. And that was the problem, i feel stupid. Another lesson learned. Thank you.
Range checking is your friend! ;-)
Ah glad to see that my psychic debugging skills were correct :)
@whosrdaddy FWIW, I actually wrote my answer before I read your comment. Clearly this had to be a heap corruption and out of bounds access is the best guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.