Solved

Invalid pointer to object's method in Destroy

Posted on 2013-05-28
4
383 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
Comment Utility
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
Comment Utility
@PC2stein

please accept your comment http:#a39203805 as the solution

aikimark -- zone advisor
0
 
LVL 36

Expert Comment

by:Geert Gruwez
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now