Solved

Invalid pointer to object's method in Destroy

Posted on 2013-05-28
4
386 Views
Last Modified: 2013-06-08
Have a problem with invalid pointer to object's method in Destroy. Does anybody have any idea why? Here is the code snippet:

Type
  TThreadSafeParametersList = class(TThreadList)
    constructor Create;
    destructor Destroy; override;
    procedure FreeAllAndClearList;
  end;

Implementation

constructor TThreadSafeParametersList.Create;
begin
  inherited Create;
  self.Duplicates := dupAccept;
end;

destructor TThreadSafeParametersList.Destroy;
begin
  FreeAllAndClearList;  ////<= from time-to-time AV is caused by pointer to this method no longer is valid (000000)
  inherited Destroy;
end;

procedure TThreadSafeParametersList.FreeAllAndClearList;
var
  i: integer;
begin
  with LockList do try
                     for i := 0 to Count-1 do try
                                                if Assigned(TObject(Items[i])) then TObject(Items[i]).Free;
                                              finally

                                              end;
                     Clear;
                   finally
                     UnlockList;
                   end;
end;

Open in new window



Somewhere in MainForm:

Parameters := TThreadSafeParametersList.Create;

... Do something with Parameters in several working threads.
... All threads gets closed.
... In MainForm (OnCloseQuery) I wanted to clean things up but ...

FreeAndNil(Parameters); //<= this is where frome time-to-time I get an AV 000000 at address 000000
0
Comment
Question by:PC2stein
  • 2
4 Comments
 

Accepted Solution

by:
PC2stein earned 0 total points
ID: 39203805
SOLVED

I've found where to problem was - somewhere deep in code I've released an object (which was in the list) and forgot to remove it from the list.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 39205781
@PC2stein

please accept your comment http:#a39203805 as the solution

aikimark -- zone advisor
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 39228799
first: remove the item, assign it to a variable and then free the variable
> check the source code for FreeAndNil in SysUtils for a sample

second: use count down, not count up

procedure TThreadSafeParametersList.FreeAllAndClearList;
var
  i: integer;
  obj: TObject;
begin
  with LockList do 
  try
    for i := Count-1 downto 0 do 
    begin
      obj := TObject(Items[i]);
      Delete(I);
      FreeAndNil(obj);
    end;
    Clear;
  finally
    UnlockList;
  end;
end;

Open in new window


PS: not difficult to read with that identation ?
0
 

Author Closing Comment

by:PC2stein
ID: 39231374
I've managed to find source of problem by myself. Explaining a problem to other people helped me understand my problem better.
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

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…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

821 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