• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

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?
0
zebada
Asked:
zebada
  • 2
1 Solution
 
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
 
zebadaAuthor Commented:
Perfect :)
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now