Deleting linked components

I have component1 that has another component2 as a published property.

In the IDE:

I set this property of component1 so that it links (points to) component2.

So far so good.

Now, when I delete component2 the property in component1 still points to component2 and causes serious problems with the IDE reporting memory access violations.

Do I need to clear the property in component1 that points to (the now deleted) component2.

And if so how do I do that, do I need component2 to have knowledge of component1?
LVL 6
zebadaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rarigoCommented:
Hi zebada,

   A piece of code would be nice. Don't you think so?


Tchau,
Reginaldo
0
zebadaAuthor Commented:
Here's sample code that demonstrates the problem:

1. Place both components on a form, from the "My stuff" palette page.
2. Set the property "other" of Image1 to Other1.
3. Delete Other1 from the form.
4. BANG!

unit Image1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls;

type
  TOther = class(TImage)
  end;

  TImage1 = class(TImage)
  private
    Fother: TOther;
    procedure Setother(const Value: TOther);
    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }
  published
    { Published declarations }
    property other: TOther read Fother write Setother;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('My Stuff', [TImage1,TOther]);
end;

{ TImage1 }

procedure TImage1.Setother(const Value: TOther);
begin
  Fother := Value;
end;

end.
0
GreymanCommented:
You need to override the Notification method of TComponent, which is provided as a protected method for exactly this purpose.  For instance:

interface

TMyWidget = class(TComponent);
private
  fThing : TComponent;
protected
  procedure Notification(AComponent:TComponent; Operation : TOperation); override;
published
  property Thing : TComponent read fThing write fThing
end;


implementation

procedure TMyWidget.Notification (AComponent:TComponent; Operation : TOperation);
begin
  if (AComponent = Thing) and (Operation = opRemove) then begin
    fThing := nil;
  end;
  inherited Notification(AComponent,Operation);
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zebadaAuthor Commented:
Perfect :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.