Link to home
Start Free TrialLog in
Avatar of steff
steff

asked on

dangerling object pointers

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
ASKER CERTIFIED SOLUTION
Avatar of d003303
d003303

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BigMadDrongo
BigMadDrongo

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
Avatar of steff

ASKER

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