Solved

dangerling object pointers

Posted on 1998-04-28
3
158 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 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

8 Experts available now in Live!

Get 1:1 Help Now