如何判断一个指向的对象是否被释放

珠海笨笨 2010-03-01 11:59:05
如下:

TButton* A = new TButton(this);
TButton* B = A;

delete A;

if (B == NULL)
{
ShowMessage("Null");
}
else
{
ShowMessage("Not Null");
}


此时,B不等于NULL,程序执行 ShowMessage("Not Null"), 请问如何判断B指向的对象是否被释放,让程序执行ShowMessage("Null");

...全文
1558 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
gucunlin 2010-03-31
  • 打赏
  • 举报
回复
delete A;
A = NULL;

这不就行了
勉励前行 2010-03-28
  • 打赏
  • 举报
回复
C++的機制,是無法判斷指針是否已經被析構的,需要程序員記住哪個指針被析構了,然後也查要程序員自己不要使用已經析構了的指針。

編譯可以發出以下警告:使用未初始化的指針;指針賦值後從未使用;等問題,至於更深一層的幫助,需要以後更智能化的編譯器來幫助我們了。

先释放,然后=NULL , 並非好習慣,而是一個壞習慣:很多情況下是純粹多余的賦值行為。有好習慣的程序員,不會使用已經析構了的指針。好的習慣是,在哪個模塊new ,就在哪個模塊delete,其余模塊只是使用。如果需要其他模塊負責delete的,必須在手冊中強調,或者在函數名中強調,而且還得盡量避免這種情況。

可以有很多個指針指向同一內存,只將一個指針置為NULL,對程序的強壯性影響極微極微。
lhy 2010-03-28
  • 打赏
  • 举报
回复
先释放,然后=NULL。形成习惯就好了
xiaohutu07070045 2010-03-27
  • 打赏
  • 举报
回复
我也有个问题 各位大侠谁能帮帮忙莫我有个很棘手的问题啊,请各位帮帮忙!#include<iostream>
using namespace std;
class intsllnode
{
public:
int c,e;
intsllnode *next;
public:
intsllnode(int a,int b,intsllnode*ptr)
{
c=a;
e=b;
next=ptr;
}
intsllnode(){}
};
class intsllist
{
private:
intsllnode*head,*tail,*f1,*f2,*f3;//f1,f2代表的是两个要相加的多项式,f3代表的是结果函数
//intsllist*s;
void paixu(intsllnode*ptr);//根据x的指数从小到大排序
public:
intsllist(){}
void creat();
void intserttof1(int a,int b);//插入数到f1;
void intserttof2(int a,int b);//插入数到f2;
void addtof3();//进行相加得到f3;
void show();
};
void intsllist::paixu(intsllnode*ptr)//ptr只的是某个函数如f1,f2
{
intsllnode*n,*m;
n=ptr->next;
m=n->next;
while(n!=NULL)
{
while(n->e>m->e)
{
intsllnode t;
t=*n;
*n=*m;
*m=t;
m=m->next;
}
n=n->next;
}
}
void intsllist::creat()
{
intsllist *s;
s=new intsllist();
}
void intsllist::intserttof1(int a,int b)
{
intsllnode*j;//*p,
//p=new intsllnode();
f1=new intsllnode();
//s=new intsllist();
//s->f1=p;//p设为链表的头节点
j=new intsllnode();
j->c=a;
j->e=b;
if(head=tail=0)
head=tail=j;
else
{
f1->next=j;
//p->next=j;
j->next=head;
head=j;
}
}
void intsllist::intserttof2(int a,int b)
{
//s=new intsllist();
intsllnode*j;//*q,
f2=new intsllnode();
//q=new intsllnode();
//s->f2=q;//p设为链表的头节点
j=new intsllnode();
j->c=a;
j->e=b;
//q->next=j;
f2->next=j;
j->next=head;
head=j;
}
void intsllist::addtof3()
{
intsllnode*x,*y;//*l;
int a;
//s=new intsllist();
//l=new intsllnode();
//s->f3=l;
f3=new intsllnode();
x=f1->next;
y=f2->next;
while(x!=NULL&&y!=NULL)
{
if(x->e==y->e)
f3->c=x->c+y->c;
else if(x->e>y->e)
{
while(x->e!=y->e&&x->e>y->e)
{
y=y->next;
f3->c=y->c;
f3=f3->next;
}
if(x->e==y->e)
f3->c=x->c+y->c;
else
f3->c=x->c;
}
else if(x->e<y->e)
{
while(x->e!=y->e&&x->e<y->e)
{
x=x->next;
f3->c=x->c;
f3=f3->next;
}
if(x->e==y->e)
f3->c=x->c+y->c;
else
f3->c=y->c;
}
x=x->next;
y=y->next;
}
}
void intsllist::show()
{
intsllnode*i;
i=f3;
cout<<"所有结果为";
while(i!=NULL)
{
if(i->c==0)
delete i;
else
cout<<i->c<<i->e;
i=i->next;
}
}
int main()
{
intsllist *k;int a,b;
k->creat();
cout<<"输入f1的x的系数和指数"<<endl;
cin>>a>>b;
cout<<"ok"<<endl;
k->intserttof1(a,b);
k->show();
cout<<"输入f2的x的系数和指数"<<endl;
cin>>a>>b;
k->intserttof2(a,b);
k->show();
k->addtof3();
k->show();
system("pause");
return 0;
}
总是会有问题
xiaohutu07070045 2010-03-27
  • 打赏
  • 举报
回复
啊 发现指针问题真的挺复杂的!
lirg8405 2010-03-26
  • 打赏
  • 举报
