[Drawkit] Notification for change in selection

Brad Larson larson at sonoplot.com
Mon Jun 29 15:42:05 PDT 2009


>>
>> Also, I happened to run the Clang Static Analyzer on the framework  
>> the other day and found a few memory leaks.  Should I submit  
>> patches for those or wait for the next beta release?
>
> I have found a small number recently, but it would be good to know  
> what you found. If patches are the easiest way, then please do,  
> otherwise a list with a brief description should do it.
>


I'll try a list, and see how that goes.

DKDrawingInfoLayer
In - (void) mouseDown:(NSEvent*) event inView:(NSView*) view

str is leaked here:

			str = [[NSTextStorage alloc] initWithString:[di objectForKey:key]  
attributes:[self attributesForDrawingInfoItem:key]];
			
			[(DKDrawingView*)view editText:str inRect: NSOffsetRect( fr, -1.5,  
2.5 ) delegate:self];
			m_editingKeyRef = key;

DKDrawableShape
In - (id) copyWithZone:(NSZone*) zone
an object is leaked here:
	[copy setDistortionTransform:[[self distortionTransform] copy]];

and here
	[copy setHotspots:[[self hotspots] deepCopy]];

DKStyle
In + (DKStyle*) defaultTrackStyle
the analyzer is throwing warnings about returning an object with a +1  
retain count from a method without a new- or copy- prefix.  I'm not  
sure that the retain is needed here, if you're caching the style using  
registerStyle:inCategories:

Also, in - (NSImage*) styleSwatchWithSize:(NSSize) size type: 
(DKStyleSwatchType) type
the image is returned with a retain count of +1, where I think it  
should be autoreleased.

NSBezierPath+Geometry
In - (CGPathRef)	quartzPath and
the analyzer is complaining about the way that mpath is released.   
This appears to be tied to it complaining about the +1 retain count  
coming out of - (CGMutablePathRef) mutableQuartzPath.  It recommends  
renaming mutableQuartzPath to newMutableQuartzPath to make clear that  
an object is being generated.

This is also the case with - (void) setQuartzPathInContext: 
(CGContextRef) context isNewPath:(BOOL) np

DKDrawingView
In - (void) createRulerMarkers
all of the ruler markers are allocated, but none are released

DKCIFilterRastGroup
In - (id) copyWithZone:(NSZone*) zone
an object is leaked here:
	[copy setArguments:[[self arguments] deepCopy]];

DKCategoryManager
In - (void) updateForKey:(NSString*) key
oldTitle looks to be leaked, unless I'm reading something wrong

NSColor+DKAdditions
In - (CGColorRef)	 quartzColor
the analyzer is once again complaining about returning something with  
a retain count of +1 from a method not prefixed with "new"

DKGradient
In - (CGShadingRef) makeLinearShaderForStartingPoint:(NSPoint) sp  
endPoint:(NSPoint) ep
it's complaining about the naming of the method, again.

In - (CGShadingRef) makeRadialShaderForStartingPoint:(NSPoint) sp  
startRadius:(float) sr endPoint:(NSPoint) ep endRadius:(float) er;
same deal, again.

NSImage+Tracing
In - (NSBezierPath*) pathFromTracePath:(potrace_path_t*) tp
it looks like the naming issue again.

DKSweptAngleGradient
In - (void) createGradientImageWithRect:(NSRect) rect
cSpace is never released

GCOneShotEffectTimer
In + (id)	oneShotWithTime:(NSTimeInterval) t forDelegate:(id) del
it looks to be the same naming issue with returning retained objects.

DKUniqueID
In + (NSString*)uniqueKey
it looks like this will leak under garbage collection, due to the  
autorelease on NSString not being recognized.  I believe that the  
recommended way to handle something like this is by replacing the last  
line with

	return [NSMakeCollectable(str) autorelease];

Unfortunately, this is Leopard-only code.

DKStyleRegistry
In - (NSMenu*)newManagedStylesMenuWithItemTarget:(id) target  
itemAction:(SEL) selector
the analyzer is complaining that the object returned from this doesn't  
appear to have a +1 retain count.  This seems to be another naming  
issue.

DKDrawingDocument
In - (NSData*)dataOfType:(NSString*) typeName error:(NSError**) outError
the line
		*outError = [NSError errorWithDomain:NSCocoaErrorDomain  
code:NSFileWriteUnsupportedSchemeError userInfo:nil];
may dereference a null pointer

In - (void)printShowingPrintPanel:(BOOL) flag
it looks like pdv is being leaked.

In - (BOOL) readFromData:(NSData*) data ofType:(NSString*) typeName  
error:(NSError **)outError
the line
		*outError = [NSError errorWithDomain:NSCocoaErrorDomain  
code:NSFileReadUnsupportedSchemeError userInfo:nil];
may dereference a null pointer.

In - (id)init
The line
		[self setUndoManager:[[DKUndoManager alloc] init]];
may leak an object.

In - (void)dealloc
The analyzer is complaining about the line
	[[self drawing] release];

DKDrawing+Export
In - (CGImageRef)CGImageWithResolution:(int) dpi hasAlpha:(BOOL)  
hasAlpha relativeScale:(float) relScale
bmRep is being leaked.

In - (CGImageRef)CGImageWithResolution:(int) dpi hasAlpha:(BOOL)  
hasAlpha relativeScale:(float) relScale
is another case where the code will leak under garbage collection, due  
to a Core Foundation class not being released.  The above solution  
should also work in this case.

In - (NSData*)JPEGDataWithProperties:(NSDictionary*) props
options can be leaked if the method returns early (when image is nil).
Also, data can be leaked if the method returns nil at the very end.   
data can also leak under garbage collection for the same Core- 
Foundation-related reasons described above.

In - (NSData*)TIFFDataWithProperties:(NSDictionary*) props
many of the same leaks in JPEGDataWithProperties: apply here.

In - (NSData*)PNGDataWithProperties:(NSDictionary*) props
many of the same leaks in JPEGDataWithProperties: apply here.

DKHatching
Not a memory leak, but if m_cache is nil, in - (void) hatchPath: 
(NSBezierPath*) path objectAngle:(float) oa
cr can be set to a garbage or undefined value via
	cr = [m_cache bounds];


Sorry about the long list.  Most of these are naming convention  
issues, which the Clang Static Analyzer seems to be a stickler for.   
Some are real leaks, particularly the ones that only strike in a  
garbage-collected environment.

______________________
Brad Larson, Ph.D.
SonoPlot, Inc.
3030 Laura Lane, Suite 120
Middleton, WI 53562






More information about the Drawkit mailing list