[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Invalid pointer to object's method in Destroy

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

649 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