GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Oct 02, 2014 8:25 am

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: g_assert
PostPosted: Tue Nov 27, 2007 3:24 am 
Offline
Familiar Face

Joined: Fri Sep 28, 2007 10:59 am
Posts: 46
what does g_assert function do in the following example? I saw this example in the glib library manual. whithout using g_assert function( macro?) , the following example works accurately . what does g_assert function and stderr (not define!!) expression do in the following example?


Code:
gchar *contents;
GError *err = NULL;
g_file_get_contents ("foo.txt", &contents, NULL, &err);
g_assert ((contents == NULL && err != NULL) || (contents != NULL && err == NULL));
if (err != NULL)
  {
    /* Report error to user, and free error */
    g_assert (contents == NULL);
    fprintf (stderr, "Unable to read file: %s\n", err->message);
    g_error_free (err);
  }
else
  {
    /* Use file contents */
    g_assert (contents != NULL);
  }
...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 27, 2007 4:40 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
g_assert() is used for debugging. It's typically used in areas where an expression SHOULD evaluate to true, and if it's not, then it throws out an error to the console. So, if you see this:

Code:
g_assert (value == NULL)


Then the developer is expecting "value == NULL" to be a true expression. If value is not null, then an error message will be seen. You will see this used OFTEN throughout the GTK+ and the GLib libraries. Think of it as us, the programmers, telling our code that we "assert" the following expression to be true, and to let us know otherwise.

Try to call a gtk_widget_xxx function on a widget which has not been initialized yet and before your program crashes (or just doesn't work), you'll see some assertion failure errors. Without them, all you would know is that your program isn't working. But now, you know exactly why.

When your program is ready to release, hopefully you aren't seeing any assertion errors!

So let's look at your snippet:

Code:
g_file_get_contents ("foo.txt", &contents, NULL, &err);
g_assert ((contents == NULL && err != NULL) || (contents != NULL && err == NULL));


At this point, we expect one of two conditions, contents is NULL and error is set, or contents is set and error is NULL. Otherwise, something we hadn't predicted has happened. So we're not expecting that assertion to output any error messages.

Same thing here:


Code:
    g_assert (contents == NULL);
    fprintf (stderr, "Unable to read file: %s\n", err->message);
    g_error_free (err);


When we get into the error handling block, we expect the contents to be NULL because there was an error. That's why we aren't freeing the contents. But if for some reason, it isn't NULL, we're going to spit out an error message about that freakish condition.

Now, This is a bit redundant if you ask me, because this was already checked in the previous assertion. But that's what it's doing.

So in short, removing assertions do not change how the code runs per say, but they assist in debugging critical error conditions--conditions in which the developer intends to never happen.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group