[Xfce4-commits] <xfce4-cpugraph-plugin:master> Fix memory allocation and freeing
Florian
noreply at xfce.org
Sat Apr 17 16:16:06 CEST 2010
Updating branch refs/heads/master
to 3ff0c4636dc13a4db5f14d5fef3a4da51ce96f58 (commit)
from c377173807992a57510f22e4e2c7213f4ce6e504 (commit)
commit 3ff0c4636dc13a4db5f14d5fef3a4da51ce96f58
Author: Florian Rivoal <frivoal at gmail.com>
Date: Sun Feb 7 14:54:31 2010 +0900
Fix memory allocation and freeing
There was an unsafe mix of plain malloc/free and of their g_* variants,
as well as unecessary checks for NULL before freeing, inconsistancy in
checking or not checking for NULL after malloc, and a minor memory leak
on shutdown. Clean up all that.
This fixes bug 4984.
panel-plugin/cpu.c | 9 ++++-----
panel-plugin/os.c | 18 +++++-------------
2 files changed, 9 insertions(+), 18 deletions(-)
diff --git a/panel-plugin/cpu.c b/panel-plugin/cpu.c
index 377c5dd..60e0e88 100644
--- a/panel-plugin/cpu.c
+++ b/panel-plugin/cpu.c
@@ -40,14 +40,14 @@ void Kill( XfcePanelPlugin * plugin, CPUGraph * base )
for(i=0; i<base->nrCores-1; i++)
gtk_widget_destroy(base->m_pBar[i]);
+ g_free( base->m_pBar );
gtk_widget_destroy(base->m_Box);
if( base->m_TimeoutID )
g_source_remove( base->m_TimeoutID );
- if( base->m_History )
- g_free( base->m_History );
+ g_free( base->m_History );
g_object_unref( base->m_Tooltip );
@@ -240,7 +240,7 @@ CPUGraph * CreateControl( XfcePanelPlugin * plugin )
if((base->nrCores = cpuData_init() - 1) < 0)
fprintf(stderr,"Cannot init cpu data !\n");
- base->m_pBar = (GtkWidget **) malloc(sizeof(GtkWidget *) * base->nrCores);
+ base->m_pBar = (GtkWidget **) g_malloc( sizeof( GtkWidget * ) * base->nrCores );
for(i=0; i<base->nrCores; i++) {
base->m_pBar[i] = GTK_WIDGET(gtk_progress_bar_new());
@@ -898,7 +898,7 @@ void ChangeColor( int color, CPUGraph * base )
void SetHistorySize( CPUGraph * base, int size )
{
int i;
- base->m_History = (long *) realloc( base->m_History, 2 * size * sizeof( long ) );
+ base->m_History = (long *) g_realloc( base->m_History, 2 * size * sizeof( long ) );
base->m_CpuData = cpuData_read();
base->m_CpuData[0].pUsed = 0;
@@ -910,7 +910,6 @@ void SetHistorySize( CPUGraph * base, int size )
base->m_History[i+size] = base->m_CpuData[0].scalCurFreq;
}
base->m_Values = size;
-
}
void ModeChange( GtkOptionMenu * om, CPUGraph * base )
diff --git a/panel-plugin/os.c b/panel-plugin/os.c
index c063778..fefd5a3 100644
--- a/panel-plugin/os.c
+++ b/panel-plugin/os.c
@@ -18,7 +18,7 @@ int nrCpus = 0;
void cpuData_free()
{
- free( cpudata );
+ g_free( cpudata );
cpudata = NULL;
nrCpus = 0;
}
@@ -46,9 +46,7 @@ int cpuData_init()
}
while( strncmp( cpuStr, "cpu", 3 ) == 0 );
/* Alloc storage for cpu data stuff */
- cpudata = (CpuData *) calloc( cpuNr, sizeof( CpuData ) );
- if( cpudata == NULL )
- return-3;
+ cpudata = (CpuData *) g_malloc0( cpuNr * sizeof( CpuData ) );
/* init frequency */
for( i=cpuNr-1; i>=0; i-- )
@@ -119,9 +117,7 @@ void cpuData_init()
cpuNr = 1;
/* Alloc storage for cpu data stuff */
- cpudata = (CpuData *) calloc( cpuNr, sizeof( CpuData ) );
- if( cpudata == NULL )
- return -3;
+ cpudata = (CpuData *) g_malloc0( cpuNr * sizeof( CpuData ) );
/* init frequency */
for( i=cpuNr-1; i>=0; i-- )
@@ -181,9 +177,7 @@ void cpuData_init()
cpuNr = 1;
/* Alloc storage for cpu data stuff */
- cpudata = (CpuData *) calloc( cpuNr, sizeof( CpuData ) );
- if( cpudata == NULL )
- return -3;
+ cpudata = (CpuData *) g_malloc0( cpuNr * sizeof( CpuData ) );
/* init frequency */
for( i=cpuNr-1; i>=0; i-- )
@@ -244,9 +238,7 @@ void cpuData_init()
cpuNr = 1;
/* Alloc storage for cpu data stuff */
- cpudata = (CpuData *) calloc( cpuNr, sizeof( CpuData ) );
- if( cpudata == NULL )
- return -3;
+ cpudata = (CpuData *) g_malloc0( cpuNr, sizeof( CpuData ) );
/* init frequency */
for( i=cpuNr-1; i>=0; i-- )
More information about the Xfce4-commits
mailing list