RAD Studio VCL Reference
|
Destroys an object and frees its associated memory, if necessary.
procedure Free;
__fastcall Free();
Use System::TObject::Free to destroy an object. System::TObject::Free automatically calls the destructor if the object reference is not nil. Any object instantiated at runtime that does not have an owner should be destroyed by a call to System::TObject::Free so that it can be properly disposed of and its memory released. Unlike System::TObject::Destroy, System::TObject::Free is successful even if the object is nil; so if the object was never initialized, System::TObject::Free won't result in an error.
When you call System::TObject::Free for a component, it calls System::TObject::Free for all components that it owns—that is, all components in its component list. Since a form owns all the controls and other components that are created on it in design mode, those components are automatically freed when the form is freed. By default, all forms are owned by the Application object; when the application terminates, it frees the Application object, which frees all forms. For objects that are not components, or for components created with a nil owner, be sure to call System::TObject::Free after you are finished with them; otherwise the allocated memory will not be usable until after the application terminates.
Delphi Examples:
{ This example demonstrates how to add and delete menu items to a popup menu at runtime and assign an event handler to the OnClick event. Place a TPopupMenu and three buttons on the form named "AddButton", "EditButton", and "DestroyButton" and add OnClick events to all three buttons. Put the TPopupMenu in the PopupMenu property of the form. Place the PopupMenuItemsClick procedure in the TForm1 type declaration so that it can be used as the method call for the menu item OnClick event. } type TForm1 = class(TForm) AddButton: TButton; EditButton: TButton; DestroyButton: TButton; PopupMenu1: TPopupMenu; procedure AddButtonClick(Sender: TObject); procedure EditButtonClick(Sender: TObject); procedure DestroyButtonClick(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public procedure PopupMenuItemsClick(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.AddButtonClick(Sender: TObject); var index: Integer; NewItem: TMenuItem; begin // The owner (PopupMenu1) will cleanup this menu item. NewItem := TMenuItem.Create(PopupMenu1); // create the new item index := PopupMenu1.Items.Count; PopupMenu1.Items.Add(NewItem);// add it to the Popupmenu NewItem.Caption := 'Menu Item ' + IntToStr(index); NewItem.Tag := index; NewItem.OnClick := PopupMenuItemsClick; // assign it an event handler end; procedure TForm1.PopupMenuItemsClick(Sender: TObject); begin with Sender as TMenuItem do begin case Tag of 0: ShowMessage('first item clicked'); 1: ShowMessage('second item clicked'); 2: ShowMessage('third item clicked'); 3: ShowMessage('fourth item clicked'); end; end; end; { To edit or destroy an item, grab its pointer via the Items property. } procedure TForm1.EditButtonClick(Sender: TObject); var ItemToEdit: TMenuItem; begin ItemToEdit := PopupMenu.Items[1]; ItemToEdit.Caption := 'Changed Caption'; end; procedure TForm1.DestroyButtonClick(Sender: TObject); var ItemToDelete: TMenuItem; begin ItemToDelete := PopupMenu.Items[2]; ItemToDelete.Free; end; procedure TForm1.FormCreate(Sender: TObject); var index: Integer; NewItem: TMenuItem; begin for index := 0 to 3 do begin // The owner (PopupMenu1) will cleanup this menu item. NewItem := TMenuItem.Create(PopupMenu1); // create the new item PopupMenu1.Items.Add(NewItem);// add it to the Popupmenu NewItem.Caption := 'Menu Item ' + IntToStr(index); NewItem.Tag := index; NewItem.OnClick := PopupMenuItemsClick; // assign it an event handler end; end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|