标签: valgrind

以下C代码中的内存丢失

我在以下代码中获得了16字节的内存丢失以进行排队.能不能让我知道如何摆脱这个问题?代码是:

      void enqueue( enqueuenode * queueNode1 ,bplus *bplusNew){
      [98] -> enqueue *queue=NULL;
              queue = malloc(sizeof(enqueue_node));
              queue->bplus = bplusNew;
              queue->next= NULL;
                if(queueNode1->headNode == NULL){
                   queueNode1->headNode=queueNode1->tailNode = queue ;
                   }
                 else{
                 queueNode1->tailNode->next = queue;
                 queueNode1->tailNode = queue;
                 }
            }
Run Code Online (Sandbox Code Playgroud)

以下是两个结构

         typedef struct enqueue_help{
           bplus bplusNode;
           struct enqueue_help * next;
         }*enqueue,enqueue_node;

        typedef struct enqueuenode_help{
          enqueue  headNode;
          enqueue  tailNode;
        }*enqueuenode,enqueuenode_node;
Run Code Online (Sandbox Code Playgroud)

对于上面的代码,以下是valgrind输出:

             =23800== 272 (16 direct, 256 indirect) bytes in 1 blocks are definitely lost in loss record 8 of 12
             ==23800==    at 0x4C2260E: malloc …
Run Code Online (Sandbox Code Playgroud)

c valgrind memory-leaks

0
推荐指数
1
解决办法
218
查看次数

C++虚拟析构函数和内存泄漏

我有最奇怪的内存泄漏,我不知道为什么.我有一个抽象类如下:

class ClassA
{
public:
    virtual ~ ClassA (){}
    virtual void notify(ClassB*) = 0;
    virtual void add(ClassB*) = 0;
}; 

class ClassC : public ClassA
{
public:
    void notify(ClassB*)
    { 
        //some cout statements
    }
    void add(ClassB*)
    { 
        //some cout statements
    }
};

int main()
{
    ClassA *f = new ClassC();
    delete f;
}
Run Code Online (Sandbox Code Playgroud)

现在,当我编译代码并运行Valgrind时,它没有泄漏.但是,当我删除ClassA析构函数(或使其成为非虚拟)时,Valgrind将32字节报告为绝对丢失的内存.我不知道为什么会发生这种情况,因为我的析构函数什么也没做,而且没有成员变量.有任何想法吗?

编辑:我用U ++编译了Ubuntu 64位

c++ polymorphism valgrind destructor memory-leaks

0
推荐指数
1
解决办法
264
查看次数

无效读取大小为1的Strcpy

我一直收到大小为1的无效读取的valgrind错误,我无法确定原因.

是什么导致错误?

==24647== Invalid read of size 1
==24647==    at 0x40258EA: strcpy (mc_replace_strmem.c:437)
==24647==    by 0x8048606: main (source.c:26)
==24647==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==24647==
==24647==
==24647== Process terminating with default action of signal 11 (SIGSEGV)
==24647==  Access not within mapped region at address 0x0
==24647==    at 0x40258EA: strcpy (mc_replace_strmem.c:437)
==24647==    by 0x8048606: main (source.c:26)
==24647==  If you believe this happened as a result of a stack
==24647==  overflow in your program's main thread (unlikely but …
Run Code Online (Sandbox Code Playgroud)

c valgrind strcpy

0
推荐指数
1
解决办法
2526
查看次数

当我malloc错误的内存量时,为什么C不崩溃

我正在学习C而且我想知道为什么下面的代码没有崩溃,我只使用Valgrind来解决问题.

void push(char *element){
  FILE *file = fopen("stack.db", "w");
  int rc = fwrite(element, sizeof(element), 1, file);
  if (file) fclose(file);
}

void top() {
  char *top_element = malloc(sizeof(char));
  FILE *file = fopen("stack.db", "r+");
  int rc = fread(top_element, sizeof(char), 1, file);
  printf("Top element: %s", top_element);
}

int main(int argc, char *argv[]) {
  char action = argv[1][0];
  switch (action) {
    case 'p':
      printf("pushing element to stack\n");
      push(argv[2]);
      break;
    case 't':
      top();
      break;
    default:
      printf("die\n");
  }
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

首先,我调用push()并写入argv[2]该文件.然后我打电话 …

c memory malloc valgrind heap-memory

0
推荐指数
1
解决办法
96
查看次数

std :: map迭代并删除valgrind错误

我有一个简单的程序,它在迭代Map时删除std :: map中的项目.如果我没有错误,则前导迭代器不会因地图中的擦除而失效.但是valgrind会抛出无效的读错误.有人可以解释原因.

#include <map>
#include <iostream>

typedef std::map<std::string, int> SourceMap;
int main(int argc, char *argv[])
{
    SourceMap sourceMap;
    sourceMap["item1"] = 1;
    sourceMap["item2"] = 2;
    sourceMap["item3"] = 3;
    sourceMap["item4"] = 4;


    for(SourceMap::const_iterator it=sourceMap.begin(); it != sourceMap.end(); ++it)
    {
        sourceMap.erase(it->first);
    }
}
Run Code Online (Sandbox Code Playgroud)

Valgrind错误:

==31703== Invalid read of size 8
==31703==    at 0x3851069E60: std::_Rb_tree_increment(std::_Rb_tree_node_base*) (in /usr/lib64/libstdc++.so.6.0.13)
==31703==    by 0x4013D6: std::_Rb_tree_const_iterator<std::pair<std::string const, int> >::operator++() (stl_tree.h:259)
==31703==    by 0x40106B: main (map_iterator.cpp:14)
==31703==  Address 0x4c2c0b8 is 24 bytes inside a block of size 48 free'd …
Run Code Online (Sandbox Code Playgroud)

c++ valgrind

0
推荐指数
1
解决办法
1201
查看次数

Valgrind error -Address在分配后为零字节 - C/C++

我正在为我的项目进行内存泄漏.我收到一个错误,我无法理解它的根本原因.我的代码很大,所以我会在这里放置需要的块.

基本上我在一个函数中有一个数组find_pulses,我动态分配如下:

float*rmsArray;
    rmsArray = (float*)malloc((N-pulse_samplewidth+1)*sizeof(float));
Run Code Online (Sandbox Code Playgroud)

我调试到这一点,发现它N-pulse_samplewidth+1不是零.(实际上~2 ^ 21)

我将值填充到此数组中,如下所示:

for (int loop1 = 0; loop1 < N-pulse_samplewidth; ++loop1) {
// populate rms array here.
}
Run Code Online (Sandbox Code Playgroud)

我将此数组发送到另一个名为findpeakthis的函数:

 int* ans = findpeak(rmsArray,N,pulse_samplewidth,startsec,min,max,x);
Run Code Online (Sandbox Code Playgroud)

声明findpeak如下:

int* findpeak(float* data, int n, int pulse_samplewidth,float startsec,float min,float max, float* x);
Run Code Online (Sandbox Code Playgroud)

findpeak函数内部,我将一个特定的值添加data到堆栈中,如下所示:

std::stack<float> peaks_y;
for (int loop1 = 0; loop1 < n; ++loop1) {
if( some condition)
{
peaks_y.push(data[loop1]); // point of error. …
Run Code Online (Sandbox Code Playgroud)

c++ arrays valgrind

0
推荐指数
1
解决办法
744
查看次数

valgrind不抱怨内存损坏?

我有一小段代码作为打击

class singler
{
    private:
        singler() { _id = id++; };
    public:
        ~singler() { cout<<"In destructor of "<<_id<<endl;};
        static singler* allocate() { singler* p = new singler(); return p; };
        static int id;
        int _id;
};
int singler::id = 0;
singler& create_new(singler*& ptr)
{
    singler * p = singler::allocate();
    ptr = p;
    return (*p);
}
int main()
{
    singler* ptr;
    singler obj = create_new(ptr);
    delete ptr;
}
Run Code Online (Sandbox Code Playgroud)

所以你可以看到对象的析构函数将被调用两次,作为证明,我编译并运行它,它给出了输出

In destructor of 0
In destructor of 0
Run Code Online (Sandbox Code Playgroud)

问题在于valgrind,它不会抱怨任何东西,valgrind输出如下== 2

0408== Memcheck, …
Run Code Online (Sandbox Code Playgroud)

c++ valgrind memory-corruption

0
推荐指数
1
解决办法
363
查看次数

cuda-memcheck无法检测R包中的内存泄漏

我正在构建CUDA加速的R包,我想调试cuda-memcheck.所以在这个最小的例子中(在deliberate_memory_leakGitHub分支中),我someCUDAcode.c通过注释掉必要的调用来创建内存泄漏cudaFree.然后,我看是否cuda-memcheck可以找到泄漏.

$ cuda-memcheck --leak-check full  Rscript -e 'library(rcppcuda); hello()'
========= CUDA-MEMCHECK
An object of class "MyClass"
Slot "x":
 [1]  1  2  3  4  5  6  7  8  9 10

Slot "y":
 [1]  1  2  3  4  5  6  7  8  9 10

[1] "Object changed."
An object of class "MyClass"
Slot "x":
 [1] 500   2   3   4   5   6   7   8   9  10

Slot "y":
 [1]    1 1000 …
Run Code Online (Sandbox Code Playgroud)

valgrind memory-leaks cuda r

0
推荐指数
1
解决办法
466
查看次数

为什么我的代码返回分段错误?

我试图在我的图形中添加一个新的顶点,它将添加它以便按字母顺序排列.但是,我一直遇到分段错误.我尝试使用调试器和valgrind,但它只告诉我导致seg错误的方法,我的add_vertex.

    /* Adds a vertex with element new_vertex */
int add_vertex(Graph *graph, const char new_vertex[])
{
    Vertex *new, *curr = graph -> vertices, *prev;
    if ( has_vertex (*graph, new_vertex) || graph == NULL)
        return 0;
    for (prev = graph->vertices; curr = prev->next; prev = curr)
        if (curr->element > new_vertex)
            break;
    new = malloc ( sizeof ( Vertex ) );
    new->element = ( char *) malloc ( sizeof ( strlen ( new_vertex ) + 1) );
    strcpy ( new -> element, …
Run Code Online (Sandbox Code Playgroud)

c gdb valgrind graph segmentation-fault

0
推荐指数
1
解决办法
91
查看次数

why does reading an array in parallel cause memory leaks?

I am reading an array in parallel using openmp. Below is a minimal reproducible example:

#include <cstdint>
#include <cstdlib>
#include <immintrin.h>
#include <iostream>
#include <memory>
#include <omp.h>

int main(int argc, char* argv[]){
  // align to cache line, which is 512 bits or 64 bytes
  size_t actualSize = 2048;

  uint8_t* array = static_cast<uint8_t *>(aligned_alloc(64, actualSize));
  for(size_t i = 0; i < actualSize; i++){
    // initialize values
    array[i] = rand() % 256;
  }

  __m256i sum_v = _mm256_setzero_si256 ();
  #pragma omp parallel for …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading valgrind openmp intrinsics

0
推荐指数
1
解决办法
81
查看次数