help for problem in g_cache_insert() example code?



#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN

#include <stdio.h>
#include <string.h>
#include "glib.h"

void function(gchar *key,gchar *value,gint *user_data)
{
// give the count of the number of times the function was called

(*user_data)++;
}

void cache_test()
{
gchar *str1,*str2,*str3;
GCache *cache = NULL;
gint user_data = 0;

g_assert((cache = g_cache_new( (GCacheNewFunc) g_ascii_strup,
g_free, (GCacheDupFunc) g_strdup, g_free, g_str_hash,
g_str_hash, g_str_equal)) != NULL);
str1 = g_cache_insert(cache,"test");
g_assert(!strcmp("TEST",str1));
str2 = g_cache_insert(cache,"test");
g_assert(!strcmp("TEST",str1));
str3 = g_cache_insert(cache,"glib");
g_assert(!strcmp("GLIB",str3));
g_cache_key_foreach (cache,(GHFunc)function,&user_data);
//g_cache_key_foreach would call function twice and make user_data == 2
g_assert(user_data == 2);
g_cache_value_foreach (cache,(GHFunc)function,&user_data);
//g_cache_value_foreach would call function twice and make user_data == 4
g_assert(user_data == 4);
g_cache_remove(cache,str1);
g_cache_remove(cache,str2);
g_cache_remove(cache,str3);
g_cache_destroy(cache);
}


int main (int argc,
char *argv[])
{
cache_test();
return 0;

}

Now my test example crashes at str1 = g_cache_insert(cache,"test"); because of null pointer dereferencing.
Any help where i could be going wrong?

Regards

Biswajeet


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]