[gnome-db] GDA 0.2.96 Memory leaks



Hi,

To learn GDA's API, I read gda-test.c but I found many memory leaks :

--- gda-test.c  Mon May  6 23:13:11 2002
+++ gda-test-fix.c      Mon May  6 23:13:11 2002
@@ -148,9 +148,12 @@
        for (gda_recordset_move_first(rs); !gda_recordset_eof(rs);
             gda_recordset_move_next(rs)) {
                for (i = 0; i < gda_recordset_rowsize(rs); i++) {
+                       char *s_value = NULL;
                        field = gda_recordset_field_idx(rs, i);
-                       g_print("%s=%s\t", gda_field_get_name(field),
-                               gda_stringify_value(NULL, 0, field));
+                       s_value = gda_stringify_value(NULL, 0, field);
+                       g_print("%s=%s\t", gda_field_get_name(field),
s_value);
+                       g_free(s_value);
+                       gda_field_free(field);
                }
                g_print("\n");
        }
@@ -193,5 +196,6 @@
        list_tables(cnc);
        g_print("\nclosing connection...\n", provider);
        gda_connection_free(cnc);
+       g_free(provider);
        return (0);
 }

-------------------------------------------------------------------------

So now, I think this file isn't a good example to learn GDA.

I used memprof to find memory leaks in my code and it finds many
errors on gda_connection_execute :
- maybe I don't use correctly GDA
- maybe memprof it's not the good tool

Are these following examples right ?

Connection :

gda_init("test-db", NULL, argc, argv);
        
gda_cnc = gda_connection_new(gda_corba_get_orb());
gda_connection_set_provider(gda_cnc,  
"OAFIID:GNOME_GDA_Provider_Postgres_Connection");
        
if (gda_connection_open(gda_cnc, "DATABASE=sps", "piid_user", NULL))
	die(gda_cnc);

gda_connection_free(gda_cnc);

Requests:

GdaRecordset *gda_rs;
gulong nb_cols = 0;
gulong gda_flags = 0;
        
gda_rs = gda_connection_execute(gda_cnc,
                                "SELECT * FROM table",
                                &gda_nb_lines, gda_flags);
gda_recordset_free(gda_rs);

Fields:

GdaField *field;
field = gda_recordset_field_idx(rs, i);	
gda_field_free(field);

Bye

Stephane

PS: GDA 0.8.X seems cleaner.



#include <gnome.h>
#include <gda-client.h>
#include <sys/types.h>
#include <sys/wait.h>

GdaConnection *gda_cnc;

int die(GdaConnection *gda_cnc)
{
	GList *erreurs;
	GList *noeud;
	GdaError *erreur;

	erreurs = gda_connection_get_errors(gda_cnc);
	for (noeud = g_list_first(erreurs);
	     noeud;
	     noeud = g_list_next(noeud)) {
		erreur = (GdaError *) noeud->data;
		g_print("%s\n", gda_error_get_description(erreur));
	}
	gda_error_list_free(erreurs);
	exit(1);
}

void gda_cb(GtkWidget *widget,
	    gpointer data)
{
	GdaRecordset *gda_rs;
	gulong nb_cols = 0;
	gulong gda_flags = 0;
	
	gda_rs = gda_connection_execute(gda_cnc,
					"SELECT nom AS \"Nom produit\" , \
		                                silo AS \"Numéro de silo\" \
                                        FROM produit",
					&nb_cols, gda_flags);
	gda_recordset_free(gda_rs);
	
	printf("NB %i\n", (int)nb_cols);
}

int main (int argc, char *argv[])
{
	GtkWidget *window;
	GtkWidget *button;

	gnome_init("test-db", NULL, argc, argv);
	gda_init("test-db", NULL, argc, argv);
	
	g_print("Welcome to Gnome-DB\n");

	gda_cnc = gda_connection_new(gda_corba_get_orb());
	gda_connection_set_provider(gda_cnc,
				    "OAFIID:GNOME_GDA_Provider_Postgres_Connection");
	if (gda_connection_open(gda_cnc, "DATABASE=sps", "piid_user", NULL))
		die(gda_cnc);

	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_signal_connect (GTK_OBJECT (window), "destroy",
			    GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
	gtk_signal_connect (GTK_OBJECT (window), "delete_event",
			    GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
	
	button = gtk_button_new_with_label("GDA");
	gtk_signal_connect (GTK_OBJECT (button), "clicked",
			    GTK_SIGNAL_FUNC (gda_cb), NULL);
	gtk_container_add(GTK_CONTAINER (window), button);
	gtk_widget_show_all(window);
	
	gtk_main();

	gda_connection_free(gda_cnc);

	puts("END");

        return 0;
}


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