Anonymous avatar Anonymous committed 2017fae

Styled Text Field - Fixed leak

Comments (0)

Files changed (2)

BWStyledTextFieldCell.h

 	BOOL shadowIsBelow, hasShadow, hasGradient;
 	NSColor *shadowColor, *startingColor, *endingColor, *solidColor;
 	
+	NSShadow *shadow;
 	NSMutableDictionary *previousAttributes;
 }
 

BWStyledTextFieldCell.m

 
 @interface BWStyledTextFieldCell ()
 @property (retain) NSMutableDictionary *previousAttributes;
+@property (nonatomic, retain) NSShadow *shadow;
 @end
 
 @implementation BWStyledTextFieldCell
 
-@synthesize shadowIsBelow, shadowColor, hasShadow, previousAttributes, startingColor, endingColor, hasGradient, solidColor;
+@synthesize shadowIsBelow, shadowColor, hasShadow, shadow, previousAttributes, startingColor, endingColor, hasGradient, solidColor;
 
 - (id)initWithCoder:(NSCoder *)decoder
 {
 	[coder encodeObject:[self solidColor] forKey:@"BWSTFCSolidColor"];
 } 
 
+- (void)dealloc
+{
+	[shadow release];
+	[previousAttributes release];
+	[solidColor release];
+	[endingColor release];
+	[startingColor release];
+	[shadowColor release];
+	
+	[super dealloc];
+}
+
+#pragma mark Text attributes
+
 - (NSDictionary *)_textAttributes
 {
 	NSMutableDictionary *attributes = [[[NSMutableDictionary alloc] init] autorelease];
 	[attributes addEntriesFromDictionary:[super _textAttributes]];
 	
 	// Shadow code
-	if (hasShadow)
-	{
-		NSShadow *shadow = [[NSShadow alloc] init];
-		[shadow setShadowColor:shadowColor];
-		
-		if (shadowIsBelow)
-			[shadow setShadowOffset:NSMakeSize(0,-1)];
-		else
-			[shadow setShadowOffset:NSMakeSize(0,1)];
-		
-		[attributes setObject:shadow forKey:NSShadowAttributeName];
-		
-		//[shadow release]; //This causes a sometimes reproducible crash at design-time. Patches welcome.
-	}
+	if (hasShadow && [self shadow] != nil)
+		[attributes setObject:[self shadow] forKey:NSShadowAttributeName];
 	
 	// Gradient code
 	if ([previousAttributes objectForKey:@"NSFont"] != nil && [[previousAttributes objectForKey:@"NSFont"] isEqualTo:[attributes objectForKey:@"NSFont"]] == NO)
 	return attributes;
 }
 
-- (void)dealloc
+#pragma mark Shadow-specific code
+
+- (void)changeShadow
 {
-	[shadowColor release];
-	[super dealloc];
+	NSShadow *tempShadow = [[[NSShadow alloc] init] autorelease];
+	[tempShadow setShadowColor:shadowColor];
+		
+	if (shadowIsBelow)
+		[tempShadow setShadowOffset:NSMakeSize(0,-1)];
+	else
+		[tempShadow setShadowOffset:NSMakeSize(0,1)];
+
+	[self setShadow:tempShadow];
 }
 
-#pragma mark Gradient-specific Code
+#pragma mark Gradient-specific code
 
 - (void)awakeFromNib
 {
 	[[NSGraphicsContext currentContext] restoreGraphicsState];
 }
 
+#pragma mark Accessors
+
 - (void)setStartingColor:(NSColor *)color
 {
 	if (startingColor != color) 
 		[self setTextColor:self.solidColor];
 }
 
+- (void)setShadowIsBelow:(BOOL)flag
+{
+	shadowIsBelow = flag;
+	
+	[self changeShadow];
+}
+
+- (void)setShadowColor:(NSColor *)color
+{
+	if (shadowColor != color) 
+	{
+        [shadowColor release];
+        shadowColor = [color retain];
+		
+		[self changeShadow];
+    }
+}
+
 @end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.