如果在回调函数中释放对象,会怎么样?
来源:互联网 发布:商业价值杂志淘宝店 编辑:程序博客网 时间:2024/04/30 05:16
问题的场景:
先new一个对象p,假设对象里会执行到一个回调函数,如果在此回调函数里delete p,再继续执行代码;结果会怎么样?
可能结果:
1.对象已经被释放,this指针指向的一块无效的内存,那么继续执行this的成员函数或者其他的成员变量,应该会崩溃;
2.执行后面的代码,虽然内存已经访问非法,但是内存其实是可用的,或者叫做强行可用的,还可以继续执行,但是会有重新被分配出去,被其他线程修改引发的不可预期的行为;
测试的代码:
#include <stdio.h>#include <stdlib.h>typedef void (* PFUNC)(void*p,int a) ;class Student;class Node{public: int a; int b;};class Student{public: Student() { a=11; b=22; c=33; n=NULL; printf("create:%p\n",this); } ~Student() { printf("close:%p\n",this); b=222; c=333; } void test() { n=new Node(); n->a=1; //n->b=2; //c=55; printf("test()_1:%p->a:%lld b:%lld c:%lld p:%p\n",this,a,b,c,n); //pf(this,11); printf("test()_2:%p->a:%lld b:%lld c:%lld p:%p\n",this,a,b,c,n); test2(); } void test2() { //printf("test2():%p->%d %d %p %d\n",this,a,b,n,c); printf("test2():%p->%p %d\n",this,n,n->a); } Node* n; long long a; long long b; long long c; PFUNC pf; //Node* n; //int c;};void func(void* p,int a){ printf("func %d\n",a); Student* p1=(Student*)p; delete p1;}int main(){ Student* p = new Student(); p->pf=func; p->test(); printf("--\n"); //delete p;// delete p; //p->test(); return 0;}
测试步骤与运行结果:
1.
Student*p=new Student();
delete p;
delete p;
重复释放会崩溃;
2.
Student*p=new Student();
delete p;
p->test()程序可以运行成功;
但是这样做的风险在于:p指向的内存会被其他线程分配得到,于是出现不可预期的错误;
delete p之后会把p指向的地址起始的8字节置为0,假如头8字节是一个指针,程序立即就会崩溃;
引申阅读:
http://blog.csdn.net/passion_wu128/article/details/38966581 C++new和delete实现原理
阅读全文
0 0
- 如果在回调函数中释放对象,会怎么样?
- 如果codefish架构在xml上会怎么样 ?
- 在Java中实现回调函数
- 在 SQLite3 中使用回调函数
- 在 SQLite3 中使用回调函数
- 在 SQLite3 中使用回调函数
- 在 SQLite3 中使用回调函数
- 你会使用回调函数吗?
- 你会使用回调函数吗?
- 自定义函数或者回调函数中调用对话框对象
- 回调函数中JSON对象的解析方式
- jQuery中$(this)在回调函数中无效
- 在C#中使用API回调函数的方法
- Thunk::在类中封装回调函数
- 请不要在回调函数中盖房子
- 在signal 回调函数中继续 signal
- 在回调函数中调用vcl事件
- 如何在类中封装回调函数?
- Linux性能调优之内存调优
- 浅谈策略类型游戏
- TextInputLayout+EditText在注册界面的使用(密码的的隐藏和可见,imeOptions的使用)
- mybatis. <sql>元素重用
- js格式化日期,漂亮
- 如果在回调函数中释放对象,会怎么样?
- Educational Codeforces Round 21
- sql优化随记
- opencv3.2.0及contrib组件在Ubuntu 16.04下的安装
- bootstrap 导航条
- 归并排序
- 获取项目接口的当前路径
- mysql保留字段小数点后两位小数
- BlockingQueue