Re: g_strdup()



Hi Ignacio, you need to be more careful about where you strdup, and you 
need to look at entry_param->_file_name, not at _wrl_name.

The rule is that each instance of ParameterFile must own the pointer to 
the _file_name string. So: strdup() when you set _file_name, free when 
you unset, and absolutely NO free/strdup anywhere else.

I have a couple of macros I use to make this safer:

#define STRFREE( S ) \
   { if( S ) { g_free( S ); (S) = 0; } }

#define STRSET( S, V ) \
   { STRFREE( S ); if( V ) { (S) = g_strdup( V ); } }

so your functions become:

> ParameterFile::ParameterFile(GtkWidget* parent, gchar* param)
> {
> 
 >  _file_name = 0;
>  STRSET( _file_name, param );
>  _File_entry = 0; //It's created later
> 
>  _value_changed = 0;
> ....
> 
> }


> ParameterFile::Update()
> {
>   gchar *ch;
> 
>   ch = gtk_entry_get_text(GTK_ENTRY(_File_entry));
> 
>   _value_changed = (strcmp(_file_name, ch) != 0);
> 
>   if (_value_changed) 
 >     STRSET( _file_name, ch );
> 
> }

and in main.cpp:

 > gchar *_wrl_name = "default.wrl";
 > ParameterFile* entry_param = new ParameterFile(parent, _wrl_name);

You also need to make sure you have STRFREE( _file_name ) in your 
destructor for ParameterFile.

Finally, in your button callback, don't print _wrl_name ... that will 
always be the start value. Instead, print

   entry_param->_file_name


John



========================================================== 
Aelbert Cuyp 13 February - 12 May 2002 

For information and tickets: 
http://www.nationalgallery.org.uk



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