how to clear memory region that point by a pointer??

Thank you for reading this message,

I have found out that my program had a serious memory leak, therefore I use MemProve to trace the memory allocation at runtime to debug my programme.

one example place that the memory leak is:


hardware := TAbacus.Create(4096);
hardware.ReadAbacus(no_Channel, no_sample);
PMatrix := hardware.getData; // Memory leak

where PMatrix : ^Matrix; (Matrix is a record that I define for storing a matrix (16 x 4096));

The PMatrix is inside a function inside a thread which will repeatly called every sec. Therefore the PMatrix will exist as long as the thread exist.

The memory region that the PMatrix is pointing is the place while the memory leak. I want to know how to clear that memory without destroying the pointer itself.

I have try freeMem(PMatrix) which actually freeing the pointer and during the second call to the function, an error "invalid pointer" occur.

Thank you very much!

Ferdinand

Comments

  • : Thank you for reading this message,
    :
    : I have found out that my program had a serious memory leak, therefore I use MemProve to trace the memory allocation at runtime to debug my programme.
    :
    : one example place that the memory leak is:
    :
    :
    : hardware := TAbacus.Create(4096);
    : hardware.ReadAbacus(no_Channel, no_sample);
    : PMatrix := hardware.getData; // Memory leak
    :
    : where PMatrix : ^Matrix; (Matrix is a record that I define for storing a matrix (16 x 4096));
    :
    : The PMatrix is inside a function inside a thread which will repeatly called every sec. Therefore the PMatrix will exist as long as the thread exist.
    :
    : The memory region that the PMatrix is pointing is the place while the memory leak. I want to know how to clear that memory without destroying the pointer itself.
    :
    : I have try freeMem(PMatrix) which actually freeing the pointer and during the second call to the function, an error "invalid pointer" occur.
    :
    : Thank you very much!
    :
    : Ferdinand
    :
    How about calling FreeMem() and immediately followed by a GetMem(). This should free the memory and still keep the pointer valid.
  • Thank you zibadian,

    You save me again! I have try to use freemem and getmem. I forgot to mention that in the last message about the freeMem wouldn't work on the pointer.

    I was written:

    var
    PMatrix : ^Matrix;
    begin
    PMatrix := hardware.getData; // Memory leak
    FreeMem(PMatrix); // Error occur.
    getMem(PMatrix, sizeof(Matrix));
    end;

    It complain that invalid pointer operation!! Pls help me cause I don't know what is going on??

    Thank you very much!

    Ferdinand

  • : Thank you zibadian,
    :
    : You save me again! I have try to use freemem and getmem. I forgot to mention that in the last message about the freeMem wouldn't work on the pointer.
    :
    : I was written:
    :
    : var
    : PMatrix : ^Matrix;
    : begin
    : PMatrix := hardware.getData; // Memory leak
    : FreeMem(PMatrix); // Error occur.
    : getMem(PMatrix, sizeof(Matrix));
    : end;
    :
    : It complain that invalid pointer operation!! Pls help me cause I don't know what is going on??
    :
    : Thank you very much!
    :
    : Ferdinand
    :
    :
    Perhaps you need to use Dispose()/New() instead. I'm not familiar with the hardware.GetData() method, so I don't know how it creates the pointer. That is why I'm just handing you several options, which might or might not work.
  • Sorry,I didn't explain the problem probably. >.<

    Ummm. Even if I use new and dispose, it still came out with the invalid pointer operation error. I will try to explain everything in a simple and clear.

    Matrix is a record:
    type
    Matrix = record
    sizeX : Integer;
    sizeY : Integer;
    Matrix : Array of Array of TComplex;
    end;

    // Problem Region
    var hardware : TAbacus;
    var PMatrix : ^Matrix;

    hardware := TAbacus.Create(4096);
    hardware.ReadAbacus(no_Channel, no_sample);
    new(PMatrix);
    PMatrix := hardware.getData;

    // do some calculation

    dispose(PMatrix); // Problem error occur

    // Error

    TAbacus is a class that I wrote to obtain data from a DSP card. Inside the TAbacus, there is a private variable data which is matrix type.

    Private : data : Matrix;

    one function call readAbacus will collect data from DSP card and store it into data.

    getData function used to return "data".

    function TAbacus.getData : Pointer;
    begin
    Result := Addr(data);
    end;

    I don't know what is going on?? is it becasue I use dynamic array in the matrix type, therefore the memory need special way to clear it??

    Thank you very much!

    Ferdinand >.<


  • : Sorry,I didn't explain the problem probably. >.<
    :
    : Ummm. Even if I use new and dispose, it still came out with the invalid pointer operation error. I will try to explain everything in a simple and clear.
    :
    : Matrix is a record:
    : type
    : Matrix = record
    : sizeX : Integer;
    : sizeY : Integer;
    : Matrix : Array of Array of TComplex;
    : end;
    :
    : // Problem Region
    : var hardware : TAbacus;
    : var PMatrix : ^Matrix;
    :
    : hardware := TAbacus.Create(4096);
    : hardware.ReadAbacus(no_Channel, no_sample);
    : new(PMatrix);
    : PMatrix := hardware.getData;
    :
    : // do some calculation
    :
    : dispose(PMatrix); // Problem error occur
    :
    : // Error
    :
    : TAbacus is a class that I wrote to obtain data from a DSP card. Inside the TAbacus, there is a private variable data which is matrix type.
    :
    : Private : data : Matrix;
    :
    : one function call readAbacus will collect data from DSP card and store it into data.
    :
    : getData function used to return "data".
    :
    : function TAbacus.getData : Pointer;
    : begin
    : Result := Addr(data);
    : end;
    :
    : I don't know what is going on?? is it becasue I use dynamic array in the matrix type, therefore the memory need special way to clear it??
    :
    : Thank you very much!
    :
    : Ferdinand >.<
    :
    :
    :
    This is what I needed to know. The memory leak isn't the GetData(), but the New() before it. The Addr() function returns a valid pointer. Here is what is happening:
    [code]
    hardware := TAbacus.Create(4096);
    hardware.ReadAbacus(no_Channel, no_sample);
    new(PMatrix);
    // Allocate a memory block and create a pointer to it
    PMatrix := hardware.getData;
    // Overwrite the pointer value with a new valid pointer, and removing
    // the previous reference in the process
    [/code]
    Each call to the New() statement allocates a new Matrix, to which you loose the pointer value in the next call. If you want to copy the Data field to the PMatrix, you need to change the GetData() into:
    [code]
    procedure TAbacus.GetData(PMatrix: Pointer);
    begin
    Move(Data, PMatrix^, SizeOf(Matrix));
    end;
    [/code]
    You can also make it simpler (and less buggy) for yourself if you change the code to:
    [code]
    function TAbacus.GetData: Matrix;
    begin
    Result := Data;
    end;

    // Problem Region
    hardware := TAbacus.Create(4096);
    hardware.ReadAbacus(no_Channel, no_sample);

    with Hardware.GetData do
    begin
    // example calculation
    NumberOfMatrixElements := SizeX*SizeY;
    // perform calculations
    end;
    [/code]
    This code allows you to do calculations directly on the Data.
  • Thank you zibadian,

    I have solve the problem. Thank you very much x 1000

    Ferdinand
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories