Valgrind在树莓派上的多线程C++程序中未检测到内存泄漏。

huangapple go评论78阅读模式
英文:

Valgrind not detecting memory leak in threaded c++ program in raspberry pi

问题

以下是您提供的代码的翻译:

#include <iostream>
#include <thread>
#include <stdlib.h>
void *liveDataPush(void *);
using namespace std;
int main(void)
{
    pthread_t liveDataPushThread;
    int RetThrd;
    void *ptr;
    RetThrd = pthread_create(&liveDataPushThread, NULL, liveDataPush, ptr);
    if (RetThrd != 0)
    {
        cout << "RetThrd" << endl;
    }
    for (int i = 0; i < 10000; i++)
    {
        int *j = (int *)(malloc(1024 * sizeof(int)));
        cout << "Hi" << endl;
        //free(j);
    }
    pthread_join(liveDataPushThread, NULL);
    return 0;
}
void *liveDataPush(void *)
{
    int *i = new int;
    *i = 30;
    //delete i;
    for (int i = 0; i < 10000; i++)
    {
        int *j = new int[1024];
        cout << "Hello" << endl;
        //delete[] j;
    }
    return NULL;
}

关于Valgrind的问题,根据您提供的信息,似乎Valgrind没有检测到内存泄漏。这可能是由于使用了多线程(pthread)导致Valgrind无法正常检测内存泄漏。

要尝试检测内存泄漏,您可以尝试以下Valgrind命令:

valgrind --leak-check=full --show-leak-kinds=all ./vlgrnd

这个命令会执行全面的内存泄漏检查,并显示所有泄漏的详细信息。希望这能帮助您找到潜在的内存泄漏问题。请确保您的Valgrind版本是最新的,以获得最佳的检测结果。

英文:
#include &lt;iostream&gt;
#include &lt;thread&gt;
#include &lt;stdlib.h&gt;
void *liveDataPush(void *);
using namespace std;
int main(void)
{
    pthread_t liveDataPushThread;
    int  RetThrd;
    void *ptr;
    RetThrd=pthread_create(&amp;liveDataPushThread,NULL,liveDataPush, ptr);
    if (RetThrd != 0)
    {
        cout &lt;&lt; &quot;RetThrd&quot; &lt;&lt; endl;
    }
    for (int i = 0; i &lt; 10000; i++)
    {
        int *j = (int *)(malloc(1024 * sizeof(int)));
        cout &lt;&lt; &quot;Hi&quot; &lt;&lt; endl;
        //free(j);
    }
    pthread_join(liveDataPushThread, NULL);
    return 0;
}
void *liveDataPush(void *)
{
    int *i = new int;
    *i = 30;
    //delete i;
    for (int i = 0; i &lt; 10000; i++)
    {
        int *j = new int[1024];
        cout &lt;&lt; &quot;Hello&quot; &lt;&lt; endl;
        //delete[] j;
    }
    return NULL;
}

I have stored this program in raspberry pi board and trying to generate memory leak errors using valgring in putty terminal.
I tried :

gcc  -Wall -pedantic -g valgrind.cpp -o vlgrnd -pthread -lstdc++ 
valgrind --leak-check=yes ./vlgrnd
valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./vlgrnd
valgrind --tool=memcheck --vgdb=yes --vgdb-error=0 --leak-check=full ./vlgrnd

with (in another putty terminal)

gdb ./vlgrnd
target remote | vgdb

monitor leak_check full reachable any

None of them are detecting memory leak.

Using pthread_t along with #include thread, pthread_create, -pthread(during gcc compile) is causing "valgrind --leak-check=yes ./vlgrnd" not to return HEAP, LEAK and ERROR SUMMARIES.valgrind output without SUMMARIES If we remove(comment out) thread related keywords and call thread as normal function and compile without pthread, then valgrind commands return SUMMARIES.
valgrind output with SUMMARIES
My question is what is the valgrind command that producet memory leak errors for this program

答案1

得分: 2

以下是要翻译的内容:

你程序中编写的分配部分不属于其可观察行为的一部分。编译器可以自由地将它们进行优化。这里有一个实时演示,在生成的机器代码中从未调用mallocoperator new

https://godbolt.org/z/oG788ebTx

英文:

The allocations written in your program are not part of its observable behavior. The compiler is then free to optimize them away. Here is a live demo, where neither malloc nor operator new is ever called in the generated machine code:

https://godbolt.org/z/oG788ebTx

huangapple
  • 本文由 发表于 2023年4月13日 17:38:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定