我无法清除 map 内存(我通过 Valgrind 检查过)。
#include <map>
class testMap {
public:
testMap(){}
~testMap();
void insert_map(int, int);
private:
std::map<int,int> _map;
};
void testMap::insert_map(int i, int j){
_map.insert( pair<int, int>(i,j));
}
我尝试了 _map.clear()、erase()、手动删除了 _map->second,但仍然没有成功。
感谢所有回复。实际上 map 本身不是问题,但是 map 和单例会导致泄漏。下面的代码有什么问题?
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include "Object.h"
#include<boost/smart_ptr.hpp>
using namespace std;
class Singleton {
public:
// A wrapper around Object class
class object
{
public:
object() : _object(new Object())
{}
Object get(void)
{ return _object.get(); }
private:
boost::shared_ptr<Object> _object;
};
object insert_new(const std::string key)
{
_object_maps.insert( pair<string,object>( key, object() ));
return _object_maps.find( key )->second;
//_test_object = object();
//return _test_object; // Leak goes away if I don't use map.
}
static Singleton* Instance();
void Print();
protected:
Singleton(){}
~Singleton();
private:
static Singleton* _instance;
std::map<std::string, object > _object_maps;
object _test_object;
};
Singleton* Singleton::_instance = 0;
Singleton* Singleton::Instance() {
if( _instance ==0 )
{
_instance = new Singleton();
}
return _instance;
}
void Singleton::Print() {
std::cout << " Hi I am a singleton object" << std::endl;
}
Singleton::~Singleton()
{
_object_maps.clear();
}
来 self 调用的另一个代码
Singleton::object _test_object(Singleton::Instance()->insert_new("TEST"));
有问题吗?我收到 Valgrind 错误,例如
==19584== 17 bytes in 1 blocks are possibly lost in loss record 31,429 of 52,291
==19584== at 0x69A1642: operator new(unsigned int) (vg_replace_malloc.c:255)
==19584== by 0x772CB0A: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.8)
==19584== by 0x772D904: ??? (in /usr/lib/libstdc++.so.6.0.8)
==19584== by 0x772DB16: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.8)
==19584== by 0xBF1BC17: test::test() (test.C:34)
==19584== by 0xBF1DB66: G__testDict_143_0_1(G__value*, char const*, G__param*, int) (testDict.C:190)
==19584== by 0x70EA4E5: Cint::G__ExceptionWrapper(int (*)(G__value*, char const*, G__param*, int), G__value*, char*, G__param*, int) (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584== by 0x71EF2E4: G__call_cppfunc (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584== by 0x71C0095: G__interpret_func (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584== by 0x71AF883: G__getfunction (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584== by 0x71D8CC1: G__new_operator (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584== by 0x718D07F: G__getexpr (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584== by 0x717724E: G__define_var (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584== by 0x71FDEC6: G__defined_type (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584== by 0x7201A6D: G__exec_statement (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584== by 0x71BF6C8: G__interpret_func (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584== by 0x71AF62F: G__getfunction (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584== by 0x718437D: G__getitem (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584== by 0x7189F12: G__getexpr (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
==19584== by 0x719713F: G__calc_internal (in /afs/rhic.bnl.gov/@sys/opt/phenix/root-5.17.01/lib/libCint.so)
最佳答案
简答:
您明确声明但未定义析构函数(忘记 {})。
长答案:
{},pair 前面缺少 std::。使用 main 更正并完成:
#include <map>
class testMap {
public:
testMap() {}
~testMap() {};
void insert_map(int, int);
private:
std::map<int,int> _map;
};
void testMap::insert_map(int i, int j) {
_map.insert(std::pair<int, int>(i,j));
}
int main() {
testMap t;
t.insert_map(12, 34);
return 0;
}
在 32 位 Ubuntu 11.04 上编译:
g++ leak.cpp -o leak
在 valgrind 监督下运行:
valgrind ./leak
==20773== Memcheck, a memory error detector
==20773== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==20773== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==20773== Command: ./leak
==20773==
==20773==
==20773== HEAP SUMMARY:
==20773== in use at exit: 0 bytes in 0 blocks
==20773== total heap usage: 1 allocs, 1 frees, 24 bytes allocated
==20773==
==20773== All heap blocks were freed -- no leaks are possible
==20773==
==20773== For counts of detected and suppressed errors, rerun with: -v
==20773== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 17 from 6)
没有内存泄漏。
可能您的编译器自动定义了一个完全空的类析构函数(因为缺少 {}),在退出私有(private)成员映射析构函数时不再自动调用。
希望对您有所帮助:)
关于c++ - map 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6770316/
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
ruby如何管理内存。例如:如果我们在执行过程中采用C程序,则以下是内存模型。类似于这个ruby如何处理内存。C:__________________|||stack|||------------------||||------------------|||||Heap|||||__________________|||data|__________________|text|__________________Ruby:? 最佳答案 Ruby中没有“内存”这样的东西。Class#allocate分配一个对象并返回该对象。这就是程序
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我
你好,我无法成功如何在散列中删除key后释放内存。当我从哈希中删除键时,内存不会释放,也不会在手动调用GC.start后释放。当从Hash中删除键并且这些对象在某处泄漏时,这是预期的行为还是GC不释放内存?如何在Ruby中删除Hash中的键并在内存中取消分配它?例子:irb(main):001:0>`ps-orss=-p#{Process.pid}`.to_i=>4748irb(main):002:0>a={}=>{}irb(main):003:0>1000000.times{|i|a[i]="test#{i}"}=>1000000irb(main):004:0>`ps-orss=-p
我需要从json记录中获取一些值并像下面这样提取curr_json_doc['title']['genre'].map{|s|s['name']}.join(',')但对于某些记录,curr_json_doc['title']['genre']可以为空。所以我想对map和join()使用try函数。我试过如下curr_json_doc['title']['genre'].try(:map,{|s|s['name']}).try(:join,(','))但是没用。 最佳答案 你没有正确传递block。block被传递给参数括号外的方法
我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“
这会导致Ruby出现内存问题吗?我知道如果大小超过10KB,Open-URI会写入TempFile。但是HTTParty会在写入TempFile之前尝试将整个PDF保存到内存吗?src=Tempfile.new("file.pdf")src.binmodesrc.writeHTTParty.get("large_file.pdf").parsed_response 最佳答案 您可以使用Net::HTTP。参见thedocumentation(特别是标题为“流媒体响应机构”的部分)。这是文档中的示例:uri=URI('http://e
有没有办法让Ruby能够做这样的事情?classPlane@moved=0@x=0defx+=(v)#thisiserror@x+=v@moved+=1enddefto_s"moved#{@moved}times,currentxis#{@x}"endendplane=Plane.newplane.x+=5plane.x+=10putsplane.to_s#moved2times,currentxis15 最佳答案 您不能在Ruby中覆盖复合赋值运算符。任务在内部处理。您应该覆盖+,而不是+=。plane.a+=b与plane.a=