|
From: | Alexander Malmberg |
Subject: | Re: Memory leak? |
Date: | Wed, 05 Jan 2005 12:50:19 +0100 |
User-agent: | Mozilla Thunderbird 0.9 (X11/20041124) |
Marko Riedel wrote:
Hi folks, I discovered a possible memory leak in an application that I am trying to update and was able to isolate the problem in a test application. I am sending the source code. The test application opens a number of windows and fills them with textviews and scrollviews. It prints the allocation count for the two classes when the user quits the application. Now do the following experiment: close some of those windows manually and watch the allocation count at exit. It should decrease by the number of windows that you have closed by hand, but it doesn't! (E.g create ten windows, close four, should leave six textviews.) What is happening here? Where is my mistake?
The text views are being autoreleased somewhere during initialization. They won't be deallocated until 'pool' is released (i.e. never; the app exits before it reaches that line). If you add an autorelease pool around the loop that creates the text views, you'll get the expected results.
In general, if one does a lot of initialization in main(), it's a good idea to wrap the initialization in a dedicated autorelease pool. Otherwise, objects autoreleased there will, in practice, never be released.
- Alexander Malmberg
[Prev in Thread] | Current Thread | [Next in Thread] |