]> git.saurik.com Git - wxWidgets.git/commitdiff
fixing memory leaks (toolbartool not released) and a bug where native embedded contro...
authorStefan Csomor <csomor@advancedconcepts.ch>
Sat, 9 Dec 2006 09:48:28 +0000 (09:48 +0000)
committerStefan Csomor <csomor@advancedconcepts.ch>
Sat, 9 Dec 2006 09:48:28 +0000 (09:48 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@43871 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/mac/carbon/toolbar.cpp

index 7614e623124c412aec03d1abf1f5b3a91f02613a..d3fcb494ac348e4fe6b75c7a637d64274324ede0 100644 (file)
@@ -82,11 +82,6 @@ public:
     virtual ~wxToolBarTool()
     {
         ClearControl();
-
-#if wxMAC_USE_NATIVE_TOOLBAR
-        if ( m_toolbarItemRef )
-            CFRelease( m_toolbarItemRef );
-#endif
     }
 
     WXWidget GetControlHandle()
@@ -116,7 +111,13 @@ public:
         }
 
 #if wxMAC_USE_NATIVE_TOOLBAR
-        m_toolbarItemRef = NULL;
+        if ( m_toolbarItemRef )
+        {
+            CFIndex count = CFGetRetainCount( m_toolbarItemRef ) ;
+            wxASSERT_MSG( count == 1 , wxT("Reference Count of native tool was not 1 in wxToolBarTool destructor") );
+            CFRelease( m_toolbarItemRef );
+            m_toolbarItemRef = NULL;
+        }
 #endif
     }
 
@@ -610,8 +611,17 @@ static pascal OSStatus ControlToolbarItemHandler( EventHandlerCallRef inCallRef,
                     break;
 
                 case kEventHIObjectDestruct:
-                    free( object ) ;
-                    result = noErr;
+                    {
+                        // we've increased the ref count when creating this, so we decrease manually again in case
+                        // it was never really installed and deinstalled
+                        HIViewRef viewRef = object->viewRef ;
+                        if( viewRef && CFGetRetainCount( viewRef ) > 1 )
+                        {
+                            CFRelease( viewRef ) ;
+                        }
+                           free( object ) ;
+                       result = noErr;
+                    }
                     break;
             }
             break;
@@ -834,6 +844,9 @@ wxToolBar::~wxToolBar()
         if (m_macUsesNativeToolbar)
             MacInstallNativeToolbar( false );
 
+        CFIndex count = CFGetRetainCount( m_macHIToolbarRef ) ;
+        wxASSERT_MSG( count == 1 , wxT("Reference Count of native control was not 1 in wxToolBar destructor") );
+
         CFRelease( (HIToolbarRef)m_macHIToolbarRef );
         m_macHIToolbarRef = NULL;
     }
@@ -1518,34 +1531,29 @@ bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase)
     CFIndex removeIndex = tool->GetIndex();
 #endif
 
+#if wxMAC_USE_NATIVE_TOOLBAR
+    if ( removeIndex != -1 && m_macHIToolbarRef )
+    {
+        HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, removeIndex );
+        tool->SetIndex( -1 );
+    }
+#endif
     switch ( tool->GetStyle() )
     {
         case wxTOOL_STYLE_CONTROL:
-            {
+            if ( tool->GetControl() )
                 tool->GetControl()->Destroy();
-                tool->ClearControl();
-            }
             break;
 
         case wxTOOL_STYLE_BUTTON:
         case wxTOOL_STYLE_SEPARATOR:
-            if ( tool->GetControlHandle() )
-            {
-#if wxMAC_USE_NATIVE_TOOLBAR
-                if ( removeIndex != -1 && m_macHIToolbarRef )
-                {
-                    HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, removeIndex );
-                    tool->SetIndex( -1 );
-                }
-#endif
-
-                tool->ClearControl();
-            }
+            // nothing special
             break;
 
         default:
             break;
     }
+    tool->ClearControl();
 
     // and finally reposition all the controls after this one