Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

dangerling object pointers

Posted on 1998-04-28
3
Medium Priority
?
190 Views
Last Modified: 2010-04-06
Hi

I would like to know how I can detect dangerling object-pointers.

Ex:
Button1: TButton;
Button2: TButton;
...
Button1 := TButton.Create;
Button2 := Button1;
Button1.Free;
<< if I here try to get to any of Button2's attributes I of cause get af AccesError>>

I would like to be able to check i an object-reference points to a freed object. I cant use Assigned, cause Assigned only checks to see if the pointer is nil. My question is how do I detect if a object-reference points to somewhere that is no longer a object (It has been freed) - In the ex. above I would like to be able to do something like "Valid(Button2)" and get false, cause the object Button2 points to no longer exists.

Thanx
0
Comment
Question by:steff
3 Comments
 
LVL 4

Accepted Solution

by:
d003303 earned 400 total points
ID: 1338137
Yo,
the easies way is to force an access violation and trap it with a silent exception handler. Like this :

function Valid(Instance : TObject): Boolean;
var CheckObj : IUnknown;
begin
  Result := true;
  try
    Instance.GetInterface(IUnknown, CheckObj);
  except
    Result := false;
  end;
end;

If the passes instance is not valid anymore, the function returns false.

Slash/d003303
0
 

Expert Comment

by:BigMadDrongo
ID: 1338138
For a quick and dirty solution, create a function like the following:

function Valid(Button: TButton): boolean
begin
  try
    if Button.Caption := '' then;
    Result := True;
  except
     Result := False;
  end;
end;

This is a really dirty way of doing it.  It access a property of the button, and if an exception is raised, the error handler returns false, else true is returned.

BigMadDrongo
0
 

Author Comment

by:steff
ID: 1338139
Thanx.

The solution from you is exactly how I have solved the problem until now, but I thought
that I was toooo dirty. But after getting the same solution for a "expert" I feel better about
it.

Per
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
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.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

783 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