Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        3年前关闭。
                                                                                            
                
        
它获取已登录系统的用户的详细信息,并打印其详细信息并将其存储在合适的数据结构中并最终显示。
这是我编写的代码。输出正确,但是我在valgrind检查中“绝对丢失”和“间接丢失”。
尝试了很多检查泄漏。
谁能帮助我清除它们。如果他们在编写prgoram本身时告诉他们如何避免它,这将很有用。
包含所有必需的头文件

void disp(gpointer key, gpointer value, gpointer userdata)
{
printf("%-10s",(char*)key);
g_slist_foreach((GSList*)value, (GFunc)displ, NULL);
printf("\n");
g_free(key);
g_slist_free((GSList*)value);
}

void displ(gpointer value, gpointer userdata)
{
printf("%-25s ", (char *)value);
g_free(value);
}

int main(int argc, char *argv[])
{
if(argc > 1)
{
    printf("Too many input arguments. \n");
    exit(1);
}

GHashTable* hash = NULL;
hash = g_hash_table_new(g_str_hash, g_str_equal);
GSList* list = NULL;

struct utmpx *ret = NULL;
struct passwd *uentry = NULL;
char *id = NULL;
char *name = NULL;
char *hostid = NULL;

while((ret = getutxent())!= NULL)
{
    if(USER_PROCESS == ret->ut_type)
    {
        /*fetch userid for this user*/
        uentry = getpwnam(ret->ut_user);
        if (NULL == uentry)
        {
            printf("No user found. \n");
            return 0;
        }
        else
        {
            /* Create new list for each user */
            list = NULL;
            id = strdup(ret->ut_user);
            name = strdup(uentry->pw_gecos);
            hostid = strdup(ret->ut_host);
            list = g_slist_append(list, name);
            list = g_slist_append(list,hostid);

            g_hash_table_insert(hash, id, list);
                        }
    }
}
/* printing the details of respective users */
g_hash_table_foreach(hash, (GHFunc)disp, NULL);
/*freeing memory created for Hash Table */
g_hash_table_destroy(hash);
return 0;
}

最佳答案

为了释放Glib使用的所有内存,您必须使用它的特定功能来执行此操作,因为Glib使用引用计数方法来释放对象。我按照以下方式更改了代码,所有使用的内存都已成功释放。

#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <utmpx.h>
#include <pwd.h>
#include <sys/types.h>

void displ(gpointer value, gpointer userdata)
{
    printf("%-25s ", (char *) value);
}

void dell(gpointer value)
{
    g_free(value);
}

void del_list(gpointer value)
{
    g_slist_free_full((GSList*)value, (GDestroyNotify)dell);
}

int disp_del(gpointer key, gpointer value, gpointer userdata)
{
    printf("%-10s",(char*)key);
    g_slist_foreach((GSList*)value, (GFunc)displ, NULL);
    printf("\n");
    return TRUE;
}

int main(int argc, char *argv[]) {
    if (argc > 1) {
        printf("Too many input arguments. \n");
        exit(1);
    }

    GHashTable* hash = NULL;
    hash = g_hash_table_new_full(g_str_hash, g_str_equal, (GDestroyNotify)g_free, (GDestroyNotify)del_list);
    GSList* list = NULL;

    struct utmpx *ret = NULL;
    struct passwd *uentry = NULL;
    char *id = NULL;
    char *name = NULL;
    char *hostid = NULL;

    while ((ret = getutxent())!= NULL) {
        if(USER_PROCESS == ret->ut_type) {
            /* fetch userid for this user */
            uentry = getpwnam(ret->ut_user);
            if (NULL == uentry) {
                printf("No user found. \n");
                return 0;
            } else {
                /* Create new list for each user */
                list = NULL;
                id = strdup(ret->ut_user);
                name = strdup(uentry->pw_gecos);
                hostid = strdup(ret->ut_host);
                list = g_slist_append(list, name);
                list = g_slist_append(list, hostid);
                g_hash_table_insert(hash, id, list);
            }
        }
    }
    /* printing the details of respective users */
    g_hash_table_foreach_remove(hash, (GHRFunc)disp_del, NULL);
    /*freeing memory created for Hash Table */
    g_hash_table_remove_all(hash);
    g_hash_table_destroy(hash);
    return 0;
}

关于c - 使用GLib软件包模拟linux命令“谁”的行为-Valgrind错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35424355/

10-11 22:43
查看更多