combobox1.items.addobject()中的对象需要释放吗?

westuser 2009-09-22 01:13:41
开发环境:delphi2007
测试代码:

procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
myClass: TMyClass;
begin
for i :=0 to 9 do
begin
myClass := TMyClass.Create;
myClass.id := 1;
myClass.context := 'cccdddeee';
ComboBox1.Items.AddObject(myClass.context,myClass);
end;
end;


前提:myClass就定义成局部变量,类成员变量或全局变量不用讨论

myClass实际上是引用,这个时候不能释放。而定义的又是局部变量,Form1.Close的时候肯定是找不着了。

问题:最后还用释放他们吗?如何释放?是否通过循环:(ComboBox1.Items[i] as myClass).Free;
...全文
337 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xingyunmm 2009-10-26
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
procedure AddItem(str:TMyStr);
var
p:Pointer; //无类型指针
begin
GetMem(P,SizeOf(str)); //固定长度
//Length(str)),实际长度;
TMyStr(p^):=str;
ComboBoxStr.Items.AddObject(str,p);
// 第二个参数实际上就是一个指针
// 指针没指向任何地方或没显式分配内存时时就是nil
// Pointer(Items.Objects[itemIndex])^ 这样取指针里的内容就会报错误
end;
begin
AddItem('aaa');
AddItem('bbb');
AddItem('ccc');
end;
xingyunmm 2009-10-26
  • 打赏
  • 举报
回复
//上面代码有错,不应该用string,要指定长度string[100]这样

type
TMyStr=string[50];


procedure TForm1.Button1Click(Sender: TObject);
procedure AddItem(str:TMyStr);
var
p:Pointer; //无类型指针
begin
GetMem(P,Length(str));
TMyStr(p^):=str;
ComboBoxStr.Items.AddObject(str,p);
// 第二个参数实际上就是一个指针
// 指针没指向任何地方或没显式分配内存时时就是nil
// Pointer(Items.Objects[itemIndex])^ 这样取指针里的内容就会报错误
end;
begin
AddItem('aaa');
AddItem('bbb');
AddItem('ccc');
end;
xingyunmm 2009-10-26
  • 打赏
  • 举报
回复

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
ComboBoxStr: TComboBox;
Button3: TButton;
Button4: TButton;
Button5: TButton;
ComboBoxInt: TComboBox;
Button6: TButton;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

const
MemSize=1024;

procedure TForm1.Button1Click(Sender: TObject);
procedure AddItem(str:string);
var
p:Pointer; //无类型指针
begin
GetMem(P,MemSize);
string(p^):='ptr: '+str;
ComboBoxStr.Items.AddObject(str,p);
// 第二个参数实际上就是一个指针
// 指针没指向任何地方或没显式分配内存时时就是nil
// string(Pointer(Items.Objects[itemIndex])^) 这样取指针里的内容就会报错误
end;
begin
AddItem('aaa');
AddItem('bbb');
AddItem('ccc');
end;

procedure TForm1.Button2Click(Sender: TObject);
var
I:Integer;
begin
with ComboBoxStr do
for I := Items.Count -1 downto 0 do
begin
FreeMem(Pointer(Items.Objects[I]),MemSize);
items.Delete(I);
//delete只是删除指针本身,所以指针指向的地方的内存要你自己负责Free
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
with ComboBoxStr do
if ItemIndex > -1 then
ShowMessage(string(Pointer(Items.Objects[itemIndex])^));
end;

procedure TForm1.Button4Click(Sender: TObject);
procedure AddItem(str:string);
begin
ComboBoxInt.Items.AddObject(str,TObject(StrToInt(str)));
//这里为什么没申请内存??因为指针本身就是用一个integer来表示的
end;
begin
AddItem('111');
AddItem('222');
AddItem('333');
end;


procedure TForm1.Button5Click(Sender: TObject);
var
I:Integer;
begin
with ComboBoxInt do
for I := Items.Count -1 downto 0 do
items.Delete(I);
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
with ComboBoxInt do
if ItemIndex > -1 then
ShowMessageFmt('%d',[Integer(Items.Objects[itemIndex])]);
end;

end.



---------------------------------------------------------------------


object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 161
ClientWidth = 505
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poDesktopCenter
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 232
Top = 40
Width = 75
Height = 25
Caption = #24314#31435
TabOrder = 0
OnClick = Button1Click
end
object Button2: TButton
Left = 394
Top = 40
Width = 75
Height = 25
Caption = #28165#38500
TabOrder = 1
OnClick = Button2Click
end
object ComboBoxStr: TComboBox
Left = 48
Top = 42
Width = 145
Height = 21
ItemHeight = 13
TabOrder = 2
Text = #27979#35797#23383#31526#20018
end
object Button3: TButton
Left = 313
Top = 40
Width = 75
Height = 25
Caption = #26597#30475
TabOrder = 3
OnClick = Button3Click
end
object Button4: TButton
Left = 232
Top = 88
Width = 75
Height = 25
Caption = #24314#31435
TabOrder = 4
OnClick = Button4Click
end
object Button5: TButton
Left = 394
Top = 88
Width = 75
Height = 25
Caption = #28165#38500
TabOrder = 5
OnClick = Button5Click
end
object ComboBoxInt: TComboBox
Left = 48
Top = 90
Width = 145
Height = 21
ItemHeight = 13
TabOrder = 6
Text = #27979#35797#25968#23383
end
object Button6: TButton
Left = 313
Top = 88
Width = 75
Height = 25
Caption = #26597#30475
TabOrder = 7
OnClick = Button6Click
end
end
Hexpate 2009-09-22
  • 打赏
  • 举报
回复
需要释放 呵呵,抢不到sf
火龙岛主 2009-09-22
  • 打赏
  • 举报
回复
不一定,只要有释放的地方就可以了。
但楼主的情况需要释放:

  for i :=ComboBox1.Items.count - 1 downto 0 do
begin
TMyClass(ComboBox1.Items.Objects[i]).Free;
//或者FreeAndNil(TMyClass(ComboBox1.Items.Objects[i]));
end;



westuser 2009-09-22
  • 打赏
  • 举报
回复
如何释放?还是我这样写已经没办法释放了?
mjp1234airen4385 2009-09-22
  • 打赏
  • 举报
回复
需要。
ZyxIp 2009-09-22
  • 打赏
  • 举报
回复
不管是局部还是全局的,自己创建的自己释放。

haitao 2009-09-22
  • 打赏
  • 举报
回复
需要人为释放

不过,如果包含这些items的对象如果能提供一个OnDeleteItem的事件就好了,可以在事件里释放,而事件是会在适当时机被触发
bdmh 2009-09-22
  • 打赏
  • 举报
回复
最好手动释放
starluck 2009-09-22
  • 打赏
  • 举报
回复
这里载入下指针,如果其它地方没有释放,这个就需要

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