智能指针,然并卵

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

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

相关推荐
c***21298 小时前
Springboot3学习(5、Druid使用及配置)
android·学习
修炼者8 小时前
【Android 进阶】别再强转 Context 了!手把手教你优雅解耦 View 与 Activity
android·android studio
x***01068 小时前
SpringSecurity+jwt实现权限认证功能
android·前端·后端
程序员江同学9 小时前
线下活动|2025 Kotlin 中文开发者大会北京分会场
android·kotlin
李坤林10 小时前
Android Vulkan 开启VK_GOOGLE_DISPLAY_TIMING 后,一个vsync 会释放两个imageBuffer现象分析
android·vulkan
Jerry10 小时前
Compose 状态思维
android
k***459910 小时前
MySQL----case的用法
android·数据库·mysql
r***869812 小时前
Plugin ‘mysql_native_password‘ is not loaded`
android·数据库·mysql
v***598312 小时前
MySQL-mysql zip安装包配置教程
android·mysql·adb
不用89k13 小时前
Android无法区分USB摄像头是哪一个
android