我有这个可证明的代码:
#include <cstdlib>
#include <cstdio>
int main() {
int ** matrix = NULL;
int c = 1, input = 0;
printf("Enter first row of the matrix:\n");
while (!feof(stdin)) {
input = fgetc(stdin);
matrix = (int**) realloc(matrix, 1 * sizeof (int*));
if (matrix == NULL) {
printf("Troubles with memory allocation!\n");
return 0;
}
matrix[0] = (int *) realloc(matrix[0], c * sizeof (int));
matrix[0][c-1] = (int) input;
c++;
}
free(matrix[0]);
free(matrix);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这导致Valgrind出错,但我真的不知道这是什么意思以及如何解决它......任何人都可以给我一个建议吗?
==30031== 1 errors in context 1 of …
Run Code Online (Sandbox Code Playgroud) 也许我完全失明或愚蠢,但我不明白我是如何在这里得到一个valgrind问题.Valgrind给了我这个:
==3728== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
==3728== at 0x57835DC: send (send.c:33)
==3728== by 0x4058B7: CBSocketSend (CBLibEventSockets.c:287)
==3728== by 0x4E522FB: CBNetworkCommunicatorOnCanSend (CBNetworkCommunicator.c:649)
==3728== by 0x40564F: CBCanSend (CBLibEventSockets.c:235)
==3728== by 0x526E54B: event_base_loop (event.c:1346)
==3728== by 0x405244: CBStartEventLoop (CBLibEventSockets.c:154)
==3728== by 0x5A54E99: start_thread (pthread_create.c:308)
==3728== Address 0x5e7b6b4 is 20 bytes inside a block of size 24 alloc'd
==3728== at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3728== by 0x4E51E2A: CBNetworkCommunicatorOnCanSend (CBNetworkCommunicator.c:592)
==3728== by 0x40564F: CBCanSend (CBLibEventSockets.c:235)
==3728== by 0x526E54B: event_base_loop (event.c:1346)
==3728== by …
Run Code Online (Sandbox Code Playgroud) 我开始使用C语言中的pthreads,我也是一个疯狂的人,我尽可能地将我的代码编写为"无bug".
尽管要特别小心,valgrind告诉我,无论天气如何,我都在泄漏记忆:
我知道这已经讨论过(看到这个,这个也是这个),但我仍然很好奇:
正如我从前面的答案和valgrind跟踪中所理解的那样,pthread_create()是根本原因,根据需要扩展线程使用的堆栈并且有时重用它,因此有一些丢失的空闲.但不太清楚的是,为什么它依赖于执行运行以及为什么在创建分离线程时也会发生这种情况.正如我从某些答案,评论以及该人那样看到的,来自分离线程的资源将在线程完成时释放.我已经尝试了各种调整来解决这个问题(在每个线程结束之前添加一个休眠时间,在主线程结束之前,增加了堆栈大小,添加更多"工作"......)但它没有改变结果很多.另外,为什么在处理分离的线程时会有一个随机数的整体"mallocs()",valgrind是否会丢失一些分离的线程?这似乎也不依赖于堆栈大小.
提供的代码是一个管理器/工作者模型的模拟示例,对于该模型,线程管理的joinable/join()方法似乎更适合imho.
感谢您提供的任何启发!我也希望这些(过度评论的)代码片段对任何希望开始使用pthreads的人都有所帮助.
- swappy
PS Sys信息:debian 64bit arch上的gcc
代码片段1(可加入的线程已加入):
/* Running this multiple times with valgrind, I sometimes end with :
- no errors (proper malloc/free balance)
- 4 extra malloc vs free (most frequently)
The number of mallocs() is more conservative and depends on the number of threads.
*/
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS macros & the likes */
#include <stdio.h> /* …
Run Code Online (Sandbox Code Playgroud) 这两个错误将出现一个链表/低级文件读/ strtok程序.
==10982== Use of uninitialised value of size 4
==10982== at 0x40C3899: strtok (strtok.S:197)
==10982== by 0x8048719: main (main.c:9)
==10982== Uninitialised value was created by a stack allocation
==10982== at 0x80487D2: buildList (functions.c:13)
==10982==
==10982== Use of uninitialised value of size 4
==10982== at 0x40C38C1: strtok (strtok.S:223)
==10982== by 0x8048719: main (main.c:9)
==10982== Uninitialised value was created by a stack allocation
==10982== at 0x80487D2: buildList (functions.c:13)
==10982==
==10982== Conditional jump or move depends on uninitialised value(s)
==10982== at 0x40C38C4: …
Run Code Online (Sandbox Code Playgroud) 我是valgrind的新手,所以这可能会错过解释输出.Valgrind似乎认为字节在以下几行中丢失:
if ( mysqlpp::StoreQueryResult result = query.store() ){
for ( size_t i = 0; i < result.num_rows(); ++i ){
hash = std::string( result[i]["hash"].data(), result[i]["hash"].length() );
label = std::string( result[i]["label"].data(), result[i]["label"].length() );
keywords = std::string( result[i]["keywords"].data(), result[i]["keywords"].length() );
task_raw = std::string( result[i]["task"].data(), result[i]["task"].length() );
solution_raw = std::string( result[i]["solution"].data(), result[i]["solution"].length() );
Run Code Online (Sandbox Code Playgroud)
事实上,每当有一个std :: string以这种方式构建时,它就会抱怨^^.如果我正在复制的方式不正确,或者结果是以某种方式泄露,我无法弄清楚这是否是误报.
有趣的是,它抱怨另一段不使用mysql ++的代码,但同样使用了一个字符串构造函数:
for ( auto & f : frames ){
for ( unsigned int i = 0; i < f.getTask().size(); i++ ){
if ( i + 1 < …
Run Code Online (Sandbox Code Playgroud) 我正在尝试了解如何在完成GTK小部件时正确释放内存,例如,如果我需要创建和销毁许多小部件.但是,无论我尝试什么,valgrind似乎都表明内存泄漏.我已经查看了其他问题,包括列出GTK的valgrind抑制文件的问题,但它没有改变结果.
这是重现我的问题的最简单的代码片段:
#include "gtk/gtk.h"
int main()
{
GtkWidget * widget = gtk_fixed_new();
g_object_ref(widget);
g_object_ref_sink(widget); // remove floating reference, and own this object ourselves
g_object_unref(widget);
gtk_widget_destroy(widget);
}
Run Code Online (Sandbox Code Playgroud)
我的期望是(在处理浮动引用之后),unref()函数应该将引用计数减少到零,然后释放所有内存.我把gtk_widget_destroy()放在那里以获得良好的衡量标准,但我不确定它实际上是否必要(并且它不会改变泄漏的大小).
使用G_SLICE=debug-blocks valgrind ./t3 --supression=~/Downloads/GNOME.supp
来自GTK hello_world程序中的Memory Leaks的valgrind命令的输出
是
==10079== HEAP SUMMARY:
==10079== in use at exit: 164,338 bytes in 847 blocks
==10079== total heap usage: 1,380 allocs, 533 frees, 219,176 bytes allocated
==10079==
==10079== LEAK SUMMARY:
==10079== definitely lost: 0 bytes in 0 blocks
==10079== indirectly lost: 0 bytes in …
Run Code Online (Sandbox Code Playgroud) 在这个问题中,我描述了boost :: asio和boost :: coroutine使用模式,它导致我的应用程序随机崩溃,我发布了我的代码和valgrind以及GDB输出的提取.
为了进一步研究这个问题,我创建了一个较小的概念证明应用程序,它应用了相同的模式.我看到在我发布的源代码较小的程序中出现了同样的问题.
代码启动几个线程并创建一个带有几个虚拟连接的连接池(用户提供的数字).其他参数是无符号整数,它们扮演虚假请求的角色.sendRequest
函数的虚拟实现只是启动异步计时器,等待输入数字的等待秒数和函数的yileds.
有人可以看到这个代码的问题,他可以提出一些解决方案吗?
#include "asiocoroutineutils.h"
#include "concurrentqueue.h"
#include <iostream>
#include <thread>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace boost;
using namespace utils;
#define id this_thread::get_id() << ": "
// ---------------------------------------------------------------------------
/*!
* \brief This is a fake Connection class
*/
class Connection
{
public:
Connection(unsigned connectionId)
: _id(connectionId)
{
}
unsigned getId() const
{
return _id;
}
void sendRequest(asio::io_service& …
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习一些事情(作为一种爱好)并试图学习使用Valgrind.然而,这对我来说似乎没有意义.似乎Valgrind说我在使用calloc之前使用calloc分配字节时会丢失字节!有人可以解释这里发生了什么,为什么第二个程序有效?我在Eclipse中以调试模式编译了程序,并在调试可执行文件上运行了Valgrind.
这是程序:
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 int main(void) {
6
7 char* origstr = calloc(37, sizeof(char*));
8 char* newsubstr = calloc(9, sizeof(char*));
9
10 origstr = "TheQuickBrownFoxJumpedOverTheLazyDog";
11
12 strncpy(newsubstr, origstr + 8, 8);
13 printf("SubString is: %s\n", newsubstr);
14
15 free(newsubstr);
16 free(origstr);
17 return 0;
18 }
Run Code Online (Sandbox Code Playgroud)
以下是Valgrind给我的意思:
$ valgrind --tool=memcheck --leak-check=full ./test
==25404== Memcheck, a memory error detector
==25404== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. …
Run Code Online (Sandbox Code Playgroud) 你能帮我理解GTest和struct packing的内容吗?
问题似乎与在GTest中作为值参数化测试中的值使用时如何打包结构有关.采用直接的方法为每个值实例化一个结构会导致与未初始化值相关的valgrind错误.
这是有关的代码:
#include <gtest/gtest.h>
struct TestItem
{
const char * aString;
int anInt0;
int anInt1;
int anInt2;
};
class TestBase : public ::testing::Test, public ::testing::WithParamInterface<TestItem> {};
TEST_P(TestBase, TestAtoi)
{
TestItem item = GetParam();
std::cout << sizeof(TestItem) << std::endl;
ASSERT_FALSE(0); // actual test doesn't matter
}
INSTANTIATE_TEST_CASE_P(
TestBaseInstantiation,
TestBase,
::testing::Values(
TestItem { "0", 0, 0, 0 }
));
Run Code Online (Sandbox Code Playgroud)
当编译和链接时(我使用cmake构建GTest):
g++ gtest_valgrind.c -o gtest_valgrind -I gtest-1.7.0/include -L gtest-1.7.0/build -lgtest -lpthread -lgtest_main -std=c++11
Run Code Online (Sandbox Code Playgroud)
然后执行:
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./gtest_valgrind
Run Code Online (Sandbox Code Playgroud)
生成以下输出:
==17290== …
Run Code Online (Sandbox Code Playgroud) 我想知道这个消息意味着什么:
==18151== brk segment overflow in thread #1: can't grow to 0x4a26000
Run Code Online (Sandbox Code Playgroud)
请注意,代码运行正常,输出正确.我应该忽略这个消息吗?这是什么意思?
valgrind ×10
c ×5
c++ ×5
memory-leaks ×2
boost-asio ×1
c++11 ×1
gcc ×1
googletest ×1
gtk ×1
memcheck ×1
packing ×1
pthreads ×1
stdstring ×1
struct ×1