我正在使用 Valgrind 检查内存泄漏。
不幸的是,我收到了 Leak_DefinitelyLost 警告。
附件是我的代码的简化版本,它重现了错误:
#include <iostream>
#include <vector>
#include <memory>
#include <unordered_map>
using namespace std;
class Base{
public:
explicit Base(double a){
a_ = a;
}
virtual void fun() = 0;
protected:
double a_;
};
class Derived_A : public Base{
public:
Derived_A(double a, vector<double> b, vector<double> c): Base(a), b_{b}, c_{c}{
}
void fun() override{
cout << "Derived_A " << a_ << endl;
}
private:
vector<double> b_;
vector<double> c_;
};
class Derived_B : public Base{
public:
Derived_B(double a, double b, double c): Base(a), b_{b}, c_{c}{
}
void fun() override{
cout << "Derived_B " << a_ << endl;
}
private:
double b_;
double c_;
};
int main() {
unordered_map<string, unique_ptr<Base> > m;
for(int i=0; i<10; ++i){
unique_ptr<Base> o;
if(i%2 == 0){
vector<double> b{1., 2., 3.};
vector<double> c{4., 5., 6.};
o = make_unique<Derived_A>(i, move(b), move(c));
m[to_string(i)] = move(o);
}else{
double b = 1.;
double c = 2.;
o = make_unique<Derived_B>(i, b, c);
m[to_string(i)] = move(o);
}
}
for(const auto &any:m){
any.second->fun();
}
return 0;
}
丢失发生在 make_unique 调用期间:
Leak_DefinitelyLost
vg_replace_malloc.c
240 bytes in 10 blocks are definitely lost in loss record 1 of 1
operator new(unsigned long)
__gnu_cxx::new_allocator<double>::allocate(unsigned long, void const*)
std::allocator_traits<std::allocator>::allocate(std::allocator<double>&, unsigned long)
std::_Vector_base<double, std::allocator>::_M_allocate(unsigned long)
std::_Vector_base<double, std::allocator>::_M_create_storage(unsigned long)
std::_Vector_base<double, std::allocator>::_Vector_base(unsigned long, std::allocator<double> const&)
std::vector<double, std::allocator>::vector(std::vector<double, std::allocator> const&)
Derived_A::Derived_A(double, std::vector<double, std::allocator>, std::vector<double, std::allocator>)
std::_MakeUniq<Derived_A>::__single_object std::make_unique<Derived_A, int&, std::vector, std::vector>(int&, std::vector<double, std::allocator>&&, std::vector<double, std::allocator>&&)
main
我不确定我做错了什么。有人可以澄清错误发生在哪里吗?
(我从 CLion 2018.1.5、Valgrind 3.13.0 调用 Valgrind。)
最佳答案
Base 缺少虚拟析构函数,因此您调用 UB .
关于c++ - Valgrind 在 std::make_unique 中显示内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52180485/
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
ruby如何管理内存。例如:如果我们在执行过程中采用C程序,则以下是内存模型。类似于这个ruby如何处理内存。C:__________________|||stack|||------------------||||------------------|||||Heap|||||__________________|||data|__________________|text|__________________Ruby:? 最佳答案 Ruby中没有“内存”这样的东西。Class#allocate分配一个对象并返回该对象。这就是程序
目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi