Glib segfault g_free 哈希表

发布于 2024-08-25 07:52:41 字数 769 浏览 11 评论 0原文

我不太清楚为什么如果我尝试释放数据会出现段错误。任何帮助将不胜感激。

struct mystu {
  char *q;
};

static GHashTable *hashtable;

static void add_inv(char *q)
{
    gpointer old_key, old_value;

    if(!g_hash_table_lookup_extended(hashtable, q, &old_key, &old_value)){
        g_hash_table_insert(hashtable, g_strdup(q), GINT_TO_POINTER(10));
    }else{
        (old_value)++;
        g_hash_table_insert(hashtable, g_strdup(q), old_value);
        g_hash_table_remove (hashtable, q); // segfault
        g_free(old_key);   // segfault
        g_free(old_value); // segfault
    }   
}
...
int main(int argc, char *argv[]){
  hashtable = g_hash_table_new(g_str_hash, g_str_equal);
  ...
  struct mystu stu;
  add_inv(stu.q);
  g_hash_table_destroy(hashtable);
}

I'm not quite sure why if I try to free the data I get segfault. Any help will be appreciate it.

struct mystu {
  char *q;
};

static GHashTable *hashtable;

static void add_inv(char *q)
{
    gpointer old_key, old_value;

    if(!g_hash_table_lookup_extended(hashtable, q, &old_key, &old_value)){
        g_hash_table_insert(hashtable, g_strdup(q), GINT_TO_POINTER(10));
    }else{
        (old_value)++;
        g_hash_table_insert(hashtable, g_strdup(q), old_value);
        g_hash_table_remove (hashtable, q); // segfault
        g_free(old_key);   // segfault
        g_free(old_value); // segfault
    }   
}
...
int main(int argc, char *argv[]){
  hashtable = g_hash_table_new(g_str_hash, g_str_equal);
  ...
  struct mystu stu;
  add_inv(stu.q);
  g_hash_table_destroy(hashtable);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹苦笑 2024-09-01 07:52:41

在这个示例中,您已经展示了无休止的段错误之战,您没有为变量 q 分配或新建内存...由于某种原因,您跳过了显示代码main 函数中的 add_inv...线索就在指向 char 的指针上,即 q,它有 mallocd 内存...

您是否尝试过这种方式:

int main(int argc, char *argv[]){
  const char *qInit = "foo";
  char *q;
  hashtable = g_hash_table_new(g_str_hash, g_str_equal);
  ...
  q = strdup(qInit); /* Now q has memory allocated! */

  add_inv(q); /* This should work */

  g_hash_table_destroy(hashtable);
}

当您尝试取消引用尚未被 mallocd 或 newd 的内存时,会发生段错误分别取决于 C/C++...如果您释放删除尚未释放的指针或<代码>新d....

In this example that you have shown and the endless battle for segfault, you have not malloc'd or new'd the memory for the variable q...for some reason you have skipped showing the code for add_inv within your main function.... the clue is on the pointer to char i.e. q, has that got mallocd memory...

Have you tried it this way:

int main(int argc, char *argv[]){
  const char *qInit = "foo";
  char *q;
  hashtable = g_hash_table_new(g_str_hash, g_str_equal);
  ...
  q = strdup(qInit); /* Now q has memory allocated! */

  add_inv(q); /* This should work */

  g_hash_table_destroy(hashtable);
}

A seg-fault occurs when you tried to de-reference memory that has not being mallocd nor newd depending on C/C++ respectively....it can happen if you freed or deleted a pointer that has not being freed or newd....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文