improved memory liberation (explicitly set to NULL after delete)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@15933 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Gilles Depeyrot
2002-06-23 17:22:54 +00:00
parent d208e6412a
commit f5bb225133
20 changed files with 140 additions and 80 deletions

View File

@@ -104,16 +104,19 @@ BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice)
EVT_CHOICE(-1, wxComboBoxChoice::OnChoice) EVT_CHOICE(-1, wxComboBoxChoice::OnChoice)
END_EVENT_TABLE() END_EVENT_TABLE()
wxComboBox::~wxComboBox() wxComboBox::~wxComboBox()
{ {
// delete the controls now, don't leave them alive even though they woudl // delete the controls now, don't leave them alive even though they would
// still be eventually deleted by our parent - but it will be too late, the // still be eventually deleted by our parent - but it will be too late, the
// user code expects them to be gone now // user code expects them to be gone now
delete m_text; if (m_text != NULL) {
delete m_choice; delete m_text;
m_text = NULL;
}
if (m_choice != NULL) {
delete m_choice;
m_choice = NULL;
}
} }

View File

@@ -253,7 +253,10 @@ wxString wxDir::GetName() const
wxDir::~wxDir() wxDir::~wxDir()
{ {
delete M_DIR; if (M_DIR != NULL) {
delete M_DIR;
m_data = NULL;
}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -141,8 +141,10 @@ wxGLCanvas::wxGLCanvas( wxWindow *parent, const wxGLCanvas *shared, wxWindowID i
wxGLCanvas::~wxGLCanvas() wxGLCanvas::~wxGLCanvas()
{ {
if (m_glContext) if (m_glContext != NULL) {
delete m_glContext; delete m_glContext;
m_glContext = NULL;
}
} }
static AGLPixelFormat ChoosePixelFormat(const int *attribList) static AGLPixelFormat ChoosePixelFormat(const int *attribList)

View File

@@ -265,6 +265,7 @@ void wxListBox::Free()
size_t uiCount = m_aItems.Count(); size_t uiCount = m_aItems.Count();
while ( uiCount-- != 0 ) { while ( uiCount-- != 0 ) {
delete m_aItems[uiCount]; delete m_aItems[uiCount];
m_aItems[uiCount] = NULL;
} }
m_aItems.Clear(); m_aItems.Clear();
@@ -373,6 +374,7 @@ void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
size_t ui = m_aItems.Count(); size_t ui = m_aItems.Count();
while ( ui-- != 0 ) { while ( ui-- != 0 ) {
delete m_aItems[ui]; delete m_aItems[ui];
m_aItems[ui] = NULL;
} }
m_aItems.Empty(); m_aItems.Empty();

View File

@@ -36,7 +36,10 @@ wxPaletteRefData::wxPaletteRefData()
wxPaletteRefData::~wxPaletteRefData() wxPaletteRefData::~wxPaletteRefData()
{ {
delete[] m_palette ; if (m_palette != NULL) {
delete[] m_palette ;
m_palette = NULL;
}
} }
wxPalette::wxPalette() wxPalette::wxPalette()

View File

@@ -111,12 +111,11 @@ wxPNGReader::Create(int width, int height, int depth, int colortype)
Width = width; Height = height; Depth = depth; Width = width; Height = height; Depth = depth;
ColorType = (colortype>=0) ? colortype: ((Depth>8) ? COLORTYPE_COLOR: 0); ColorType = (colortype>=0) ? colortype: ((Depth>8) ? COLORTYPE_COLOR: 0);
delete m_palette; delete m_palette;
delete[] RawImage ; m_palette = NULL;
RawImage = 0; delete[] RawImage;
m_palette = 0; RawImage = NULL;
if (lpbi) if (lpbi) {
{
wxMacDestroyGWorld( (GWorldPtr) lpbi ) ; wxMacDestroyGWorld( (GWorldPtr) lpbi ) ;
} }
lpbi = wxMacCreateGWorld( Width , Height , Depth); lpbi = wxMacCreateGWorld( Width , Height , Depth);
@@ -134,11 +133,18 @@ wxPNGReader::Create(int width, int height, int depth, int colortype)
wxPNGReader::~wxPNGReader ( ) wxPNGReader::~wxPNGReader ( )
{ {
delete[] RawImage ; if (RawImage != NULL) {
if (lpbi) { delete[] RawImage ;
RawImage = NULL;
}
if (lpbi) {
wxMacDestroyGWorld( (GWorldPtr) lpbi ) ; wxMacDestroyGWorld( (GWorldPtr) lpbi ) ;
} lpbi = NULL;
delete m_palette; }
if (m_palette != NULL) {
delete m_palette;
m_palette = NULL;
}
} }
@@ -260,10 +266,12 @@ void wxPNGReader::NullData()
{ {
if (lpbi) { if (lpbi) {
wxMacDestroyGWorld( (GWorldPtr) lpbi ) ; wxMacDestroyGWorld( (GWorldPtr) lpbi ) ;
lpbi = NULL;
}
if (m_palette != NULL) {
delete m_palette;
m_palette = NULL;
} }
delete m_palette;
lpbi = NULL;
m_palette = NULL;
} }
wxBitmap* wxPNGReader::GetBitmap(void) wxBitmap* wxPNGReader::GetBitmap(void)
@@ -432,7 +440,7 @@ bool wxPNGReader::ReadFile(char * ImageFileName)
if (!info_ptr) if (!info_ptr)
{ {
fclose(fp); fclose(fp);
delete(png_ptr); delete png_ptr;
return FALSE; return FALSE;
} }
/* set error handling */ /* set error handling */
@@ -440,8 +448,8 @@ bool wxPNGReader::ReadFile(char * ImageFileName)
{ {
png_read_destroy(png_ptr, info_ptr, (png_info *)0); png_read_destroy(png_ptr, info_ptr, (png_info *)0);
fclose(fp); fclose(fp);
delete(png_ptr); delete png_ptr;
delete(info_ptr); delete info_ptr;
/* If we get here, we had a problem reading the file */ /* If we get here, we had a problem reading the file */
return FALSE; return FALSE;
@@ -623,8 +631,8 @@ bool wxPNGReader::ReadFile(char * ImageFileName)
png_read_destroy(png_ptr, info_ptr, (png_info *)0); png_read_destroy(png_ptr, info_ptr, (png_info *)0);
/* free the structures */ /* free the structures */
delete(png_ptr); delete png_ptr;
delete(info_ptr); delete info_ptr;
/* close the file */ /* close the file */
fclose(fp); fclose(fp);
@@ -663,7 +671,7 @@ bool wxPNGReader::SaveFile(char * ImageFileName)
if (!info_ptr) if (!info_ptr)
{ {
fclose(fp); fclose(fp);
delete(png_ptr); delete png_ptr;
return FALSE; return FALSE;
} }
@@ -672,8 +680,8 @@ bool wxPNGReader::SaveFile(char * ImageFileName)
{ {
png_write_destroy(png_ptr); png_write_destroy(png_ptr);
fclose(fp); fclose(fp);
delete(png_ptr); delete png_ptr;
delete(info_ptr); delete info_ptr;
/* If we get here, we had a problem reading the file */ /* If we get here, we had a problem reading the file */
return FALSE; return FALSE;
@@ -770,8 +778,8 @@ bool wxPNGReader::SaveFile(char * ImageFileName)
delete[] (info_ptr->palette); delete[] (info_ptr->palette);
/* free the structures */ /* free the structures */
delete(png_ptr); delete png_ptr;
delete(info_ptr); delete info_ptr;
/* close the file */ /* close the file */
fclose(fp); fclose(fp);

View File

@@ -71,8 +71,10 @@ bool wxPrintDialog::Create(wxWindow *p, wxPrintDialogData* data)
wxPrintDialog::~wxPrintDialog() wxPrintDialog::~wxPrintDialog()
{ {
if (m_destroyDC && m_printerDC) if (m_destroyDC && m_printerDC) {
delete m_printerDC; delete m_printerDC;
m_printerDC = NULL;
}
} }
int wxPrintDialog::ShowModal() int wxPrintDialog::ShowModal()

View File

@@ -99,7 +99,7 @@ public:
{ {
if ( m_locked > 0 ) if ( m_locked > 0 )
{ {
wxLogDebug(_T("Warning: freeing a locked mutex (%d locks)."), m_locked); wxLogDebug(_T("Warning: freeing a locked mutex (%ld locks)."), m_locked);
} }
} }
@@ -576,7 +576,10 @@ wxThread::wxThread(wxThreadKind kind)
wxThread::~wxThread() wxThread::~wxThread()
{ {
s_threads.Remove( (void*) this ) ; s_threads.Remove( (void*) this ) ;
delete m_internal; if (m_internal != NULL) {
delete m_internal;
m_internal = NULL;
}
} }
// create/start thread // create/start thread

View File

@@ -92,8 +92,10 @@ bool wxTimer::IsRunning() const
wxTimer::~wxTimer() wxTimer::~wxTimer()
{ {
Stop(); Stop();
delete m_info ; if (m_info != NULL) {
m_info = NULL ; delete m_info ;
m_info = NULL ;
}
int index = gTimersInProcess.Index( this ) ; int index = gTimersInProcess.Index( this ) ;
if ( index != wxNOT_FOUND ) if ( index != wxNOT_FOUND )
gTimersInProcess.RemoveAt( index ) ; gTimersInProcess.RemoveAt( index ) ;

View File

@@ -201,10 +201,12 @@ void wxMacToolTip::Setup( WindowRef win , wxString text , wxPoint localPosition
wxMacToolTip::~wxMacToolTip() wxMacToolTip::~wxMacToolTip()
{ {
if ( m_timer ) if ( m_timer ) {
delete m_timer ; delete m_timer ;
if ( m_backpict ) m_timer = NULL;
Clear() ; }
if ( m_backpict )
Clear() ;
} }
const short kTipBorder = 2 ; const short kTipBorder = 2 ;

View File

@@ -104,16 +104,19 @@ BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice)
EVT_CHOICE(-1, wxComboBoxChoice::OnChoice) EVT_CHOICE(-1, wxComboBoxChoice::OnChoice)
END_EVENT_TABLE() END_EVENT_TABLE()
wxComboBox::~wxComboBox() wxComboBox::~wxComboBox()
{ {
// delete the controls now, don't leave them alive even though they woudl // delete the controls now, don't leave them alive even though they would
// still be eventually deleted by our parent - but it will be too late, the // still be eventually deleted by our parent - but it will be too late, the
// user code expects them to be gone now // user code expects them to be gone now
delete m_text; if (m_text != NULL) {
delete m_choice; delete m_text;
m_text = NULL;
}
if (m_choice != NULL) {
delete m_choice;
m_choice = NULL;
}
} }

View File

@@ -253,7 +253,10 @@ wxString wxDir::GetName() const
wxDir::~wxDir() wxDir::~wxDir()
{ {
delete M_DIR; if (M_DIR != NULL) {
delete M_DIR;
m_data = NULL;
}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -141,8 +141,10 @@ wxGLCanvas::wxGLCanvas( wxWindow *parent, const wxGLCanvas *shared, wxWindowID i
wxGLCanvas::~wxGLCanvas() wxGLCanvas::~wxGLCanvas()
{ {
if (m_glContext) if (m_glContext != NULL) {
delete m_glContext; delete m_glContext;
m_glContext = NULL;
}
} }
static AGLPixelFormat ChoosePixelFormat(const int *attribList) static AGLPixelFormat ChoosePixelFormat(const int *attribList)

View File

@@ -265,6 +265,7 @@ void wxListBox::Free()
size_t uiCount = m_aItems.Count(); size_t uiCount = m_aItems.Count();
while ( uiCount-- != 0 ) { while ( uiCount-- != 0 ) {
delete m_aItems[uiCount]; delete m_aItems[uiCount];
m_aItems[uiCount] = NULL;
} }
m_aItems.Clear(); m_aItems.Clear();
@@ -373,6 +374,7 @@ void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
size_t ui = m_aItems.Count(); size_t ui = m_aItems.Count();
while ( ui-- != 0 ) { while ( ui-- != 0 ) {
delete m_aItems[ui]; delete m_aItems[ui];
m_aItems[ui] = NULL;
} }
m_aItems.Empty(); m_aItems.Empty();

View File

@@ -36,7 +36,10 @@ wxPaletteRefData::wxPaletteRefData()
wxPaletteRefData::~wxPaletteRefData() wxPaletteRefData::~wxPaletteRefData()
{ {
delete[] m_palette ; if (m_palette != NULL) {
delete[] m_palette ;
m_palette = NULL;
}
} }
wxPalette::wxPalette() wxPalette::wxPalette()

View File

@@ -111,12 +111,11 @@ wxPNGReader::Create(int width, int height, int depth, int colortype)
Width = width; Height = height; Depth = depth; Width = width; Height = height; Depth = depth;
ColorType = (colortype>=0) ? colortype: ((Depth>8) ? COLORTYPE_COLOR: 0); ColorType = (colortype>=0) ? colortype: ((Depth>8) ? COLORTYPE_COLOR: 0);
delete m_palette; delete m_palette;
delete[] RawImage ; m_palette = NULL;
RawImage = 0; delete[] RawImage;
m_palette = 0; RawImage = NULL;
if (lpbi) if (lpbi) {
{
wxMacDestroyGWorld( (GWorldPtr) lpbi ) ; wxMacDestroyGWorld( (GWorldPtr) lpbi ) ;
} }
lpbi = wxMacCreateGWorld( Width , Height , Depth); lpbi = wxMacCreateGWorld( Width , Height , Depth);
@@ -134,11 +133,18 @@ wxPNGReader::Create(int width, int height, int depth, int colortype)
wxPNGReader::~wxPNGReader ( ) wxPNGReader::~wxPNGReader ( )
{ {
delete[] RawImage ; if (RawImage != NULL) {
if (lpbi) { delete[] RawImage ;
RawImage = NULL;
}
if (lpbi) {
wxMacDestroyGWorld( (GWorldPtr) lpbi ) ; wxMacDestroyGWorld( (GWorldPtr) lpbi ) ;
} lpbi = NULL;
delete m_palette; }
if (m_palette != NULL) {
delete m_palette;
m_palette = NULL;
}
} }
@@ -260,10 +266,12 @@ void wxPNGReader::NullData()
{ {
if (lpbi) { if (lpbi) {
wxMacDestroyGWorld( (GWorldPtr) lpbi ) ; wxMacDestroyGWorld( (GWorldPtr) lpbi ) ;
lpbi = NULL;
}
if (m_palette != NULL) {
delete m_palette;
m_palette = NULL;
} }
delete m_palette;
lpbi = NULL;
m_palette = NULL;
} }
wxBitmap* wxPNGReader::GetBitmap(void) wxBitmap* wxPNGReader::GetBitmap(void)
@@ -432,7 +440,7 @@ bool wxPNGReader::ReadFile(char * ImageFileName)
if (!info_ptr) if (!info_ptr)
{ {
fclose(fp); fclose(fp);
delete(png_ptr); delete png_ptr;
return FALSE; return FALSE;
} }
/* set error handling */ /* set error handling */
@@ -440,8 +448,8 @@ bool wxPNGReader::ReadFile(char * ImageFileName)
{ {
png_read_destroy(png_ptr, info_ptr, (png_info *)0); png_read_destroy(png_ptr, info_ptr, (png_info *)0);
fclose(fp); fclose(fp);
delete(png_ptr); delete png_ptr;
delete(info_ptr); delete info_ptr;
/* If we get here, we had a problem reading the file */ /* If we get here, we had a problem reading the file */
return FALSE; return FALSE;
@@ -623,8 +631,8 @@ bool wxPNGReader::ReadFile(char * ImageFileName)
png_read_destroy(png_ptr, info_ptr, (png_info *)0); png_read_destroy(png_ptr, info_ptr, (png_info *)0);
/* free the structures */ /* free the structures */
delete(png_ptr); delete png_ptr;
delete(info_ptr); delete info_ptr;
/* close the file */ /* close the file */
fclose(fp); fclose(fp);
@@ -663,7 +671,7 @@ bool wxPNGReader::SaveFile(char * ImageFileName)
if (!info_ptr) if (!info_ptr)
{ {
fclose(fp); fclose(fp);
delete(png_ptr); delete png_ptr;
return FALSE; return FALSE;
} }
@@ -672,8 +680,8 @@ bool wxPNGReader::SaveFile(char * ImageFileName)
{ {
png_write_destroy(png_ptr); png_write_destroy(png_ptr);
fclose(fp); fclose(fp);
delete(png_ptr); delete png_ptr;
delete(info_ptr); delete info_ptr;
/* If we get here, we had a problem reading the file */ /* If we get here, we had a problem reading the file */
return FALSE; return FALSE;
@@ -770,8 +778,8 @@ bool wxPNGReader::SaveFile(char * ImageFileName)
delete[] (info_ptr->palette); delete[] (info_ptr->palette);
/* free the structures */ /* free the structures */
delete(png_ptr); delete png_ptr;
delete(info_ptr); delete info_ptr;
/* close the file */ /* close the file */
fclose(fp); fclose(fp);

View File

@@ -71,8 +71,10 @@ bool wxPrintDialog::Create(wxWindow *p, wxPrintDialogData* data)
wxPrintDialog::~wxPrintDialog() wxPrintDialog::~wxPrintDialog()
{ {
if (m_destroyDC && m_printerDC) if (m_destroyDC && m_printerDC) {
delete m_printerDC; delete m_printerDC;
m_printerDC = NULL;
}
} }
int wxPrintDialog::ShowModal() int wxPrintDialog::ShowModal()

View File

@@ -99,7 +99,7 @@ public:
{ {
if ( m_locked > 0 ) if ( m_locked > 0 )
{ {
wxLogDebug(_T("Warning: freeing a locked mutex (%d locks)."), m_locked); wxLogDebug(_T("Warning: freeing a locked mutex (%ld locks)."), m_locked);
} }
} }
@@ -576,7 +576,10 @@ wxThread::wxThread(wxThreadKind kind)
wxThread::~wxThread() wxThread::~wxThread()
{ {
s_threads.Remove( (void*) this ) ; s_threads.Remove( (void*) this ) ;
delete m_internal; if (m_internal != NULL) {
delete m_internal;
m_internal = NULL;
}
} }
// create/start thread // create/start thread

View File

@@ -92,8 +92,10 @@ bool wxTimer::IsRunning() const
wxTimer::~wxTimer() wxTimer::~wxTimer()
{ {
Stop(); Stop();
delete m_info ; if (m_info != NULL) {
m_info = NULL ; delete m_info ;
m_info = NULL ;
}
int index = gTimersInProcess.Index( this ) ; int index = gTimersInProcess.Index( this ) ;
if ( index != wxNOT_FOUND ) if ( index != wxNOT_FOUND )
gTimersInProcess.RemoveAt( index ) ; gTimersInProcess.RemoveAt( index ) ;

View File

@@ -201,10 +201,12 @@ void wxMacToolTip::Setup( WindowRef win , wxString text , wxPoint localPosition
wxMacToolTip::~wxMacToolTip() wxMacToolTip::~wxMacToolTip()
{ {
if ( m_timer ) if ( m_timer ) {
delete m_timer ; delete m_timer ;
if ( m_backpict ) m_timer = NULL;
Clear() ; }
if ( m_backpict )
Clear() ;
} }
const short kTipBorder = 2 ; const short kTipBorder = 2 ;