From f5bb2251332040fd8ac7adb9406ea2516a077ed6 Mon Sep 17 00:00:00 2001 From: Gilles Depeyrot Date: Sun, 23 Jun 2002 17:22:54 +0000 Subject: [PATCH] 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 --- src/mac/carbon/combobox.cpp | 15 ++++++----- src/mac/carbon/dirmac.cpp | 5 +++- src/mac/carbon/glcanvas.cpp | 6 +++-- src/mac/carbon/listbox.cpp | 2 ++ src/mac/carbon/palette.cpp | 5 +++- src/mac/carbon/pnghand.cpp | 52 +++++++++++++++++++++---------------- src/mac/carbon/printdlg.cpp | 4 ++- src/mac/carbon/thread.cpp | 7 +++-- src/mac/carbon/timer.cpp | 6 +++-- src/mac/carbon/tooltip.cpp | 8 +++--- src/mac/combobox.cpp | 15 ++++++----- src/mac/dirmac.cpp | 5 +++- src/mac/glcanvas.cpp | 6 +++-- src/mac/listbox.cpp | 2 ++ src/mac/palette.cpp | 5 +++- src/mac/pnghand.cpp | 52 +++++++++++++++++++++---------------- src/mac/printdlg.cpp | 4 ++- src/mac/thread.cpp | 7 +++-- src/mac/timer.cpp | 6 +++-- src/mac/tooltip.cpp | 8 +++--- 20 files changed, 140 insertions(+), 80 deletions(-) diff --git a/src/mac/carbon/combobox.cpp b/src/mac/carbon/combobox.cpp index db47c86aaa..e67e1d9601 100644 --- a/src/mac/carbon/combobox.cpp +++ b/src/mac/carbon/combobox.cpp @@ -104,16 +104,19 @@ BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice) EVT_CHOICE(-1, wxComboBoxChoice::OnChoice) END_EVENT_TABLE() - - - 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 // user code expects them to be gone now - delete m_text; - delete m_choice; + if (m_text != NULL) { + delete m_text; + m_text = NULL; + } + if (m_choice != NULL) { + delete m_choice; + m_choice = NULL; + } } diff --git a/src/mac/carbon/dirmac.cpp b/src/mac/carbon/dirmac.cpp index 1acff5b7ca..729be9473d 100644 --- a/src/mac/carbon/dirmac.cpp +++ b/src/mac/carbon/dirmac.cpp @@ -253,7 +253,10 @@ wxString wxDir::GetName() const wxDir::~wxDir() { - delete M_DIR; + if (M_DIR != NULL) { + delete M_DIR; + m_data = NULL; + } } // ---------------------------------------------------------------------------- diff --git a/src/mac/carbon/glcanvas.cpp b/src/mac/carbon/glcanvas.cpp index 62d9c307c4..16e47d4c79 100644 --- a/src/mac/carbon/glcanvas.cpp +++ b/src/mac/carbon/glcanvas.cpp @@ -141,8 +141,10 @@ wxGLCanvas::wxGLCanvas( wxWindow *parent, const wxGLCanvas *shared, wxWindowID i wxGLCanvas::~wxGLCanvas() { - if (m_glContext) - delete m_glContext; + if (m_glContext != NULL) { + delete m_glContext; + m_glContext = NULL; + } } static AGLPixelFormat ChoosePixelFormat(const int *attribList) diff --git a/src/mac/carbon/listbox.cpp b/src/mac/carbon/listbox.cpp index 2d8471f00e..2d9a8708d1 100644 --- a/src/mac/carbon/listbox.cpp +++ b/src/mac/carbon/listbox.cpp @@ -265,6 +265,7 @@ void wxListBox::Free() size_t uiCount = m_aItems.Count(); while ( uiCount-- != 0 ) { delete m_aItems[uiCount]; + m_aItems[uiCount] = NULL; } m_aItems.Clear(); @@ -373,6 +374,7 @@ void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData) size_t ui = m_aItems.Count(); while ( ui-- != 0 ) { delete m_aItems[ui]; + m_aItems[ui] = NULL; } m_aItems.Empty(); diff --git a/src/mac/carbon/palette.cpp b/src/mac/carbon/palette.cpp index 1c18196135..c847131f40 100644 --- a/src/mac/carbon/palette.cpp +++ b/src/mac/carbon/palette.cpp @@ -36,7 +36,10 @@ wxPaletteRefData::wxPaletteRefData() wxPaletteRefData::~wxPaletteRefData() { - delete[] m_palette ; + if (m_palette != NULL) { + delete[] m_palette ; + m_palette = NULL; + } } wxPalette::wxPalette() diff --git a/src/mac/carbon/pnghand.cpp b/src/mac/carbon/pnghand.cpp index 1ebf612f17..3d2690803c 100644 --- a/src/mac/carbon/pnghand.cpp +++ b/src/mac/carbon/pnghand.cpp @@ -111,12 +111,11 @@ wxPNGReader::Create(int width, int height, int depth, int colortype) Width = width; Height = height; Depth = depth; ColorType = (colortype>=0) ? colortype: ((Depth>8) ? COLORTYPE_COLOR: 0); delete m_palette; - delete[] RawImage ; - RawImage = 0; - m_palette = 0; + m_palette = NULL; + delete[] RawImage; + RawImage = NULL; - if (lpbi) - { + if (lpbi) { wxMacDestroyGWorld( (GWorldPtr) lpbi ) ; } lpbi = wxMacCreateGWorld( Width , Height , Depth); @@ -134,11 +133,18 @@ wxPNGReader::Create(int width, int height, int depth, int colortype) wxPNGReader::~wxPNGReader ( ) { - delete[] RawImage ; - if (lpbi) { + if (RawImage != NULL) { + delete[] RawImage ; + RawImage = NULL; + } + if (lpbi) { wxMacDestroyGWorld( (GWorldPtr) lpbi ) ; - } - delete m_palette; + lpbi = NULL; + } + if (m_palette != NULL) { + delete m_palette; + m_palette = NULL; + } } @@ -260,10 +266,12 @@ void wxPNGReader::NullData() { if (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) @@ -432,7 +440,7 @@ bool wxPNGReader::ReadFile(char * ImageFileName) if (!info_ptr) { fclose(fp); - delete(png_ptr); + delete png_ptr; return FALSE; } /* set error handling */ @@ -440,8 +448,8 @@ bool wxPNGReader::ReadFile(char * ImageFileName) { png_read_destroy(png_ptr, info_ptr, (png_info *)0); fclose(fp); - delete(png_ptr); - delete(info_ptr); + delete png_ptr; + delete info_ptr; /* If we get here, we had a problem reading the file */ return FALSE; @@ -623,8 +631,8 @@ bool wxPNGReader::ReadFile(char * ImageFileName) png_read_destroy(png_ptr, info_ptr, (png_info *)0); /* free the structures */ - delete(png_ptr); - delete(info_ptr); + delete png_ptr; + delete info_ptr; /* close the file */ fclose(fp); @@ -663,7 +671,7 @@ bool wxPNGReader::SaveFile(char * ImageFileName) if (!info_ptr) { fclose(fp); - delete(png_ptr); + delete png_ptr; return FALSE; } @@ -672,8 +680,8 @@ bool wxPNGReader::SaveFile(char * ImageFileName) { png_write_destroy(png_ptr); fclose(fp); - delete(png_ptr); - delete(info_ptr); + delete png_ptr; + delete info_ptr; /* If we get here, we had a problem reading the file */ return FALSE; @@ -770,8 +778,8 @@ bool wxPNGReader::SaveFile(char * ImageFileName) delete[] (info_ptr->palette); /* free the structures */ - delete(png_ptr); - delete(info_ptr); + delete png_ptr; + delete info_ptr; /* close the file */ fclose(fp); diff --git a/src/mac/carbon/printdlg.cpp b/src/mac/carbon/printdlg.cpp index adb620e76f..ec13b97455 100644 --- a/src/mac/carbon/printdlg.cpp +++ b/src/mac/carbon/printdlg.cpp @@ -71,8 +71,10 @@ bool wxPrintDialog::Create(wxWindow *p, wxPrintDialogData* data) wxPrintDialog::~wxPrintDialog() { - if (m_destroyDC && m_printerDC) + if (m_destroyDC && m_printerDC) { delete m_printerDC; + m_printerDC = NULL; + } } int wxPrintDialog::ShowModal() diff --git a/src/mac/carbon/thread.cpp b/src/mac/carbon/thread.cpp index 5ce510da31..5ea6f21f32 100644 --- a/src/mac/carbon/thread.cpp +++ b/src/mac/carbon/thread.cpp @@ -99,7 +99,7 @@ public: { 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() { s_threads.Remove( (void*) this ) ; - delete m_internal; + if (m_internal != NULL) { + delete m_internal; + m_internal = NULL; + } } // create/start thread diff --git a/src/mac/carbon/timer.cpp b/src/mac/carbon/timer.cpp index debf409696..2355b8eace 100644 --- a/src/mac/carbon/timer.cpp +++ b/src/mac/carbon/timer.cpp @@ -92,8 +92,10 @@ bool wxTimer::IsRunning() const wxTimer::~wxTimer() { Stop(); - delete m_info ; - m_info = NULL ; + if (m_info != NULL) { + delete m_info ; + m_info = NULL ; + } int index = gTimersInProcess.Index( this ) ; if ( index != wxNOT_FOUND ) gTimersInProcess.RemoveAt( index ) ; diff --git a/src/mac/carbon/tooltip.cpp b/src/mac/carbon/tooltip.cpp index c4eea989d6..ba6de8cc8a 100644 --- a/src/mac/carbon/tooltip.cpp +++ b/src/mac/carbon/tooltip.cpp @@ -201,10 +201,12 @@ void wxMacToolTip::Setup( WindowRef win , wxString text , wxPoint localPosition wxMacToolTip::~wxMacToolTip() { - if ( m_timer ) + if ( m_timer ) { delete m_timer ; - if ( m_backpict ) - Clear() ; + m_timer = NULL; + } + if ( m_backpict ) + Clear() ; } const short kTipBorder = 2 ; diff --git a/src/mac/combobox.cpp b/src/mac/combobox.cpp index db47c86aaa..e67e1d9601 100644 --- a/src/mac/combobox.cpp +++ b/src/mac/combobox.cpp @@ -104,16 +104,19 @@ BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice) EVT_CHOICE(-1, wxComboBoxChoice::OnChoice) END_EVENT_TABLE() - - - 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 // user code expects them to be gone now - delete m_text; - delete m_choice; + if (m_text != NULL) { + delete m_text; + m_text = NULL; + } + if (m_choice != NULL) { + delete m_choice; + m_choice = NULL; + } } diff --git a/src/mac/dirmac.cpp b/src/mac/dirmac.cpp index 1acff5b7ca..729be9473d 100644 --- a/src/mac/dirmac.cpp +++ b/src/mac/dirmac.cpp @@ -253,7 +253,10 @@ wxString wxDir::GetName() const wxDir::~wxDir() { - delete M_DIR; + if (M_DIR != NULL) { + delete M_DIR; + m_data = NULL; + } } // ---------------------------------------------------------------------------- diff --git a/src/mac/glcanvas.cpp b/src/mac/glcanvas.cpp index 62d9c307c4..16e47d4c79 100644 --- a/src/mac/glcanvas.cpp +++ b/src/mac/glcanvas.cpp @@ -141,8 +141,10 @@ wxGLCanvas::wxGLCanvas( wxWindow *parent, const wxGLCanvas *shared, wxWindowID i wxGLCanvas::~wxGLCanvas() { - if (m_glContext) - delete m_glContext; + if (m_glContext != NULL) { + delete m_glContext; + m_glContext = NULL; + } } static AGLPixelFormat ChoosePixelFormat(const int *attribList) diff --git a/src/mac/listbox.cpp b/src/mac/listbox.cpp index 2d8471f00e..2d9a8708d1 100644 --- a/src/mac/listbox.cpp +++ b/src/mac/listbox.cpp @@ -265,6 +265,7 @@ void wxListBox::Free() size_t uiCount = m_aItems.Count(); while ( uiCount-- != 0 ) { delete m_aItems[uiCount]; + m_aItems[uiCount] = NULL; } m_aItems.Clear(); @@ -373,6 +374,7 @@ void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData) size_t ui = m_aItems.Count(); while ( ui-- != 0 ) { delete m_aItems[ui]; + m_aItems[ui] = NULL; } m_aItems.Empty(); diff --git a/src/mac/palette.cpp b/src/mac/palette.cpp index 1c18196135..c847131f40 100644 --- a/src/mac/palette.cpp +++ b/src/mac/palette.cpp @@ -36,7 +36,10 @@ wxPaletteRefData::wxPaletteRefData() wxPaletteRefData::~wxPaletteRefData() { - delete[] m_palette ; + if (m_palette != NULL) { + delete[] m_palette ; + m_palette = NULL; + } } wxPalette::wxPalette() diff --git a/src/mac/pnghand.cpp b/src/mac/pnghand.cpp index 1ebf612f17..3d2690803c 100644 --- a/src/mac/pnghand.cpp +++ b/src/mac/pnghand.cpp @@ -111,12 +111,11 @@ wxPNGReader::Create(int width, int height, int depth, int colortype) Width = width; Height = height; Depth = depth; ColorType = (colortype>=0) ? colortype: ((Depth>8) ? COLORTYPE_COLOR: 0); delete m_palette; - delete[] RawImage ; - RawImage = 0; - m_palette = 0; + m_palette = NULL; + delete[] RawImage; + RawImage = NULL; - if (lpbi) - { + if (lpbi) { wxMacDestroyGWorld( (GWorldPtr) lpbi ) ; } lpbi = wxMacCreateGWorld( Width , Height , Depth); @@ -134,11 +133,18 @@ wxPNGReader::Create(int width, int height, int depth, int colortype) wxPNGReader::~wxPNGReader ( ) { - delete[] RawImage ; - if (lpbi) { + if (RawImage != NULL) { + delete[] RawImage ; + RawImage = NULL; + } + if (lpbi) { wxMacDestroyGWorld( (GWorldPtr) lpbi ) ; - } - delete m_palette; + lpbi = NULL; + } + if (m_palette != NULL) { + delete m_palette; + m_palette = NULL; + } } @@ -260,10 +266,12 @@ void wxPNGReader::NullData() { if (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) @@ -432,7 +440,7 @@ bool wxPNGReader::ReadFile(char * ImageFileName) if (!info_ptr) { fclose(fp); - delete(png_ptr); + delete png_ptr; return FALSE; } /* set error handling */ @@ -440,8 +448,8 @@ bool wxPNGReader::ReadFile(char * ImageFileName) { png_read_destroy(png_ptr, info_ptr, (png_info *)0); fclose(fp); - delete(png_ptr); - delete(info_ptr); + delete png_ptr; + delete info_ptr; /* If we get here, we had a problem reading the file */ return FALSE; @@ -623,8 +631,8 @@ bool wxPNGReader::ReadFile(char * ImageFileName) png_read_destroy(png_ptr, info_ptr, (png_info *)0); /* free the structures */ - delete(png_ptr); - delete(info_ptr); + delete png_ptr; + delete info_ptr; /* close the file */ fclose(fp); @@ -663,7 +671,7 @@ bool wxPNGReader::SaveFile(char * ImageFileName) if (!info_ptr) { fclose(fp); - delete(png_ptr); + delete png_ptr; return FALSE; } @@ -672,8 +680,8 @@ bool wxPNGReader::SaveFile(char * ImageFileName) { png_write_destroy(png_ptr); fclose(fp); - delete(png_ptr); - delete(info_ptr); + delete png_ptr; + delete info_ptr; /* If we get here, we had a problem reading the file */ return FALSE; @@ -770,8 +778,8 @@ bool wxPNGReader::SaveFile(char * ImageFileName) delete[] (info_ptr->palette); /* free the structures */ - delete(png_ptr); - delete(info_ptr); + delete png_ptr; + delete info_ptr; /* close the file */ fclose(fp); diff --git a/src/mac/printdlg.cpp b/src/mac/printdlg.cpp index adb620e76f..ec13b97455 100644 --- a/src/mac/printdlg.cpp +++ b/src/mac/printdlg.cpp @@ -71,8 +71,10 @@ bool wxPrintDialog::Create(wxWindow *p, wxPrintDialogData* data) wxPrintDialog::~wxPrintDialog() { - if (m_destroyDC && m_printerDC) + if (m_destroyDC && m_printerDC) { delete m_printerDC; + m_printerDC = NULL; + } } int wxPrintDialog::ShowModal() diff --git a/src/mac/thread.cpp b/src/mac/thread.cpp index 5ce510da31..5ea6f21f32 100644 --- a/src/mac/thread.cpp +++ b/src/mac/thread.cpp @@ -99,7 +99,7 @@ public: { 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() { s_threads.Remove( (void*) this ) ; - delete m_internal; + if (m_internal != NULL) { + delete m_internal; + m_internal = NULL; + } } // create/start thread diff --git a/src/mac/timer.cpp b/src/mac/timer.cpp index debf409696..2355b8eace 100644 --- a/src/mac/timer.cpp +++ b/src/mac/timer.cpp @@ -92,8 +92,10 @@ bool wxTimer::IsRunning() const wxTimer::~wxTimer() { Stop(); - delete m_info ; - m_info = NULL ; + if (m_info != NULL) { + delete m_info ; + m_info = NULL ; + } int index = gTimersInProcess.Index( this ) ; if ( index != wxNOT_FOUND ) gTimersInProcess.RemoveAt( index ) ; diff --git a/src/mac/tooltip.cpp b/src/mac/tooltip.cpp index c4eea989d6..ba6de8cc8a 100644 --- a/src/mac/tooltip.cpp +++ b/src/mac/tooltip.cpp @@ -201,10 +201,12 @@ void wxMacToolTip::Setup( WindowRef win , wxString text , wxPoint localPosition wxMacToolTip::~wxMacToolTip() { - if ( m_timer ) + if ( m_timer ) { delete m_timer ; - if ( m_backpict ) - Clear() ; + m_timer = NULL; + } + if ( m_backpict ) + Clear() ; } const short kTipBorder = 2 ; -- 2.45.2