Tag: valgrind

编写getline()实现 – Valgrind错误

我必须重新getline()函数的实现,但是使用文件的文件描述符而不是FILE * 。 我只允许使用malloc()和free() ,以及最多25行的5个函数。 我想我已经正确完成了项目,虽然我是C语言的初学者,但我的代码可能并不好。 当我运行它,它工作正常,但valgrind显示我定义definetely lost x bytes ,x取决于文件长度和READ_SIZE(标头中定义的宏)。 根据valgrind的–leak-check=full ,当我使用malloc dest时,我在str_realloc_cat函数中有内存泄漏。 我试过但找不到我应该在哪里/做其他事情? 以下是我的代码: char *get_next_line(const int fd) { static char *remaining = “”; char *buffer; ssize_t cread; size_t i; i = 0; if (remaining == NULL) return (NULL); if ((buffer = malloc(SOF(char) * READ_SIZE + 1)) == NULL || (cread = read(fd, […]

是由程序员在exit()上解除分配吗?

我有一个程序,当我从键盘输入错误数据时,它只是退出exit(1) 。 我正在使用Valgrind进行测试,虽然发生这种情况但没有错误,但我可以看到仍有可达的x字节。 所以我的问题是:程序员在点击exit()或exit()操作系统exit()之前是否需要释放内存?

当使用gcc-5.2.0编译程序时,为什么valgrind没有发现泄漏

今天我正在编写一些东西,在我完成之后,我与valgrind进行了检查,我得到了一个惊喜。 如果我使用gcc-4.9.2在我的Ubuntu(15.04 64BIT)上编译我的程序,如下所示: gcc -Wextra -Werror -Wstrict-prototypes -Wconversion –std=c11 -O2 -g program.c -o program 然后运行valgrind: valgrind –leak-check=full –track-origins=yes ./program 我得到以下输出: ==5325== Memcheck, a memory error detector ==5325== Copyright (C) 2002-2013, and GNU GPL’d, by Julian Seward et al. ==5325== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info ==5325== Command: ./program ==5325== Bye ==5325== […]

很难跟踪内存损坏 – 运行Valgrind时运行正常,没有错误

我们有一个复杂的程序,在重载输入(实际上是任何输入)上运行良好,没有实现multithreading。 我们用线程池实现了multithreading,给定这些输入参数我得到了这些结果: ( 注意 :我说没有错误 ,这意味着我已经用valgrind -v进行了测试,当我说没有内存泄漏时 ,这意味着我已经用valgrind –leak-check=full -v测试了它)。 small_file:成功运行超过1个worker(线程),没有valgrind错误,没有内存泄漏 medium_file:使用1个worker成功运行,没有错误/内存泄漏。 有> 1名工人,我得到:a。 通常是堆损坏错误,b。 双自由。 当使用valgrind -v运行> 1个worker时,程序成功完成。 此外,没有从valgrind打印错误,即ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2) 。 现在我从valgrind开始没有得到任何错误,我该怎么做才能找到这个复杂而庞大的应用程序中的内存损坏问题? DevelopmentEnvironment: Ubuntu,64bit,gcc版本:4.7.2和4.8.1(不同的计算机,更新版本的Ubuntu)。

C字符串,strlen和Valgrind

我试图理解为什么Valgrind正在吐痰: ==3409== Invalid read of size 8 ==3409== at 0x4EA3B92: __GI_strlen (strlen.S:31) 每当我在动态分配的字符串上应用strlen时? 这是一个简短的测试用例: #include #include #include int main() { char *hello = “Hello World”; char *hello2; /* Step 1 */ printf(“Step 1\n”); printf(“strlen : %lu\n”,(unsigned long)strlen(hello)); /* Step 2 */ hello2 = calloc(12,sizeof(char)); hello2[0] = ‘H’; hello2[1] = ‘e’; hello2[2] = ‘l’; hello2[3] = […]

内存没有被释放但仍然可以访问,它是否泄漏?

通过检查valgrind,我看到在终止程序后没有释放5块内存,但它们仍然可以访问。 我需要被它打扰吗? 它是如何发生的? zhanwu@gelata:~/sandbox$ valgrind ./a.out ==2430== Memcheck, a memory error detector ==2430== Copyright (C) 2002-2010, and GNU GPL’d, by Julian Seward et al. ==2430== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info ==2430== Command: ./a.out ==2430== Hello world! Thread1 returns 1 Thread2 returns 10 Thread3 returns 10 ==2430== ==2430== HEAP SUMMARY: ==2430== […]

valgrind mac os mem泄漏

今天我在我的Mac os x 10.6上安装了valgrind并尝试测试它。 事实certificate,系统中存在奇怪的内存泄漏。 我所做的只是创建简单的c文件,获取一些堆内存并立即释放它。 当我跑valgrind时,它表现出类似的东西 Realfrees-MacBook-Pro:C Realfree$ valgrind –tool=memcheck –leak-check=yes –show-reachable=yes ./a.out ==2621== Memcheck, a memory error detector ==2621== Copyright (C) 2002-2010, and GNU GPL’d, by Julian Seward et al. ==2621== Using Valgrind-3.6.0 and LibVEX; rerun with -h for copyright info ==2621== Command: ./a.out ==2621== –2621– ./a.out: –2621– dSYM directory is missing; consider […]

Valgrind报告在OS X 10.8.1上泄露了内存

我在OS X 10.8.1,Mountain Lion上使用Valgrind版本3.8.0。 关于与10.8.1的兼容性, Valgrind的网站说(italics mine): Valgrind 3.8.0适用于 {x86,amd64} -darwin(Mac OS X 10.6和10.7,支持10.8)。 那么,我知道10.8.1只有“有限的支持”。 尽管如此, 这个错误报告说(斜体矿): 这个(最新的3.8.0版本)使Valgrind编译并能够在OSX 10.8上运行小程序。 但请注意,它仍然使用更大的应用程序断言,并且根本没有正确检查32位程序(Memcheck错过了大多数错误)。 好没关系。 因此,如果气质,Valgrind应该在10.8.1上工作。 所以现在我的问题是: 我能够在没有任何问题的情况下让Valgrind在10.8.1上编译,但是当我在几个小C程序上运行它时,我看到了一些奇怪的结果。 为了尝试减少问题的可能原因,我最终编写了以下“程序”: int main () { return 0; } 我会说,不是很令人兴奋,而且很少有漏洞。 然后,我编译并通过Valgrind运行它: gcc testC.c valgrind ./a.out 这是我的输出: ==45417== Command: ./a.out ==45417== ==45417== WARNING: Support on MacOS 10.8 is experimental and mostly broken. ==45417== […]

OS X Yosemite上的Valgrind,出现虚假错误?

我正在跟随学习C艰难的方式 ,我正在练习4:介绍Valgrind 。 我在Mac OS X Yosemite上,在撰写本文时,没有稳定的Valgrind for Yosemite版本。 我找到了Yosemite和Valgrind,并使用了最高投票回答brew install –HEAD valgrind 。 这个安装了Valgrind和我能够跟随Zed的练习。 但是,当我“修复”应用程序时,我仍然遇到错误。 为了仔细检查,我回到练习3 ,这应该没有错误,但我仍然在Valgrind中出错。 这是代码,然后是输出: ex3.c #include int main() { int age = 10; int height = 72; printf(“I am %d years old.\n”, age); printf(“I am %d inches tall.\n”, height); return 0; } 在iTerm中: ransom:learn-c-the-hard-way ben$ rm -f ex3 ransom:learn-c-the-hard-way ben$ […]

openmp是否会分配内存并释放所有内容

openmp是否分配内存并释放所有内存? 因为我跑了valgrind,并且释放了我所有的名单..我在malloc的所有东西,我都自由了。 ==11442== HEAP SUMMARY: ==11442== in use at exit: 192 bytes in 1 blocks ==11442== total heap usage: 2,001 allocs, 2,000 frees, 2,917,280 bytes allocated ==11442== ==11442== LEAK SUMMARY: ==11442== definitely lost: 0 bytes in 0 blocks ==11442== indirectly lost: 0 bytes in 0 blocks ==11442== possibly lost: 0 bytes in 0 blocks ==11442== […]