回复
delete A;
之后,A指向的资源已经释放,其他程序就可以A原来占有的资源,这个时候A不是没指向,而是继续指向原来的内存,当没有其他程序使用该内存的时候,不变;但是有其他在使用就会得到不确定值;需要不能引用的A,但是A不是空的,必须人为的指向为空。
就像你上厕所,锁着门,别人都用不到,你不用了开锁,别人可以用,但是你不走出来的话,你不是在里面的。。。。
cptang 2010-03-26
  • 打赏
  • 举报
回复
mark
xxgclj 2010-03-09
  • 打赏
  • 举报
回复
引用 6 楼 regersubadm 的回复:
如果指针在多个地方使用,比较好的习惯是在delete后将对象指针赋值为NULL.


此方法较好。
jackleeno 2010-03-02
  • 打赏
  • 举报
回复
好象搞复杂了,只要判断(B)就可以了:

TButton* A = new TButton(this);
TButton* B = A;

delete A;

if (B)
{
ShowMessage("Null");
}
else
{
ShowMessage("Not Null");
}
珠海笨笨 2010-03-01
  • 打赏
  • 举报
回复
还是不行,以下代码还是无法判断

TButton* A = new TButton(this);
TButton* B = A;
A->Free();
if (B == NULL)
{
ShowMessage("Null");
}
else
{
ShowMessage("Not Null");
}
likeyrain 2010-03-01
  • 打赏
  • 举报
回复
试试 Free 一下
aozhi 2010-03-01
  • 打赏
  • 举报
回复
delete 之后,一定要把所有指向这片内存的指针置NULL,可别出野指针呀。
周药师 2010-03-01
  • 打赏
  • 举报
回复
引用 11 楼 silverpot 的回复:
引用 10 楼 zhouzhangkui 的回复:调用 delete A 是将A指针所指向的内存中的资源释放掉 , 指针A仍然存在,这时A指针还是指向着该内存地址 所以加一句 A = NULL; 可以避免一些内存错误,
即使加了A = NULL;
delete掉A后,B的值还是不变,指向先前A的地址空间。较好的解决办法是使用指针引用,而不是复制指针。

说到一边去了

TButton* A = new TButton(this);
TButton* B = A;
delete A;
if(B == NULL)
{
ShowMessage("Null");
}
else
{
ShowMessage("Not Null");
}
lz的这段代码里 给B指针赋值后 就没有对B指针进行任何操作 所以B不会变
后面说的
delete A;
A =NULL ;//加这句 就像我说的是对于A进行释放、置空

对你后面说的指针引用B,指针引用B所引用的地址A为NULL了
结果就是B =BULL 了
xinxin26 2010-03-01
  • 打赏
  • 举报
回复
路过,进来看看,学习。。。。。。。。。。。。。。。。。。。!
sczyq 2010-03-01
  • 打赏
  • 举报
回复
1、通过组件查找, 组件的 Owner 必须是当前类
bool Exists = false;
for (int i = 0; i < ComponentCount; i++)
if (Components[i] == B)
Exists = true;

2、通过控件查找, 下例的控件的 Parent 是当前表单,在知道放在什么面板或页面等内时,请在该父控件下找,不知道放在哪里,可以采用枚举方式。

bool Exists = false;
for (int i = 0; i < ControlCount; i++)
if (Control[i] == B)
Exists = true;


3、很另类吧通过容错机制,但也值得一试。
//--------------------

if (B)
try
{
if (B->Visible);
}
catch ( EAccessViolation &E)
{
B = NULL;
}

ShowMessage( B ? "还在呢!" : "已经释放!");

银点 2010-03-01
  • 打赏
  • 举报
回复
引用 10 楼 zhouzhangkui 的回复:
调用 delete A
是将A指针所指向的内存中的资源释放掉 ,
指针A仍然存在,这时A指针还是指向着该内存地址
所以加一句 A = NULL;
可以避免一些内存错误,

即使加了A = NULL;
delete掉A后,B的值还是不变,指向先前A的地址空间。较好的解决办法是使用指针引用,而不是复制指针。
周药师 2010-03-01
  • 打赏
  • 举报
回复
调用 delete A
是将A指针所指向的内存中的资源释放掉 ,
指针A仍然存在,这时A指针还是指向着该内存地址
所以加一句 A = NULL;
可以避免一些内存错误,

银点 2010-03-01
  • 打赏
  • 举报
回复
这是一个很值得挖掘的问题,在指针、内存及指针引用的使用方面非常具有教学研究意义

可惜啊
银点 2010-03-01
  • 打赏
  • 举报
回复
使用指针引用,而不是指针可解决你的问题。就用你的代码做为例子说明:

TButton* A = new TButton(this);
TButton*& B = A;
delete A;
A = NULL;

if (B == NULL)
{
ShowMessage("Null");
}
else
{
ShowMessage("Not Null");
}
珠海笨笨 2010-03-01
  • 打赏
  • 举报
回复
找到方法了,自己写一个函数

bool ComponentExists(TComponent *Sender)
{
try
{
Sender->HasParent();
return true;
}
catch(...)
{
return false;
}
}


TButton* A = new TButton(this);
TButton* B = A;
A->Free();

if (!ComponentExists(B))
{
ShowMessage("Null");
}
else
{
ShowMessage("Not Null");
}


这样就可以解决问题了

加载更多回复(4)

13,873

社区成员

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

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