智能指针,然并卵

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";
}

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

相关推荐
牛哇网络工作室4 小时前
UnityHDRP写实数字人全流程基础5—语音输入和语音识别
android·unity·c#·游戏引擎·aigc·语音识别·xcode
一笑的小酒馆4 小时前
Androidiot蓝牙配网简单封装
android
愚公搬代码7 小时前
【愚公系列】《Android应用案例开发大全》016-LBS类应用掌上杭州(辅助工具类的开发)
android·前端
g10565591398 小时前
LAMP博客平台Wordpress实战
android·mysql·nginx·php
hunterandroid9 小时前
[Android 从零到一] Compose LazyColumn 性能优化:key、稳定性与重组治理
android·前端
pengyu9 小时前
【Kotlin 协程修仙录 · 元婴境 · 中阶】 | 操作符真解:Flow 中间操作符与数据流变幻术
android·kotlin
pengyu10 小时前
【Kotlin 协程修仙录 · 元婴境 · 初阶】 | 冷流初啼:Flow 的基础与响应式编程的入门
android·kotlin
wawo0011 小时前
Kuikly实践-从Android compose迁移到kuikly compose
android
Carson带你学Android12 小时前
Android 版 MCP 正式登场:AppFunctions
android·ai编程·jetbrains
古法安卓15 小时前
Android-Direct Boot 阶段与 getFilesDir() 路径变更问题深度分析
android·java·android studio