简体   繁体   English

避免GLib内存池和Valgrind可能在C中丢失

[英]Avoiding GLib memory pools & Valgrind possibly lost in C

Due to memory pools (g_slice) , I get possibly lost in my code. 由于内存池 (g_slice) ,我的代码possibly lost了。 My question is: is there anything I could do in my code to avoid leaking or is this purely a GLib issue? 我的问题是:我可以在代码中做些什么来避免泄漏,或者这纯粹是GLib问题?

All of these are reported as 'possibly lost'. 所有这些都被报告为“可能丢失”。

==2552== 
==2552== 744 bytes in 3 blocks are possibly lost in loss record 6 of 8
==2552==    at 0x40235BE: memalign (vg_replace_malloc.c:694)
==2552==    by 0x402361B: posix_memalign (vg_replace_malloc.c:835)
==2552==    by 0x408693E: ??? (in /usr/lib/libglib-2.0.so.0.1600.6)
==2552==    by 0x4088112: g_slice_alloc (in /usr/lib/libglib-2.0.so.0.1600.6)
==2552==    by 0x405B503: ??? (in /usr/lib/libglib-2.0.so.0.1600.6)
==2552==    by 0x804876C: add_inv (in /home/user/a.out)
==2552==    by 0x8048818: main (in /home/user/a.out)

#include <glib.h>
static GHashTable *hashtable1;
static GHashTable *hashtable2;

int add_inv (char *a, char *b) {
  GHashTable *table = NULL;
  gpointer old_value;
  char *mykey = NULL;
  int i, plus, *pointer;

  for (i = 0; i < 2; i++)
    {
      if (i == 0)
        {
          table = hashtable1;
          mykey = a;
        }
      else if (i == 1)
        {
          table = hashtable2;
          mykey = b;
        }
      old_value = g_hash_table_lookup (table, mykey);
      if (old_value != NULL)
        {
          pointer = (int *) old_value;
          plus = *pointer + 10;
        }
      else
        plus = 10;

      pointer = g_malloc (sizeof (int));
      *pointer = plus;
      g_hash_table_replace (table, g_strdup (mykey), pointer);
    }
}

int main () {
  int i;
  hashtable1 = g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify) g_free, g_free);
  hashtable2 = g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify) g_free, g_free);

  for (i = 0; i < 20; i++)
    {
      char *a = g_strdup ("val1");
      char *b = g_strdup ("val2");
      add_inv (a, b);
      g_free (a);
      g_free (b);
    }
  g_hash_table_destroy (hashtable1);
  g_hash_table_destroy (hashtable2);
  return 0;
}

Set G_SLICE environment variable to reconfigure the GSlice memory allocator. 设置G_SLICE环境变量以重新配置GSlice内存分配器。

G_SLICE=always-malloc ./your_application

Here is the related part of the GLib documentation . 这是GLib文档的相关部分。

This will cause all slices allocated through g_slice_alloc() and released by g_slice_free1() to be actually allocated via direct calls to g_malloc() and g_free(). 这将导致通过g_slice_alloc()分配并由g_slice_free1()释放的所有条带实际上通过直接调用g_malloc()和g_free()进行分配。 This is most useful for memory checkers and similar programs that use Bohem GC alike algorithms to produce more accurate results. 这对于使用Bohem GC类似算法的内存检查器和类似程序最有用,以产生更准确的结果。 It can also be in conjunction with debugging features of the system's malloc implementation such as glibc's MALLOC_CHECK_=2 to debug erroneous slice allocation code, allthough debug-blocks usually is a better suited debugging tool. 它也可以与系统的malloc实现的调试功能(例如glibc的MALLOC_CHECK_ = 2)结合使用,以调试错误的分片分配代码,尽管debug-blocks通常是更适合的调试工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM