Fix memory leak in NSTextView

On Windows, there is an occasional sporadic failure of autorelease to work correctly. In particular, this seems to happen when autorelease is invoked on an object while a call stack is in the middle of draining an autorelease pool. So, for example, when dealloc is called on an NSTextContainer, it then calls setTextContainer: on the associated NSTextView, which in turn calls textContainers on the layoutManager, which returns an autoreleased array of NSTextContainers. This array is sometimes released and sometimes it isn't.
Wrapping the setTextContainer: in an autorelease pool, which will then drain at the end of the method, rather than relying on the main autorelease pool in the run loop (which is in the middle of being drained), appears to fix this problem. This adds a small amount of overhead, but also makes the memory usage a little more efficient, since anything autoretained during the course of this method is released more quickly.
This commit is contained in:
williameveretteggplant 2022-02-11 17:12:50 -07:00 committed by GitHub
parent f5ebdcd1d2
commit f37e93a440
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -64,6 +64,7 @@
#import <Foundation/NSTimer.h>
#import <Foundation/NSUndoManager.h>
#import <Foundation/NSValue.h>
#import <Foundation/NSAutoreleasePool.h>
#import "AppKit/NSApplication.h"
#import "AppKit/NSAttributedString.h"
@ -1170,6 +1171,9 @@ to this method from the text container or layout manager.
*/
- (void) setTextContainer: (NSTextContainer *)container
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSUInteger i, c;
NSArray *tcs;
NSTextView *other;
@ -1233,6 +1237,8 @@ to this method from the text container or layout manager.
_currentInsertionPointMovementDirection = 0;
[self _updateMultipleTextViews];
[pool drain];
}
- (void) replaceTextContainer: (NSTextContainer *)newContainer