Boost的Serialization和SmartPoint搭配使用
准确来说,这篇博文并不是译文,而是一篇某个网页中代码改写而来。原文章中的代码存在几处严重错误,网页又不提供留言功能(不是没有而是一个没有留言功能的留言板)。4年过去了,作者对这些错误不更正让人无法接受。遂在此“翻译”之。原网址:点击打开链接
此代码是用来解决Boost库中持久化内存问题(主要是内存泄露)。代码如下:
#include <fstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <cstdlib>
#include <boost/tr1/memory.hpp>
#include <vector>
#include <map>
#include <boost/tr1/unordered_map.hpp>
#include <memory>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/split_free.hpp> using namespace std;
using namespace boost::serialization; namespace boost
{
namespace serialization
{
//implement serialization for auto_ptr<T>
//note: this must be added to the boost namesapce in order to
//be called by the libary
template< typename Archive, typename T>
inline void save( Archive &ar, const std::auto_ptr<T> &t, const unsigned int file_version)
{
//only the raw pointer has to be saved
cont T *const tx = t->get();
ar << tx;
} template <typename Archive, typename T>
inline void load(Archive &ar, std::auto_ptr<T> &t, const unsigned int file_version)
{
T *pTarget;
ar >> pTarget;
//note that the reset automagically maintains the reference count
#if BOOST_WORKAROUND (BOOST_DINKUMWARE_STDLIB, ==1)
t->release();
t = std::auto_ptr<T>(pTarget);
#else
t->reset(pTarget);
#endif
}
//split non-instrusive serialization function member into sparate
//non intrusive asve/load member functions
template<typename Archive, typename T>
inline void serialize(Archive &ar, std::auto_ptr<T> &t, const unsigned int file_version)
{
boost::serialization::split_free(ar, t, file_version);
}
}// namespace serialization
}//namespace boost //举例说明简单类型的序列化
class gps_position
{
private:
friend class boost::serialization::access; template<typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & degrees;
ar & minutes;
ar & seconds;
}
int degrees;
int minutes;
float seconds;
public:
gps_position()
{
degrees = 0;
minutes = 0;
seconds = 0.0f;
}
gps_position(int d, int m, float s):
degrees(d),minutes(m),seconds(s) { }
}; class bus_stop
{
friend class boost::serialization::access;
template <typename Archive>
void serialize( Archive &ar, const unsigned int version )
{
ar & latitude;
ar & longitude;
}
gps_position latitude;
gps_position longitude;
public:
bus_stop() { }
bus_stop(const gps_position &lat_, const gps_position &long_):
latitude(lat_), longitude(long_) { }
virtual ~bus_stop() { }
}; class bus_stop_corner:public bus_stop
{
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
// 序列化基类信息
ar & boost::serialization::base_object<bus_stop>(*this);
ar & street1;
ar & street2;
}
std::string street1;
std::string street2; public:
bus_stop_corner() { }
bus_stop_corner(const gps_position & lat_, const gps_position & long_,
const std::string & s1_, const std::string & s2_) :
bus_stop(lat_, long_), street1(s1_), street2(s2_) { } virtual std::string description() const
{
return street1 + " and " + street2;
}
~bus_stop_corner() { }
}; class bus_route
{
friend class boost::serialization::access;
//相对于BoostLearn03,此处改写是为了减少代码量并显示更清楚
boost::shared_ptr<bus_stop_corner> msptrBusStop;
boost::shared_ptr<bus_stop_corner> maptrBusStop; template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & msptrBusStop;
ar & maptrBusStop;
}
public:
bus_route(bus_stop_corner *apStop1, bus_stop_corner *apStop2)
:msptrBusStop(apStop1/*new bus_stop_corner(apStop1)*/),
maptrBusStop(apStop2/*new bus_stop_corner(apStop2)*/)
{ }
bus_route()
{ }
~bus_route()
{ }
}; int main(void)
{
std::ofstream ofs("bus_route");
//创建类实例
const gps_position latitude(1, 2, 3.3f);
const gps_position longitude(4, 5, 6.6f); //注解1:
bus_stop_corner *lpStop1 = new bus_stop_corner(latitude, longitude, "corn1", "corn2");
bus_stop_corner *lpStop2 = new bus_stop_corner(latitude, longitude, "corn3", "corn4"); //注解2:
bus_route route(lpStop1, lpStop2); {
boost::archive::text_oarchive oa(ofs);
oa << route;
} bus_route new_route;
{
std::ifstream ifs("bus_route", std::ios::binary);
boost::archive::text_iarchive ia(ifs);
ia >> new_route;
}
//当智能指针的引用计数为0时,内存会被释放.
return 0;
}
在修改原来的代码是发现一个弄不明白的地方,如进行下面的更改后便出现 “Expression:_BLOCK_TYPE_IS_VALID (pHead->nBlockUse)”错误.
1.代码中"注解①"处若换成如下两行代码
bus_stop_corner lpStop1 (latitude, longitude, "corn1", "corn2");
bus_stop_corner lpStop2 (latitude, longitude, "corn3", "corn4");
2."注解②"处换成
bus_route route(&lpStop1, &lpStop2);
3.运行时出现"“Expression:_BLOCK_TYPE_IS_VALID (pHead->nBlockUse)”错误".
为什么出现如此现象我也不能可定。猜测:
对比发现,一个是构造函数构造出来的,另一个是用new运算符分配的内存。构造函数构造对象时分配的内存由析构函数负责完成内存释放,new运算符分配的内存通过delete运算或是通过智能指针的引用计数来释放。
这个猜测还需验证,现记录在此,以后有能力证明这个猜测再来完善。 2013-11-15
Boost的Serialization和SmartPoint搭配使用的更多相关文章
- 最经常使用的两种C++序列化方案的使用心得(protobuf和boost serialization)
导读 1. 什么是序列化? 2. 为什么要序列化?优点在哪里? 3. C++对象序列化的四种方法 4. 最经常使用的两种序列化方案使用心得 正文 1. 什么是序列化? 程序猿在编写应用程序的时候往往须 ...
- 最常用的两种C++序列化方案的使用心得(protobuf和boost serialization)
导读 1. 什么是序列化? 2. 为什么要序列化?好处在哪里? 3. C++对象序列化的四种方法 4. 最常用的两种序列化方案使用心得 正文 1. 什么是序列化? 程序员在编写应用程序的时候往往需要将 ...
- boost asio 异步实现tcp通讯
---恢复内容开始--- asioboost 目录(?)[-] 一前言 二实现思路 通讯包数据结构 连接对象 连接管理器 服务器端的实现 对象串行化 一.前言 boost asio可算是一个简 ...
- Boost 序列化
原文链接: https://blog.csdn.net/qq2399431200/article/details/45621921 1. 编译器 gcc, boost 1.55 2.1第一个简单的例子 ...
- boost--序列化库serialization
序列化可以把对象转化成一个字节流存储或者传输,在需要时再回复成与原始状态一致的等价对象.C++标准没有定义这个功能.boost.serialization以库的形式提供了这个功能,非常强大,可以序列化 ...
- Boost Asioserver使用
今天主要想说道说道boost里面的网络通信库怎样设计和使用,由于近期一直在和网络一起工作,大数据处理和机器学习都离不开最后使用网络进行上线部署.先看看所有的源码吧. #include <cstd ...
- 如何在C++中使用boost库序列化自定义class ?| serialize and deserialize a class in cpp with boost
本文首发于个人博客https://kezunlin.me/post/6887a6ee/,欢迎阅读! serialize and deserialize a class in cpp Guide how ...
- Serializable unordered set
Serializable unordered set 可序列化哈希set #include <boost/algorithm/string/predicate.hpp> #include ...
- QxOrm 1.2.9 下载 以及编译方法 简介.
QxOrm 是一个基于QT开发的数据库方面的ORM库,功能很强大,是QT C++数据开发方面的好工具. 目前已经更新1.3.1 .但 不幸的是 它的官网http://www.qxorm.com/ 莫名 ...
随机推荐
- LA 3263 (平面图的欧拉定理) That Nice Euler Circuit
题意: 平面上有n个端点的一笔画,最后一个端点与第一个端点重合,即所给图案是闭合曲线.求这些线段将平面分成多少部分. 分析: 平面图中欧拉定理:设平面的顶点数.边数和面数分别为V.E和F.则 V+F- ...
- SVN备份及其还原 — dump/load方法
本文中采用最简单的dump/load方法.备份:一个较大的Subsersion版本库想用最少的空间来将它备份下来,用这个命令(请将/repo替换成你的版本库路径)svnadmin dump --del ...
- 利用dns解析来实现网站的负载均衡
当网站的访问量大了就会考虑负载均衡,这也是每一个架构师的基本功了,其基本地位就相当于相声里的说学逗唱,活好不好就看这个了 :) 传统的负载均衡思路是单点的,不管你是硬件的还是软件的基本都是这样的原理 ...
- 新浪使用Redis
新浪微博的工程师们曾经在多个公开场合都讲到过,微博平台当前在使用并维护着可能是世界上最大的Redis集群,其中最大的一个业务,单个业务使用了超过 10T 的内存,这里说的就是微博关系服务. 风起 20 ...
- linux-LINUX试题
ylbtech-doc:linux-LINUX试题 LINUX试题 1.A,LINUX试题返回顶部 01.{Linux题目}在使用匿名登录ftp时,用户名为( )? (选择1项) A) login ...
- JVM 性能调优实战之:使用阿里开源工具 TProfiler 在海量业务代码中精确定位性能代码
本文是<JVM 性能调优实战之:一次系统性能瓶颈的寻找过程> 的后续篇,该篇介绍了如何使用 JDK 自身提供的工具进行 JVM 调优将 TPS 由 2.5 提升到 20 (提升了 7 倍) ...
- C语言反转字符串
也是面腾讯的一道编程题=,= 这题比较简单 代码如下: #include <stdio.h> #include <string.h> // 非递归实现字符串反转 char *r ...
- JQuery插件的学习
此前一直想就关于Jquery插件的开发,做一个深入的学习,由于各种原因,当然主要是自己太懒了...今天就系统分析一下Jquery插件的开发(参考了http://www.xprogrammer.com/ ...
- sensor BMA250源代码执行分析
重力传感器是根据压电效应的原理来工作的. 所谓的压电效应就是 “对于不存在对称中心的异极晶体加在晶体上的外力除了使晶体发生形变以外,还将改变晶体的极化状态,在晶体内部建立电场,这种由于机械力作用使 ...
- spoj 839 Optimal Marks(二进制位,最小割)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17875 [题意] 给定一个图,图的权定义为边的两端点相抑或值的 ...