- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
根据 valgrind,我有一些严重的内存泄漏错误。奇怪的是,当我从 visual studio 运行我的程序时,它没有崩溃。但是,当我尝试在 linux 上编译时,我遇到了段错误,所以我猜是出了什么问题。
我注释掉了大部分代码,只留下了 new 和 delete[] 部分(注意,没有 new 语句,valgrind 没有任何问题)但奇怪的是,在这个测试版本中,我是从字面上删除[]/紧随/新的,而 valgrind 仍在提示内存泄漏。
从理论上讲(据我所知)这应该是不可能的。我是误报还是忽略了什么?
代码:
void PosApp::loadRecs() {//.......................This function reads all of the perishable/nonperishable data
bool mycontinue = true;
fstream myfile;
myfile.open(_filename, ios::in);
if (!myfile.is_open()) {
myfile.clear();
myfile.close();
myfile.open(_filename, ios::out);
myfile.close();
mycontinue = false;
}
if (mycontinue) {
char buffer = '\0'; //Valgrind didn't like it unassigned
int itemindex = 0;
Item* myitem;
while (!myfile.get(buffer).fail()) {// && itemindex <= _noOfItems - 1) {
std::cout << std::endl << "buff: " << buffer << std::endl;
if (buffer == 'N' || buffer == 'P') {
std::cout << std::endl;
std::cout << "The Deallocation:" << std::endl
<< "No of items -1: " << _noOfItems
<< " >= itemindex: " << itemindex << std::endl;
if (_noOfItems - 1 >= itemindex) { //unitialized valgrind error (conditional depends on unitialized values)
std::cout << std::endl << "going to deallocate name: " << _items[itemindex]->name() << " at index: " << itemindex << std::endl;
delete[] _items[itemindex]; //VALGRIND SAY CANT DO THIS invalid read of size 4
}
if (buffer == 'P') {
std::cout << std::endl << "going to allocate a perishable";
myitem = new Perishable();//new keyword
std::cout << std::endl << "DONE allocating a perishable" << std::endl;
delete[] myitem;
}
else if (buffer == 'N') {//else is extra safe
std::cout << std::endl << "going to allocate a nonperishable";
myitem = new NonPerishable();
std::cout << std::endl << "DONE allocating a nonperishable" << std::endl;
delete[] myitem;
}
//std::cout << std::endl << "Going to assign " << " at index: " << itemindex << std::endl;
//myfile.ignore();//.................ignore comma NOTE THIS IS IN WRONG SPOT?
//myitem->load(myfile);
//_items[itemindex] = myitem;
//std::cout << "Assigned name: " << _items[itemindex]->name() << " at index: " << itemindex << std::endl;
//itemindex++; //NoOfItems
}
}
myfile.close();
_noOfItems = itemindex;//inderect lost blocks
}
}
Valgrind 输出:
== 20875 == HEAP SUMMARY :
== 20875 == in use at exit : 656 bytes in 5 blocks
== 20875 == total heap usage : 16 allocs, 16 frees, 10, 869 bytes allocated
== 20875 ==
== 20875 == Searching for pointers to 5 not- freed blocks
== 20875 == Checked 101, 796 bytes
== 20875 ==
== 20875 == 656 bytes in 5 blocks are definitely lost in loss record 1 of 1
== 20875 == at 0x4028C39: operator new(unsigned int) (in / usr / lib / valgrind / vgpreload_memcheck - x86 - linux.so)
== 20875 == by 0x804D6BB : sict::PosApp::run() (PosApp.cpp:403)
== 20875 == by 0x804C1A3 : main(milestone4.cpp:8)
== 20875 ==
== 20875 == LEAK SUMMARY :
== 20875 == definitely lost : 656 bytes in 5 blocks
== 20875 == indirectly lost : 0 bytes in 0 blocks
== 20875 == possibly lost : 0 bytes in 0 blocks
== 20875 == still reachable : 0 bytes in 0 blocks
== 20875 == suppressed : 0 bytes in 0 blocks
== 20875 ==
== 20875 == ERROR SUMMARY : 13 errors from 6 contexts(suppressed : 19 from 8)
== 20875 ==
== 20875 == 1 errors in context 1 of 6 :
== 20875 == Conditional jump or move depends on uninitialised value(s)
== 20875 == at 0x4017E9D : strlen(in / lib / ld - 2.14.1.so)
== 20875 == by 0x636F6C2E : ? ? ?
== 20875 == Uninitialised value was created by a stack allocation
== 20875 == at 0x40078F1 : _dl_init_paths(in / lib / ld - 2.14.1.so)
== 20875 ==
== 20875 ==
== 20875 == 1 errors in context 2 of 6 :
== 20875 == Conditional jump or move depends on uninitialised value(s)
== 20875 == at 0x4017E93 : strlen(in / lib / ld - 2.14.1.so)
== 20875 == by 0x636F6C2E : ? ? ?
== 20875 == Uninitialised value was created by a stack allocation
== 20875 == at 0x40078F1 : _dl_init_paths(in / lib / ld - 2.14.1.so)
== 20875 ==
== 20875 ==
== 20875 == 2 errors in context 3 of 6 :
== 20875 == Invalid read of size 4
== 20875 == at 0x804C70D : sict::PosApp::loadRecs() (PosApp.cpp:132)
== 20875 == by 0x804D6BB : sict::PosApp::run() (PosApp.cpp:403)
== 20875 == by 0x804C1A3 : main(milestone4.cpp:8)
== 20875 == Address 0x43021e4 is 4 bytes before a block of size 148 alloc'd
== 20875 == at 0x4028C39 : operator new(unsigned int) (in / usr / lib / valgrind / vgpreload_memcheck - x86 - linux.so)
== 20875 == by 0x804D6BB : sict::PosApp::run() (PosApp.cpp:403)
== 20875 == by 0x804C1A3 : main(milestone4.cpp:8)
== 20875 ==
== 20875 ==
== 20875 == 3 errors in context 4 of 6 :
== 20875 == Invalid read of size 4
== 20875 == at 0x804C7DB : sict::PosApp::loadRecs() (PosApp.cpp:139)
== 20875 == by 0x804D6BB : sict::PosApp::run() (PosApp.cpp:403)
== 20875 == by 0x804C1A3 : main(milestone4.cpp:8)
== 20875 == Address 0x4302ab4 is 4 bytes before a block of size 120 alloc'd
== 20875 == at 0x4028C39 : operator new(unsigned int) (in / usr / lib / valgrind / vgpreload_memcheck - x86 - linux.so)
== 20875 == by 0x804D6BB : sict::PosApp::run() (PosApp.cpp:403)
== 20875 == by 0x804C1A3 : main(milestone4.cpp:8)
== 20875 ==
== 20875 ==
== 20875 == 5 errors in context 5 of 6 :
== 20875 == Invalid free() / delete / delete[]
== 20875 == at 0x4027B13 : operator delete[](void*) (in / usr / lib / valgrind / vgpreload_memcheck - x86 - linux.so)
== 20875 == by 0x804D6BB : sict::PosApp::run() (PosApp.cpp:403)
== 20875 == by 0x804C1A3 : main(milestone4.cpp:8)
== 20875 == Address 0x43021e4 is 4 bytes before a block of size 148 alloc'd
== 20875 == at 0x4028C39 : operator new(unsigned int) (in / usr / lib / valgrind / vgpreload_memcheck - x86 - linux.so)
== 20875 == by 0x804D6BB : sict::PosApp::run() (PosApp.cpp:403)
== 20875 == by 0x804C1A3 : main(milestone4.cpp:8)
== 20875 ==
--20875--
--20875--used_suppression : 19 U1004 - ARM - _dl_relocate_object
== 20875 ==
== 20875 == ERROR SUMMARY : 13 errors from 6 contexts(suppressed : 19 from 8)
最佳答案
new
应与 delete
匹配,new[]
应与 delete[]
匹配。否则它是未定义的行为。您似乎正在对通过 new
分配的内存执行 delete[]
。
关于删除时C++ valgrind内存丢失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34209450/
我在 Ubuntu 10.04 LTS 上运行 Eclipse Galileo。今天 Ubuntu 在我身上崩溃了,重新启动后,我发现 Eclipse 已经完全失去了 Java Perspective
我使用配置了 sonata_user 的 SonataAdminBundle在 config.yml : sonata_user: impersonating: route:
我有 ubuntu 14.04 但它不见了 docker exec sudo docker exec -it ubuntu_bash bash 我希望在现有正在运行的 docker 容器中运行交互式
我正在使用 Ubuntu 8.04/32 位(作为虚拟机)。在一个不是 min 的项目上执行一些 make 时,我得到了错误: g++:/usr/lib/libstdc++.a: 没有这样的文件或目录
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许在 Stack Overflow 上提出有关通用计算硬件和软件的问题。您可以编辑问题,使其成为
我正在尝试获取有关我在 UIImagePicker 中选择的视频的一些数据。 因此,当它进入 UIImagePicker 委托(delegate)方法(如下)时,我知道我需要使用信息字典中的 UIIm
我的网站最近被可能的黑客行为删除了。我上传了备份的文件夹和数据库,但现在我的 View 没有显示。其他一切都有效。我想不出有什么变化,只是上传了几天前的备份。 这些字段在 admin/build/vi
我执行以下操作来设置我的 session ,这是有效的,因为 echo 出现了。但是当我转到下一页或另一页时, session 不存在吗?我做错了什么? $session_start(); if ($
我试图在 BigQuery 中使用这段代码,显然是从 GA 中获取数据,但 _TABLE_SUFFIX 似乎有问题。错误显示“错误:无法识别的名称:_TABLE_SUFFIX at [12:3]”您能
输入:8(2 5 6 9 10 2 7 4)预期输出:(2 7 9 9 12 5 7 6)实际输出:(2 7 9 9) 这是我的大学作业,也是我第一次在这里提问。我不知道为什么,但 10 没有扫描,有
$('div'); // 我在上面的代码中遇到错误。在检查 .js 代码时,我找不到名为 $ 的函数,但根据文档,应该有一个。 最佳答案 试试 http://ajax.googleapis.co
以下简单代码的输出对我来说有点奇怪。它错过了在控制台上打印的 0 到 100 之间的一些数字。 谁能解释一下为什么省略打印?我对并发编程完全陌生。 import java.util.concurren
我正在学习 xamarin 以构建移动应用程序,但我对 Xamarin 和移动应用程序一无所知。我打开了一个空白的移动应用程序,其中有“Hello world”示例并编辑了一些文本,但我的应用程序图标
我正在将一些值存储到 sqlite 数据库中。因此,数据是作为字符串从文本字段收集的,然后转换为 double 并持久化。 这是我试过的 NSDecimalNumber 答案; value.answe
我有一个奇怪的案例。突然,其中一个表中的一些记录(这么多记录)丢失了。首先,我认为这是由我的 PHP 脚本中的错误引起的。但是,我检查了一下,我的脚本中没有DELETE操作,只有UPDATE。有谁知道
我正在复制 NSString来自 NSDictionary进入本地NSString使用 [[NSString alloc] initWithString:] ,对其进行处理(删除一些字符),然后将其发
当保存在根文件夹中时,我的非常基本的 html 页面保持样式。为一个组创建了一个新文件夹,但是当我将页面移动到该文件夹时,它们似乎失去了与 css 文件的连接。 认为问题可能出在链接上,因为它现在
我需要使用 OpenCV 训练一些图像。但问题是,我找不到 opencv_createsamples 程序。我以正常方式安装了 OpenCV,因为我使用的是 Windows 7。这个 opencv_c
我有一个绑定(bind)到 JTable 的 ArrayList。之后 bindingGroup.unbind(); bindingGroup.bind(); (完成刷新数据)我丢失了表格单元格渲
所以基本上我们只是丢失了一个 keystore 和备份 keystore 。但是我们可能知道原始 keystore 的密码。 我的问题是,如果我们知道原始 keystore 的密码,我们可以重新生成
我是一名优秀的程序员,十分优秀!