- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这是一段我用来在共享内存上分配映射的代码,我正在使用boost::interprocess和托管共享内存段,现在的问题是我遇到了内存泄漏。下面给出的是最高输出。
最高输出:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1. 27594 tpmon 20 0 46132 2140 1664 S 0.0 0.0 0:00.00 test_stub
2. 27594 tpmon 20 0 46132 2176 1664 S 0.0 0.0 0:00.01 test_stub
3. 27594 tpmon 20 0 46264 2248 1664 S 0.0 0.0 0:00.01 test_stub
4. 27594 tpmon 20 0 46264 2280 1664 S 0.0 0.0 0:00.01 test_stub
sudo -u tpmon valgrind --tool=memcheck --leak-check=yes ./bin/test_stub
And below is the output:
==21404== Memcheck, a memory error detector
==21404== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==21404== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==21404== Command: ./bin/test_stub
==21404==
Finished initializing TickerInfo Manager
^C==21404==
==21404== HEAP SUMMARY:
==21404== in use at exit: 60,627 bytes in 1,264 blocks
==21404== total heap usage: **5,059 allocs, 3,795 frees**, 812,123 bytes allocated
==21404==
==21404== 29 bytes in 1 blocks are possibly lost in loss record 2 of 7
==21404== at 0x4A075BC: operator new(unsigned long) (vg_replace_malloc.c:298)
==21404== by 0x3A7149C3C8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.13)
==21404== by 0x3A7149CDE4: ??? (in /usr/lib64/libstdc++.so.6.0.13)
==21404== by 0x3A7149CF32: std::basic_string<char, std::char_traits<char>, > std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.13)
==21404== by 0x40986F: main (test_stub.cxx:12)
#include <stdio.h>
#include <iostream>
#include<string>
#include <sstream>
using namespace std;
int main() {
TickerInfoManager * ticker_info_manager_inst = TickerInfoManager::get_instance("test");
char_allocator ca(ticker_info_manager_inst->get_managed_memory_segment().get_allocator<char>());
while(1) {
for( int i=0; i < 30; i++ ) {
basic_time now;
stringstream convert;
convert << i;
int curr_time = now.fullTime();
ticker_info_manager_inst->put_records( *(new tickerUpdateInfo(const_cast<char*>(("Deb"+convert.str()).c_str()), i, curr_time, ca ) ));
}
sleep(1);
}
//ticker_info_manager_inst->print_contents();
return 0;
}
#include <TickerInfoManager.h>
#include <TickerInfoMangerImplementation.h>
TickerInfoManager::TickerInfoManager( const sharedMemoryNameT & name) : pInfoMangerImpl( new tickerInfoMangerImplementation( name )) {
}
TickerInfoManager* TickerInfoManager::get_instance( const sharedMemoryNameT & name ) {
return (new TickerInfoManager( name ) );
}
bool TickerInfoManager::put_records( const tickerUpdateInfoT & record ) {
return pInfoMangerImpl->put_records( record );
}
void TickerInfoManager::print_contents() {
return pInfoMangerImpl->print_contents();
}
bip::managed_shared_memory& TickerInfoManager::get_managed_memory_segment() {
return pInfoMangerImpl->get_managed_memory_segment();
}
#include <TickerInfoMangerImplementation.h>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <iostream>
#include "basic_time.h"
using namespace boost::interprocess;
tickerInfoMangerImplementation::tickerInfoMangerImplementation( const sharedMemoryNameT & name ): m_name(name),
m_managed_memory_segment( open_or_create, "test", 1000000 ),
p_ticker_info_map( m_managed_memory_segment.find_or_construct<KeyTickerCountMap>("TickerInfoMap")(std::less<SharedString>(), m_managed_memory_segment.get_segment_manager() ) ) {
std::cout<<"Finished initializing TickerInfo Manager" << std::endl;
}
bool tickerInfoMangerImplementation::put_records( const tickerUpdateInfoT & record ) {
//If the key has not been inserted, insert the key and update the map
KeyTickerCountMap::iterator iterator_to_map = p_ticker_info_map->find( record.m_id );
if( iterator_to_map == p_ticker_info_map->end() ) {
p_ticker_info_map->emplace( record.m_id, std::make_pair( record.m_total_ticker_count, record.m_active_ticker_count ) );
}
else {
p_ticker_info_map->at(record.m_id) = std::make_pair( record.m_total_ticker_count, record.m_active_ticker_count) ;
}
//record.m_ca.deallocate( const_cast<char*> ((record.m_id).c_str()), record.m_id.length() );
return true;
}
int tickerInfoMangerImplementation::calculate_historical_time_using_threshold( const thresholdT seconds ) {
basic_time::Secs_t secs( seconds );
basic_time tick_time;
tick_time -= secs;
return ( tick_time.fullTime() );
}
void tickerInfoMangerImplementation::print_contents() {
KeyTickerCountMap::iterator map_iter = (*p_ticker_info_map).begin();
KeyTickerCountMap::iterator map_end = (*p_ticker_info_map).end();
for ( ; map_iter != map_end; ++map_iter ) {
std::cout<< map_iter->first << " " << map_iter->second.first << " " << map_iter->second.second << std::endl;
}
}
#ifndef __TICKER_INFO__
#define __TICKER_INFO__
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <iostream>
typedef boost::interprocess::managed_shared_memory::allocator<char>::type char_allocator;
typedef boost::interprocess::basic_string<char, std::char_traits<char>, char_allocator> shm_string;
//Data to insert in shared memory
typedef struct tickerUpdateInfo {
shm_string m_id;
int m_total_ticker_count;
int m_active_ticker_count;
char_allocator m_ca;
tickerUpdateInfo( char * id,
int total_ticker_count,
int active_ticker_count,
const char_allocator &a )
: m_id( id, a),
m_total_ticker_count(total_ticker_count),
m_active_ticker_count(active_ticker_count),
m_ca(a) {
}
~tickerUpdateInfo() {
std::cout<< "Calling destructor" <<std::endl;
}
tickerUpdateInfo& operator=(const tickerUpdateInfo& other) {
if (this != &other) {
m_total_ticker_count = other.m_total_ticker_count;
m_active_ticker_count = other.m_active_ticker_count;
}
return *this;
}
} tickerUpdateInfoT;
#endif
**TickerInfoManager.h**
#ifndef __TICKER_INFO_MANAGER__
#define __TICKER_INFO_MANAGER__
#include <TickerInfoManagerConstants.h>
#include <TickerInfoMangerImplementation.h>
//class tickerInfoMangerImplementation;
class TickerInfoManager {
public:
static TickerInfoManager* get_instance( const sharedMemoryNameT & name );
bool put_records( const tickerUpdateInfoT & record );
TickerInfoManager( const sharedMemoryNameT & name);
void print_contents();
boost::interprocess::managed_shared_memory& get_managed_memory_segment();
private:
std::auto_ptr<tickerInfoMangerImplementation> pInfoMangerImpl;
};
#endif
#ifndef __TICKER_INFO_MANAGER_IMPL__
#define __TICKER_INFO_MANAGER_IMPL__
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/containers/map.hpp>
//#include <TickerInfoManagerConstants.h>
#include <TickerInfo.h>
#include <vector>
#include <fire/HashMap.h>
#include <string>
typedef std::string sharedMemoryNameT;
typedef int thresholdT;
namespace bip = boost::interprocess;
//the strings also need to be assigned from the shared memory
typedef bip::allocator<void, bip::managed_shared_memory::segment_manager> VoidAllocator;
typedef bip::allocator<char, bip::managed_shared_memory::segment_manager> CharAllocator;
typedef bip::basic_string<char, std::char_traits<char>, CharAllocator> SharedString;
//Note that map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>,
//so the allocator must allocate that pair.
typedef bip::allocator<std::pair<const SharedString, std::pair<int,int> >, bip::managed_shared_memory::segment_manager> MapValueTypeAllocator;
typedef bip::map<SharedString, std::pair<int,int>, std::less<SharedString>, MapValueTypeAllocator> KeyTickerCountMap;
//allocator for the string
typedef bip::allocator<SharedString, bip::managed_shared_memory::segment_manager> StringAllocator;
class tickerInfoMangerImplementation {
public:
tickerInfoMangerImplementation( const sharedMemoryNameT & name );
bool put_records( const tickerUpdateInfoT & record );
void print_contents();
bip::managed_shared_memory& get_managed_memory_segment() {
return m_managed_memory_segment;
}
private:
const sharedMemoryNameT m_name;
bip::managed_shared_memory m_managed_memory_segment;
bip::offset_ptr<KeyTickerCountMap> p_ticker_info_map;
int calculate_historical_time_using_threshold( const thresholdT seconds );
};
#endif
最佳答案
Warning: ranting ahead. I do get constructive and provide a constructive fixed version at the end.
*new
中使用内存泄漏运算符(main()
)¹:*(new tickerUpdateInfo(const_cast<char *>(("Deb" + convert.str()).c_str()), i, curr_time, ca)));
const_cast
。只需已接受std::string
即可,不要邪恶。 tickerUpdateInfo::operator=
通过不分配id(和分配器)而违反了值语义。 (不使用该运算符)tickerUpdateInfo
类的目的,只是要确保id
字符串的分配和分配超出了必要的范围(哦,当然要泄漏,请参见第一个项目符号)。 typedef struct X {...} X_t;
是一种C-ism。无需在C++ tickerUpdateInfo
不需要保留分配器(它是字符串的属性)。 TickerUpdateManager
具有get_instance
。那么,是单例吗?哦,不,不是因为每次调用它都会返回一个新实例。哦,经理也被泄露了。 get_segment_manager
)。那是一个界面设计的禁忌和违反《法则》。TicketUpdateManagerImplementation
的构造不能保护共享内存或带锁的TickerInfoMap
的构造。 auto_ptr
已弃用。使用std::unique_ptr
(或必须使用boost::scoped_ptr
)。当然,这是假设您甚至在那儿需要pimpl。 m_name
(甚至不用于打开共享内存段),而sharedMemoryNameT
却是蛇油。 std::less<SharedString>
是已经存在的默认比较器(*p_ticker_info_map)[record.m_id] = { record.m_total_ticker_count, record.m_active_ticker_count };
true
返回值? calculate_historical_time_using_threshold
是一个有趣的名称。该函数不属于此类,并且不计算历史时间。哦,它不使用任何阈值。 (AFAICT它将返回全时seconds
之前的代码。)shm_string
与SharedString
(及其各自的分配器)的typedef进行竞争。 #pragma once
#include <string>
#include <memory>
class TickerInfoManager {
public:
TickerInfoManager(const char* name);
~TickerInfoManager();
void put(std::string const& id, int total, int active);
void print_contents() const;
private:
struct Impl;
std::unique_ptr<Impl> pimpl_;
};
#include <iostream>
#include <string>
#include "TickerInfoManager.hxx"
#include "basic_time.h"
#include <thread>
int main() {
TickerInfoManager tickerinfo("test");
while (1) {
for (int i = 0; i < 30; i++) {
tickerinfo.put("Deb" + std::to_string(i), i, basic_time().fullTime());
}
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // sleep(1)
std::cout << "." << std::flush;
}
}
#include "TickerInfoManager.hxx"
#include <iostream>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/thread/lock_guard.hpp>
namespace /*anon*/ {
namespace bip = boost::interprocess;
typedef bip::allocator<char, bip::managed_shared_memory::segment_manager> CharAllocator;
typedef bip::basic_string<char, std::char_traits<char>, CharAllocator> SharedString;
typedef bip::allocator<std::pair<SharedString const, std::pair<int, int> >, bip::managed_shared_memory::segment_manager> MapValueTypeAllocator;
typedef bip::map<SharedString, std::pair<int, int>, std::less<SharedString>, MapValueTypeAllocator> KeyTickerCountMap;
typedef boost::lock_guard<bip::interprocess_mutex> lock_guard;
struct LockableMap {
LockableMap(MapValueTypeAllocator alloc) : map(alloc) {}
KeyTickerCountMap map;
bip::interprocess_mutex mutex;
};
}
struct TickerInfoManager::Impl {
Impl(const char* name)
: m_segment(bip::open_or_create, name, 1000000),
m_alloc(m_segment.get_segment_manager()),
p_data(m_segment.find_or_construct<LockableMap>("TickerInfoMap")(m_segment.get_segment_manager()))
{}
bip::managed_shared_memory m_segment; // order is relevant
CharAllocator m_alloc;
bip::offset_ptr<LockableMap> p_data;
KeyTickerCountMap& map() { return p_data->map; }
bip::interprocess_mutex& mutex() { return p_data->mutex; }
};
TickerInfoManager::TickerInfoManager(const char* name) : pimpl_(new Impl(name)) { }
TickerInfoManager::~TickerInfoManager() { }
void TickerInfoManager::put(std::string const& id, int total, int active) {
SharedString shid(id.begin(), id.end(), pimpl_->m_alloc);
lock_guard lock(pimpl_->mutex());
pimpl_->map()[shid] = { total, active };
}
void TickerInfoManager::print_contents() const {
lock_guard lock(pimpl_->mutex());
for (auto const& e : pimpl_->map())
std::cout << e.first << " " << e.second.first << " " << e.second.second << std::endl;
}
关于c++ - boost::interprocess 和valgrind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26661835/
有没有人尝试创建进程间通信的日志文件?有人可以就实现这一目标的最佳方式给我一些建议吗? 最佳答案 这个问题不太清楚,评论使它不太清楚,但无论如何...... 首先要尝试的两件事是 ipcs和 stra
我使用boost::interprocess在共享内存中创建了boost::multi_index数据结构。有许多客户端进程将访问此数据结构。访问时,我将锁定数据结构。我遇到的问题是,一旦客户端进程正
我正在尝试使用 this 创建内存映射文件回答,但我收到编译错误。这是我的代码: namespace bi = boost::interprocess; std::string vecFile = "
我正在编写实时数据以增加共享内存。最初我每次想访问 shm 时都使用它: boost::interprocess::managed_shared_memory segment(boost::inte
我正在查看两个进程共享互斥锁和条件变量的 Boost 示例代码: https://www.boost.org/doc/libs/1_57_0/doc/html/interprocess/synchro
我刚刚读了this page Boost.Interprocess 文档。这似乎表明,为了适应不同操作系统之间的差异并达成某种共识,某些进程间机制并没有使用操作系统提供的直接对应的本地机制来实现,而是
我有以下崩溃的代码。我怀疑这是因为分配了对堆栈的引用,所以我遇到了这个问题。但我想避免每次都必须堆栈分配互斥锁和作用域锁的成本 class Cache { public: void cr
在 Boost.Interprocess 文档中 Where is this being allocated?据称 Boost.Interprocess 容器同时使用两种机制放置在共享内存中: Boo
我已经在这个问题上待了好几天(甚至在 boost 论坛上的 posted)并且能够让第二个进程识别锁定的互斥锁似乎不起作用。请帮忙。这是代码: 通用头文件:SharedObject.hpp #ifnd
我正在使用 boost::interprocess::named_upgradable_mutex 来同步一些进程。 我正在使用 boost::interprocess::sharable_lock
我搜索在两个进程之间共享一个结构。但我没有成功。你能帮忙理解吗? 这是我的第一个过程的代码: #include #include #include #include #include #in
我正在移植源代码以打开/读取/写入在多个进程之间共享的文件。它在 Windows 下运行良好,因为它主要使用 boost::interprocess (1.44),我没想到会有太多问题,但我发现了一些
我想我终于掌握了 boost:interprocess 库的基础知识,并且在处理包含一些全是标准数据类型的成员变量的相对简单的类时,我一直在成功地使用它。 但是,我现在面临着将一个相当复杂的类推送到进
现在查看此链接: http://www.boost.org/doc/libs/1_56_0/doc/html/interprocess/quick_guide.html#interprocess.qu
我想用 boost 编写一个简单的应用程序,将字符串对象传递给其他进程。它编译得很好,但是当我尝试从第二个进程打印出字符串时,以下消息被发送到控制台并且第二个进程崩溃: ../boost_1_44_0
目前,我有 2 个进程使用 message_queue 和 shared_memory 表单 boost 进行通信。一切如常。 现在我需要使这个进程中的一个成为多线程的(再次感谢 boost),我想知
我需要围绕一个硬件进行进程间同步。因为此代码需要在 Windows 和 Linux 上运行,所以我使用 Boost 进程间互斥锁进行封装。一切正常接受我检查互斥量放弃的方法。这有可能发生,所以我必须为
这是一段我用来在共享内存上分配映射的代码,我正在使用boost::interprocess和托管共享内存段,现在的问题是我遇到了内存泄漏。下面给出的是最高输出。 最高输出: PID USER
我有一个应用程序实现了 boost named_mutex 以锁定 C++ 项目 (Visual Studio) 中的多个模块。我需要不惜一切代价删除所有 boost 依赖项。 还有其他方法可以实现吗
我似乎遇到了 boost::interprocess::file_lock 的问题 我的流程 1 本质上是 boost::interprocess::file_lock test_lock("
我是一名优秀的程序员,十分优秀!