请大虾看看使用TStringList来存储指针遇到的问题!

thomasmei 2003-10-17 11:25:29
使用TStringList来存储指针
初始化
TField * field = new TField(...);
TStringList * list = new TStringList;
list->AddObject("str1",field);

使用list中存储的指针没有问题,但是删除的时候
delete list->Object[ list->IndexOf("str1") ];
list->Object[ list->IndexOf(str1) ] = NULL;

在执行 delete list->Object[ list->IndexOf("str1") ] 的时候
总是会报错 EAccessViolation,

请问在这里要如何删除list中指针指向的对象
...全文
64 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
thomasmei 2003-10-17
  • 打赏
  • 举报
回复
回复人:ljianq(泉深水清)
你的方法也用过,不行.
ljianq 2003-10-17
  • 打赏
  • 举报
回复
TObject* temp=list->Object[ list->IndexOf("str1") ];
list->Object[ list->IndexOf("str1") ]=NULL;
delete temp;
temp=NULL;
destructor 2003-10-17
  • 打赏
  • 举报
回复
TStringList::Objects的帮助
the TStringList object does not own the objects in the Objects array. Objects added to the Objects array still exist even if the string list is destroyed. They must be explicitly destroyed by the application.

你把new出来的东东都delete就行了,其它的application去管吧
然后编译一下,看是否有内存泄漏?应该是没了
thomasmei 2003-10-17
  • 打赏
  • 举报
回复
不行,这个方法我已经试过,还是报一样的错,我在其他地方也这么用过,只是不是使用TField指针,而是自定义的得一个类的指针
class ttest :public TObject
{
...
}
使用ttest代替TField就毫无问题,不知道为什么。
myy 2003-10-17
  • 打赏
  • 举报
回复
最好用:

delete (TField*)(list->Object[ list->IndexOf("str1") ] );
thomasmei 2003-10-17
  • 打赏
  • 举报
回复
I see.Thanks.
wt_sanlian 2003-10-17
  • 打赏
  • 举报
回复
Do not create TField instances. TField is an abstract base class which should not be directly instantiated. The constructor for descendant classes is usually called indirectly by using the Fields editor at design time to create a persistent field component. Dataset objects also call field constructors automatically to create dynamic field components.

After calling the parent class抯 constructor, TField initializes the Visible property to true.

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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