?
Solved

dangerling object pointers

Posted on 1998-04-28
3
Medium Priority
?
184 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
[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
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

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

719 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