?
Solved

Invalid pointer to object's method in Destroy

Posted on 2013-05-28
4
Medium Priority
?
397 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 46

Expert Comment

by:aikimark
ID: 39205781
@PC2stein

please accept your comment http:#a39203805 as the solution

aikimark -- zone advisor
0
 
LVL 38

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month15 days, 23 hours left to enroll

850 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