-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Description
Bugzilla Link | 2633 |
Version | unspecified |
OS | MacOS X |
Attachments | Xcode project for Mac OS X |
Reporter | LLVM Bugzilla Contributor |
CC | @tkremenek,@haoNoQ |
Extended Description
Clang Static Analyzer (build 71 August 2, 2008) detects a Memory Leak in Objective-C source code. It seems to be a false positive.
You can follow these steps to reproduce the problem:
- open the Xcode project
- run the application
- choose "New Sample Window" from the "File" menu
- it calls openSampleWindow in MainClass.m
- it allocates a SampleWindow object (it's a NSWindowController subclass)
- it displays the window
- you can click the window close button
- it calls windowWillClose in SampleWindow.m
- the window controller is autoreleased and the window is released
- you can check the log, the SampleWindow object is deallocated
Clang Static Analyzer detects a memory leak in MainClass.m
I have found this technique in Apple documentation:
http://developer.apple.com/documentation/Cocoa/Conceptual/Documents/Concepts/WindowClosingBehav.html
If you want closing a window to deallocate the window and its window controller when it isn’t owned by a document, you should add code to the object that does own the window controller to observe the NSWindow notification NSWindowWillCloseNotification or, as window delegate, implement the windowWillClose: method. When you see that your window controller’s window is closing, you can autorelease the window controller, which will also have the effect of releasing the window and any other top-level objects loaded from the window controller’s nib file when the application reenters the main event loop.
Clang Static Analyzer detects a memory leak because the object is allocated in MainClass.m and it is autoreleased in SampleWindow.m when windowWillClose is called.
Do you think you can improve Clang Static Analyzer? or do you think autoreleasing the NSWindowController in windowWillClose is a bad design?
Thanks