Solved

Invalid pointer to object's method in Destroy

Posted on 2013-05-28
4
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

749 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