- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在以下程序中观察到内存泄漏:
// g++ -std=c++11 32_MyTime.cpp
//
// valgrind --leak-check=full ./a.out
//
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
using namespace std;
class MyTime {
private:
int year;
int month;
int day;
int hour;
boost::posix_time::ptime *ptrTime;
void update() {
year = ptrTime->date().year();
month = ptrTime->date().month();
day = ptrTime->date().day();
hour = ptrTime->time_of_day().hours();
}
public:
~MyTime() {
if (ptrTime!=nullptr) delete ptrTime;
}
MyTime(int y, int m, int d, int h):year{y},month{m},day{d},hour{h} {
ptrTime = new boost::posix_time::ptime(
boost::gregorian::date(year, month, day),
boost::posix_time::hours(hour));
}
void addHours(int nHours) {
if (ptrTime!=nullptr) delete ptrTime;
ptrTime = new boost::posix_time::ptime(
boost::gregorian::date(year, month, day),
boost::posix_time::hours(hour+nHours));
update();
}
int getYear() const { return year; }
int getMonth() const { return month; }
int getDay() const { return day; }
int getHour() const { return hour; }
void set(int y, int m, int d, int h) {
if (ptrTime!=nullptr) delete ptrTime;
ptrTime = new boost::posix_time::ptime(
boost::gregorian::date(y, m, d),
boost::posix_time::hours(h));
update();
}
time_t getSecSince() const {
return (*ptrTime - boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))).total_seconds();
}
const boost::posix_time::ptime* getPTime() const {
return ptrTime;
}
bool operator==(const MyTime& other) const {
return *ptrTime==*other.getPTime();
}
bool operator!=(const MyTime& other) const {
return !(*this == other);
}
bool operator<(const MyTime& other) const {
return *ptrTime < *(other.getPTime());
}
bool operator<=(const MyTime& other) const {
return *ptrTime <= *(other.getPTime());
}
bool operator>(const MyTime& other) const {
return *ptrTime > *other.getPTime();
}
bool operator>=(const MyTime& other) const {
return *ptrTime >= *other.getPTime();
}
};
void test() {
MyTime t1 {2016,5,21,17};
MyTime t2 {2016,5,22,10};
bool print = false;
if (print) {
cout << "From : " << *t1.getPTime() << endl; // Memory leak
cout << "To : " << *t2.getPTime() << endl; // Memory leak
}
cout << " t1 < t2 " << (t1<t2) << endl;
cout << " t1 > t2 " << (t1>t2) << endl;
cout << " t1 == t2 " << (t1==t2) << endl;
cout << " t1 < t1 " << (t1<t1) << endl;
cout << " t1 > t1 " << (t1>t1) << endl;
cout << " t1 == t1 " << (t1==t1) << endl;
while (t1<=t2) {
if (print)
cout << "> Time : " << *t1.getPTime() << " " << t1.getSecSince() << endl; // Memory leak
int s = t1.getSecSince();
t1.addHours(1);
}
}
int main() {
test();
}
我已经用valgrind
分析了这个程序,这个类应该没有错误(从内存泄漏的角度来看)。
这条指令导致泄漏:
cout << "From : " << *t1.getPTime() << endl; // Memory leak
但我不明白为什么。我假设boost库没有错误。它可能是一些复制构造函数(只是一种解释尝试)。
我该如何避免呢?
解决这个问题对我来说并不重要(代码只是为了调试),但重要的是理解为什么会发生这种情况以避免将来出现类似的错误。
编辑 代码没有内存泄漏。你必须打开打印才能得到泄漏:print = true;
编辑 2使用 print = true;
==22204==
==22204== HEAP SUMMARY:
==22204== in use at exit: 1,748 bytes in 31 blocks
==22204== total heap usage: 223 allocs, 192 frees, 38,751 bytes allocated
==22204==
==22204== LEAK SUMMARY:
==22204== definitely lost: 0 bytes in 0 blocks
==22204== indirectly lost: 0 bytes in 0 blocks
==22204== possibly lost: 619 bytes in 20 blocks
==22204== still reachable: 1,129 bytes in 11 blocks
==22204== suppressed: 0 bytes in 0 blocks
==22204== Rerun with --leak-check=full to see details of leaked memory
==22204==
==22204== For counts of detected and suppressed errors, rerun with: -v
==22204== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 1 from 1)
使用 print = true;
和更多的 cout
执行 Valgrind:
==22277== HEAP SUMMARY:
==22277== in use at exit: 1,748 bytes in 31 blocks
==22277== total heap usage: 79,279 allocs, 79,248 frees, 15,972,927 bytes allocated
==22277==
==22277== LEAK SUMMARY:
==22277== definitely lost: 0 bytes in 0 blocks
==22277== indirectly lost: 0 bytes in 0 blocks
==22277== possibly lost: 619 bytes in 20 blocks
==22277== still reachable: 1,129 bytes in 11 blocks
==22277== suppressed: 0 bytes in 0 blocks
==22277== Rerun with --leak-check=full to see details of leaked memory
==22277==
==22277== For counts of detected and suppressed errors, rerun with: -v
==22277== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 1 from 1)
用空 main
执行 Valgrind:
==22211== Memcheck, a memory error detector
==22211== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==22211== Using Valgrind-3.10.0 and LibVEX; rerun with -h for copyright info
==22211== Command: ./a.out
==22211==
==22211==
==22211== HEAP SUMMARY:
==22211== in use at exit: 0 bytes in 0 blocks
==22211== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==22211==
==22211== All heap blocks were freed -- no leaks are possible
==22211==
==22211== For counts of detected and suppressed errors, rerun with: -v
==22211== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 1 from 1)
最佳答案
好的。泄漏是虚假报告:
==20323== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==20323== at 0x4C2BBCF: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==20323== by 0x4EC21FF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==20323== by 0x4010609: call_init.part.0 (dl-init.c:72)
==20323== by 0x401071A: call_init (dl-init.c:30)
==20323== by 0x401071A: _dl_init (dl-init.c:120)
==20323== by 0x4000D09: ??? (in /lib/x86_64-linux-gnu/ld-2.21.so)
==20323==
如您所见,它是从某个共享库初始化器分配的。如果您从 main 中删除所有代码,它会保持不变。
对于 print = true
,我注意到泄漏的方面(这是设计使然)。
其次,正如其他人所说,不要做 new
事情。没有必要效仿 Java 和 invite all kinds of programmer error 的
缺点
.
只需使用 ptime
成员即可。老实说,我什至不明白为什么要重复 year
、month
、day
和 hour
字段,但你在这里:
我建议首先删除所有重复项:
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
class MyTime : public boost::posix_time::ptime {
using ptime = boost::posix_time::ptime;
using date = boost::gregorian::date;
public:
MyTime(short unsigned y, short unsigned m, short unsigned d, short unsigned h)
: ptime {date{y, m, d}, boost::posix_time::hours(h)}
{ }
void addHours(int nHours) { *this += boost::posix_time::hours(nHours); }
int getYear() const { return date().year(); }
int getMonth() const { return date().month(); }
int getDay() const { return date().day(); }
int getHour() const { return time_of_day().hours(); }
// instead of set, just assign `v = { 2016, 5, 22, 9 }`
time_t getSecSince() const { return (*this - ptime{date{1970, 1, 1}}).total_seconds(); }
};
void test() {
MyTime t1{ 2016, 5, 21, 17 };
MyTime t2{ 2016, 5, 22, 10 };
bool print = false;
if (print) {
std::cout << "From : " << t1 << std::endl;
std::cout << "To : " << t2 << std::endl;
}
std::cout << " t1 < t2 " << (t1 < t2) << std::endl;
std::cout << " t1 > t2 " << (t1 > t2) << std::endl;
std::cout << " t1 == t2 " << (t1 == t2) << std::endl;
std::cout << " t1 < t1 " << (t1 < t1) << std::endl;
std::cout << " t1 > t1 " << (t1 > t1) << std::endl;
std::cout << " t1 == t1 " << (t1 == t1) << std::endl;
while (t1 <= t2) {
if (print)
std::cout << "> Time : " << t1 << " " << t1.getSecSince() << std::endl;
int s = t1.getSecSince();
t1.addHours(1);
}
}
int main() {
test();
}
既然 MyTime
现在实际上只是对 ptime
的额外操作,为什么不呢:
using MyTime = boost::posix_time::ptime;
using MyDate = boost::gregorian::date;
MyTime make_hdate(short unsigned y, short unsigned m, short unsigned d, short unsigned h) {
return { MyDate{ y, m, d }, boost::posix_time::hours(h) };
}
MyTime addHours(MyTime const &mt, int nHours) {
return mt + boost::posix_time::hours(nHours);
}
time_t getSecSince(MyTime const &mt) {
return (mt - MyTime{ MyDate{ 1970, 1, 1 } }).total_seconds();
}
关于c++ 内存泄漏 - boost 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37373944/
IntentReceiver 正在泄漏 由于 onDetachedFromWindow 在某些情况下未被调用。 @Override protected void onDetachedFromWind
好吧,我很难追踪这个内存泄漏。运行此脚本时,我没有看到任何内存泄漏,但我的 objectalloc 正在攀升。 Instruments 指向 CGBitmapContextCreateImage >
我编写了一个测试代码来检查如何使用 Instrument(Leaks)。我创建了一个单一 View 应用程序,单击按钮后我加载了一个像这样的新 View ... - (IBAction)btn_clk
我正在使用这个简单的代码并观察单调增加的内存使用量。我正在使用这个小模块将内容转储到磁盘。我观察到它发生在 unicode 字符串上而不是整数上,我做错了什么吗? 当我这样做时: >>> from u
我有以下泄漏的代码。 Instruments 表示,泄漏的是 rssParser 对象。我“刷新”了 XML 提要,它运行了该 block 并且发生了泄漏...... 文件.h @interface
我在我编写的以下代码片段中发现了内存泄漏 NSFileManager *fileManager=[[NSFileManager alloc] init]; fileList=[[fileManager
因此,我正在开发HTML5 / javascript rts游戏。观察一直有几种声音在播放。因此,对我来说,是一段时间后声音听起来像是“崩溃”,并且此浏览器选项卡上的所有声音都停止了工作。我只能通过重
下面是我正在使用的一段代码及其输出。 my $handle; my $enterCount = Devel::Leak::NoteSV($handle); print "$date entry $en
在这篇关于 go-routines 泄漏的帖子之后,https://www.ardanlabs.com/blog/2018/11/goroutine-leaks-the-forgotten-sende
我想知道为什么在执行 ./a.out 后随机得到以下结果。有什么想法我做错了吗?谢谢 http://img710.imageshack.us/img710/8708/trasht.png 最佳答案 正
我正在 Swift 中开发一个应用程序,在呈现捕获我放在一起的二维码的自定义 ViewController 后,我注意到出现了巨大的内存跳跃。 该代码本质上基于以下示例:http://www.appc
下面是我的 javascript 代码片段。它没有按预期运行,请帮我解决这个问题。 function getCurrentLocation() { console.log("insi
我们在生产环境中部署了 3 个代理 Kafka 0.10.1.0。有些应用程序嵌入了 Kafka Producer,它们将应用程序日志发送到某个主题。该主题有 10 个分区,复制因子为 3。 我们观察
我正在使用仪器来检测一些泄漏,但有一些泄漏我无法解决; NSMutableString *textedetails = [[NSMutableString alloc] init];
如果我使用性能工具测试我的代码 - 泄漏,它没有检测到任何泄漏。这是否意味着代码没有泄漏任何内存? 我有一个越狱的 iPhone,我可以监控可用内存。如果有人知道,那就是 SBSettings。我测试
我在从 AddressBook 中获取图像时遇到了很大的问题,下面我粘贴了我的代码。此 imageData 从未被释放,在我的 Allocations Instruments 上它看起来总是在内存中它
- (NSMutableArray *)getArrayValue:(NSArray *)array{ NSMutableArray *valueArray = [NSMutableArra
Instruments 工具说这是一个泄漏,有什么想法吗? 我在 for 循环结束时释放变量对象 在上述方法的开头,这就是我设置变量对象的方式,即自动释放; NSMutableArray *varia
我正在跟踪我的 iOS 应用程序的内存泄漏,我有一个奇怪的泄漏导致我的应用程序崩溃......负责的框架是:CGImageMergeXMPPropsWhithLegacyProps。在某些时候,我的应
我正在尝试使用 NSOperationQueue 在后台线程中执行一个方法,如下所示: NSOperationQueue *queue = [NSOperationQueue new]; NS
我是一名优秀的程序员,十分优秀!