From 65da6b3cd7339c541699ce01a3be79af617eb612 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 19 Oct 2008 15:20:31 +0000 Subject: [PATCH] Memory Leak: Better checking of mismatching alloc and dealloc for gtk --- CheckMemoryLeak.cpp | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index 56cf12f00..6fa6eef88 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -43,7 +43,7 @@ static bool isclass( const std::string &typestr ) //--------------------------------------------------------------------------- -enum AllocType { No, Malloc, New, NewA }; +enum AllocType { No, Malloc, gMalloc, New, NewA }; // Extra allocation.. class AllocFunc @@ -82,18 +82,6 @@ static AllocType GetAllocationType( const TOKEN *tok2 ) "strdup", "kmalloc", "kzalloc", - "g_new", - "g_new0", - "g_renew", - "g_try_new", - "g_try_new0", - "g_try_renew", - "g_malloc", - "g_malloc0", - "g_realloc", - "g_try_malloc", - "g_try_malloc0", - "g_try_realloc", 0}; for ( unsigned int i = 0; mallocfunc[i]; i++ ) { @@ -101,6 +89,28 @@ static AllocType GetAllocationType( const TOKEN *tok2 ) return Malloc; } + // Does tok2 point on "malloc", "strdup" or "kmalloc".. + const char *gmallocfunc[] = {"g_new", + "g_new0", + "g_renew", + "g_try_new", + "g_try_new0", + "g_try_renew", + "g_malloc", + "g_malloc0", + "g_realloc", + "g_try_malloc", + "g_try_malloc0", + "g_try_realloc", + "g_strdup", + "g_strndup", + 0}; + for ( unsigned int i = 0; gmallocfunc[i]; i++ ) + { + if ( strcmp(gmallocfunc[i], tok2->str) == 0 ) + return gMalloc; + } + if ( Match( tok2, "new %type% [;(]" ) ) return New; @@ -136,12 +146,14 @@ static AllocType GetDeallocationType( const TOKEN *tok, const char *varnames[] ) return NewA; if ( Match(tok, "free ( %var1% ) ;", varnames) || - Match(tok, "kfree ( %var1% ) ;", varnames) || - Match(tok, "g_free ( %var1% ) ;", varnames) ) + Match(tok, "kfree ( %var1% ) ;", varnames) ) { return Malloc; } + if ( Match(tok, "g_free ( %var1% ) ;", varnames) ) + return gMalloc; + return No; } //---------------------------------------------------------------------------