智能指针,然并卵

cpp 复制代码
// test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string>
#include <memory>

#ifndef _SAFE_DELETE
#define _SAFE_DELETE(x) \
	{ \
	if( (x) ) { \
	delete (x); \
	(x) = NULL; \
	} \
	}
#endif

class mysql_result
{
public:
	mysql_result(){
		m_nRows = 0;
	}
	~mysql_result(){
		std::cout << "~mysql_result()\n";
	}
	int m_nRows;		// 结果集行数
};

mysql_result *ptest;

void test()
{
	mysql_result *result = new mysql_result();
	std::shared_ptr<mysql_result> p(result);
	_SAFE_DELETE(result);
}

int main()
{
	
	//mysql_result *result = new mysql_result();
	//mysql_result *test = result;
	//_SAFE_DELETE(result);
	test();
	system("pause");
    std::cout << "Hello World!\n";
}

两个指针指向同一个对象,释放其中一个指针,另外一个就会野指针。当你想引入智能指针解决问题,然并卵,智能指针也是野指针。

相关推荐
2501_9159214327 分钟前
如何在苹果手机上面进行抓包?iOS代理抓包,数据流抓包
android·ios·智能手机·小程序·uni-app·iphone·webview
_李小白1 小时前
【Android 美颜相机】第五天:GPUImageFilterTools
android·数码相机
冬奇Lab1 小时前
【Kotlin系列05】集合框架:从Java的冗长到函数式编程的优雅
android·kotlin·编程语言
冬奇Lab1 小时前
稳定性性能系列之十四——电量与网络优化:Battery Historian与弱网处理实战
android·性能优化·debug
Coffeeee1 小时前
了解一下Android16更新事项,拿捏下一波适配
android·前端·google
用户41659673693551 小时前
深入解析安卓 ELF 16KB 页对齐:原生编译与脚本修复的权衡
android
恋猫de小郭1 小时前
Compose Multiplatform 1.10 Interop views 新特性:Overlay 和 Autosizing
android·flutter·macos·kotlin·github·objective-c·cocoa
胖虎12 小时前
Android 文件下载实践:基于 OkHttp 的完整实现与思考
android·okhttp·下载文件·安卓下载·安卓中的下载
_李小白2 小时前
【Android 美颜相机】第四天:CameraLoader、Camera1Loader 与 Camera2Loader
android·数码相机
00后程序员张2 小时前
iOS APP 性能测试工具,监控CPU,实时日志输出
android·ios·小程序·https·uni-app·iphone·webview