1

我知道在此之前有关于此问题的类似线程,并且在此站点https://live.gnome.org/Valgrind上已进行了解释,我在下面编写了我的简单程序

  #include <glib.h>
  #include <glib/gprintf.h>
  #include <iostream>

  int main()
 {
const gchar *signalfound = g_strsignal(1);
std::cout <<  signalfound<< std::endl;
return 0; 
  }

但是当我尝试使用 valgrind 使用此命令检查 G_DEBUG=gc-friendly G_SLICE=always-malloc valgrind --leak-check=full --leak-resolution=high ./g_strsignal

这是结果

   ==30274== Memcheck, a memory error detector
   ==30274== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
   ==30274== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
   ==30274== Command: ./g_strsignal
   ==30274== Parent PID: 5201
   ==30274== 
   ==30274== 
   ==30274== HEAP SUMMARY:
   ==30274==     in use at exit: 14,746 bytes in 18 blocks
   ==30274==   total heap usage: 24 allocs, 6 frees, 23,503 bytes allocated
   ==30274== 
   ==30274== LEAK SUMMARY:
   ==30274==    definitely lost: 0 bytes in 0 blocks
   ==30274==    indirectly lost: 0 bytes in 0 blocks
   ==30274==      possibly lost: 0 bytes in 0 blocks
   ==30274==    still reachable: 14,746 bytes in 18 blocks
   ==30274==         suppressed: 0 bytes in 0 blocks
   ==30274== Reachable blocks (those to which a pointer was found) are not shown.
   ==30274== To see them, rerun with: --leak-check=full --show-reachable=yes
   ==30274== 
   ==30274== For counts of detected and suppressed errors, rerun with: -v
   ==30274== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

我注意到 valgrind 所说的“未显示可到达的块(找到指针的块)。”。然后我尝试检查相应函数的 gmem.c 源代码,因为我使用了 glib-2.35.4 版本。我发现以下代码

   gpointer
   g_malloc (gsize n_bytes)
   {
      if (G_LIKELY (n_bytes))
         {
              gpointer mem;

               mem = glib_mem_vtable.malloc (n_bytes);
               TRACE (GLIB_MEM_ALLOC((void*) mem, (unsigned int) n_bytes, 0, 0));
               if (mem)
              return mem;

               g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
                G_STRLOC, n_bytes);
           }

       TRACE(GLIB_MEM_ALLOC((void*) NULL, (int) n_bytes, 0, 0));

   return NULL;
  }

我的问题是

  1. 这仍然是 valgrind 所说的“未显示可到达块(找到指针的块)的正常情况。”,我认为这个语句是指上面的 g_malloc 函数,其中返回 mem 一个 gpointer 变量?

  2. 如果没有,是否有任何替代方案可以解决,“仍然可达:18 个块中的 14,746 个字节”关于 valgrind 上面所说的内容?

我正在运行 x86 fedora 18 谢谢

4

1 回答 1

1

它很可能是指函数返回的动态分配的内存g_strsignal()
valgrind 说"Reachable blocks....",因为有效的指针(signalfound)仍然指向动态分配的内存。
如果 Valgrind 发现指向动态内存的指针丢失(被覆盖),那么它会报告“确定的泄漏......”,因为它可以肯定地说永远无法释放动态内存块。在您的情况下,指针仍然指向块 valgrind 不会假设它丢失了,但它假设它可能是设计使然。

于 2013-01-28T13:44:32.257 回答