给定一个C++中带有私有成员变量name
和基本构造函数的简单类:
#include <QString>
class Testclass
{
private:
QString *name;
public:
Testclass(): name(new QString()) {}
};
Run Code Online (Sandbox Code Playgroud)
为什么valgrind的memcheck会抱怨1个块中的8个字节,这在使用这个构造函数时肯定会丢失?
我有一个包含一些元素的结构,我在循环中释放了这个结构的内存,大致如下:
for (i = 0; i < teller; i++) {
free((glycan+i)->desc);
}
free(glycan)
Run Code Online (Sandbox Code Playgroud)
我假设指针仍然指向空的内存块,因此我想将它们设置为NULL,如下所示:
for (i = teller; i > 0; i--) {
(glycan+i)->desc = NULL;
}
glycan = NULL;
Run Code Online (Sandbox Code Playgroud)
然而Valgrind告诉我一些我不太懂的东西:
==11783== Invalid write of size 4
==11783== at 0x8048F49: main (spectral_matcher.c:122)
==11783== Address 0x431c070 is 72 bytes inside a block of size 28,000 free'd
==11783== at 0x4027C02: free (vg_replace_malloc.c:366)
==11783== by 0x8048F2C: main (spectral_matcher.c:121)
Run Code Online (Sandbox Code Playgroud)
任何人都可以向我解释为什么会发生这种警告/错误以及我应该采取哪些不同的方法来解决它?
编辑:我知道我在释放后将指针设置为NULL,释放只将内存标记为空闲,因此指针仍然完整(如果我没有记错),我随后希望将其设置为NULL.
我正在编写一个程序来从txt文件中选择单词.编译标志:-std = gnu99程序有一些我用GDB和Valgrind调试的分段错误.Valgrind标志: - track-origin = yes --leak-check = full --show-reachable = yes我对Valgrind错误消息有疑问
首先,这里的代码解释了
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <errno.h>
struct character
{
char **words;
int *count;
int arrayCounter;
};
/*
* if finds in the array the word to add, then increase counter
* if it does not find the word, it puts the word non la trova ce la mette nel primo slot che è NULL, …
Run Code Online (Sandbox Code Playgroud) 在实现下面的C++代码后,我运行valgrind --leak-check=full
以检查是否有任何内存泄漏.结果是在退出时使用了0个字节,并且 没有泄漏是可能的.
然而,后来我发现我忘了使用delete[] x
而不是仅仅delete x
在析构函数中使用.
我搜索了一些解释(例如:C++中的delete vs delete []运算符),我读到的所有内容都说使用delete
without []
会导致内存泄漏,因为它只调用数组中第一个对象的析构函数.
我将代码更改为删除[],并且valgrind输出相同(如预期的那样).但是现在我很困惑:"valgrind是否有问题,或者delete
即使没有运算符,阵列也能正常工作[]
吗?"
#include <iostream>
#include <string.h>
using namespace std;
class Foo {
private: char *x;
public:
Foo(const char* px) {
this->x = new char[strlen(px)+1];
strcpy(this->x, px);
}
~Foo() {
delete x;
}
void printInfo() { cout << "x: " << x << endl; }
};
int main() {
Foo *objfoo = …
Run Code Online (Sandbox Code Playgroud) 我不知道为什么我在这里有内存泄漏,非常感谢任何建议.请注意,在进程终止之前,我调用destroy(),这是一个应该删除单例对象的静态成员函数.
这是相关的代码和valgrind的messaeg:
Manager.h:
class Manager {
public:
// Constructor/destructor
static Manager * instance();
static void destroy();
~Manager();
// Bunch of functions that I didn't write here
private:
Manager();
static Manager * _singleton;
// Bunch of fields that I didn't write here
};
Manager.cpp:
#include "Manager.h"
Manager * Manager::_singleton = NULL;
Manager * Manager::instance() {
if (_singleton == NULL) {
_singleton = new Manager();
}
return _singleton;
}
void Manager::destroy()
{
delete _singleton;
_singleton = NULL;
}
/*
* Destructor
*/ …
Run Code Online (Sandbox Code Playgroud) 我是C,Ubuntu的新手,并决定安装Valgrind并在这个简单的C代码上测试它:
#include <stdio.h>
int *p;
int main(void) {
p = calloc(100, sizeof(int));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我已将此代码放在sum.c文件中并进行编译.然后我输入了:
valgrind --tool=memcheck --leak-check=yes sum
Run Code Online (Sandbox Code Playgroud)
在终端窗口,这是我得到的:
我不知道它是继续循环还是只是卡住,但它会保持这种方式,直到我点击ctrl + d来停止它,这就是我得到的:
难道我做错了什么 ?为什么我不能看到我有内存泄漏?
顺便说一句,这是Ubuntu版本11.04.
提前致谢
==3905== ERROR SUMMARY: 14 errors from 2 contexts (suppressed: 2 from 2)
==3905==
==3905== 6 errors in context 1 of 2:
==3905== Invalid write of size 4
==3905== at 0x401BFE: EliminateXr (in /home/suraj/Desktop/project/fm)
==3905== by 0x402040: fm_elim (in /home/suraj/Desktop/project/fm)
==3905== by 0x401395: name_fm (in /home/suraj/Desktop/project/fm)
==3905== by 0x400C38: main (in /home/suraj/Desktop/project/fm)
==3905== Address 0x51fc724 is 36 bytes inside a block of size 39 alloc'd
==3905== at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3905== by 0x401064: alloc_matrix (in /home/suraj/Desktop/project/fm)
==3905== by 0x401A59: EliminateXr …
Run Code Online (Sandbox Code Playgroud) 我用valgrind用选项调试我的代码track-origins=yes
并遇到了这个错误.
$ valgrind --track-origins=yes ./frgtnlng < in > out
==7098==
==7098== Conditional jump or move depends on uninitialised value(s)
==7098== at 0x4C2F1BC: strcmp (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7098== by 0x400857: main (frgtnlng.c:24)
==7098== Uninitialised value was created by a stack allocation
==7098== at 0x40064C: main (frgtnlng.c:9)
==7098==
==7098== Conditional jump or move depends on uninitialised value(s)
==7098== at 0x40085A: main (frgtnlng.c:24)
==7098== Uninitialised value was created by a stack allocation
==7098== at 0x40064C: main (frgtnlng.c:9)
Run Code Online (Sandbox Code Playgroud)
第9行是:
scanf("%d", &t);
Run Code Online (Sandbox Code Playgroud)
我不明白这是如何导致问题的.
frgtnlng.c: …
我已经将我的代码减少到最简单以隔离我的问题,我弄清楚我的问题是什么,但我无法解决它.事实上,我甚至不知道是否有问题.
我有一个函数,用于初始化未初始化的变量并重新初始化已初始化的变量.我的问题是我声明的变量似乎是初始化的.
以下是代码的内容:
/**
* This software defines the type TabDyn and gives the tools to manipulate it
*
* TabDyn is conceptually an array of integers. The first element is the size while the others are the members of the array.
*
* Here are the function provided to manipulate TabDyn :
* td_clear(TabDyn* td) : Create the TabDyn object if non existant and initialize it to an empty one. If it exists, it empties it.
*
*/ …
Run Code Online (Sandbox Code Playgroud) valgrind ×10
c ×6
c++ ×3
memory-leaks ×3
calloc ×1
constructor ×1
free ×1
gcc ×1
gdb ×1
new-operator ×1
pointers ×1
ubuntu ×1