Solved

dangerling object pointers

Posted on 1998-04-28
3
162 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
indy Tmemory stream non latin charcter issue 2 98
Virtuailstring tree compare node issue 14 105
Delphi Form ownership 4 73
how to update exe applicatio from internet ? 6 68
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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