wxWidgets Logo

Docs/technote

Tutorials
FAQ
Licence

The use of wxASSERT in wxWidgets 2


Delivered-To: wxwin-developers@wx.dent.med.uni-muenchen.de
Date: Mon, 25 May 1998 14:31:37 +0200 (MET DST)
From: Vadim Zeitlin 
To: wxwin-developers 
Subject: wxASSERT

 Hello all,

 I'd like to draw the attention of all the people who are writing code
for wxWidgets (and I think that with the availability of it's sources
on CVS there will be now more of persons who modify it directly, without
involving Julian or Robert) that it now has a "standard" set of debugging
macros in the file . These macros are:
  wxASSERT( condition ) - as assert(), disappears if __WXDEBUG__ is not defined
  wxASSERT_MSG( cond, msg ) - same, but also prints or shows in MsgBox msg
  wxFAIL - just the same as wxASSERT(0), also disappears in release version
  wxFAIL_MSG( msg ) - ...

There are also CHECK() macros which stay in release version as well, except
that, of course, wxFAIL() then does nothing:
  wxCHECK( cond ) -  if ( !cond ) { wxFAIL; return; }
  wxCHECK2( cond, op) - if ( !cond ) { wxFAIL; op; }
which are particularly useful for checking function arguments: for example,
if you have
        void Foo(wxWindow *pWin) {
          wxASSERT( pWin != NULL );
          pWin->SetTitle("Foo");
        }
the wxASSERT is useless, while replacing it with wxCHECK allows both find
the bugs in debug mode (and you may continue execution after wxFAIL) and
also keep program alive in the release version.

 If I allow myself to bring these macros to your attention, it's only
because I've done some MFC related work this week-end and stepping through
it's code I've seen (not for the first time) that there are a *lot* of
ASSERT()s, VERIFY()s (== wxCHECK) &c in it's code which is enourmously
helpful and costs exactly nothing in releease builds.

 As an example of a situation where it might have helped: in wxMSW,
if the creation of a window fails, there is no way to know about it
(except that it doesn't appears on the screen). Moreover, the internal
data structures get corrupted because a NULL pointer to wxWindow is added
to the window list, leading to all kinds of beautiful (and strange) 
problems... Would there be a wxCHECK( pWindow != NULL ) in the function
which adds a "Window *" to the list, it would never happen.

 Of course, if there were 
        if ( m_hwnd == 0 )
          wxLogSysError("Window '%s' creation failed", name);
it would be even more useful (wxLogSysError also adds the code and
description of the last system error using errno under Unix and
GetLastError() under Windows)...

 I'm sorry for a long message and thank you for reading till the end
of it, but I really had to say it. I understand that it's a bit late,
but I sincerely believe that *massive* usage of wxASSERT()s will help
us to solve many problems even before they're detected by wxWidgets
users.

 Thanks again for reading!
VZ

 
 

Quick Links

 
  Tutorials
Mailing Lists
Latest Docs

Community

wxForum
wxCommunity
Applications
Add-ons
wxWiki
 
 
 
 

wx Solutions

 
   
 
 
 

wxWidgets Book

 
 

Buy it From:
Amazon.com
Amazon.co.uk

 
 
 
 

Donate Now

 
 
 
 
SourceForge.net Logo

Site design and update scripts by Kevin Ollivier, with special thanks to Brad Anderson for his improvements to the sidebar, intro table and navbar designs, Bryan Petty for the new wxWidgets blocks graphics and logo text, and to the wxWidgets community for all their helpful suggestions, comments and testing!