Skip to content

Commit

Permalink
Trying to reduce the memory footprint some.
Browse files Browse the repository at this point in the history
	There seems to be a bug in NSImage that causes it to leak when I ask for
the TIFF representation of an image.
  • Loading branch information
dustin committed May 23, 2007
1 parent 1ee364c commit 8ba209e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion AppHider.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
29B97325FDCFA39411CA2CEA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
32CA4F630368D1EE00C91783 /* AppHider_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppHider_Prefix.pch; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
8D1107320486CEB800E47090 /* AppHider.app */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = wrapper.application; path = AppHider.app; sourceTree = BUILT_PRODUCTS_DIR; };
8D1107320486CEB800E47090 /* AppHider.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AppHider.app; sourceTree = BUILT_PRODUCTS_DIR; };
DA1B35290B86EC4500E2D853 /* English */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text.html; name = English; path = English.lproj/Credits.html; sourceTree = "<group>"; };
DA2439D40B86D597007A594A /* AppTracker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppTracker.h; sourceTree = "<group>"; };
DA2439D50B86D597007A594A /* AppTracker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppTracker.m; sourceTree = "<group>"; };
Expand Down
2 changes: 0 additions & 2 deletions AppTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
NSMutableDictionary *activityTimes;

NSMutableSet *ignored;

BOOL isGrowlReady;
}

-(NSArray*)currentApps;
Expand Down
33 changes: 23 additions & 10 deletions AppTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,24 @@ -(void)updateArray {
[arry release];
}

-(NSData *)getAppIcon:(NSString *)path {
NSImage *icon=[[NSWorkspace sharedWorkspace] iconForFile: path];
// This avoids a minor memory leak due to the image being cached.
[icon setCachedSeparately:YES];
[icon setCacheMode:NSImageCacheNever];
return [icon TIFFRepresentation];
}

-(void)appLaunched:(NSNotification*)notification {
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
[self willChangeValueForKey:@"currentApps"];
id anAppDict=[notification userInfo];

[GrowlApplicationBridge
notifyWithTitle:[NSString stringWithFormat: @"Launched %@", [anAppDict valueForKey:@"NSApplicationName"]]
description:[NSString stringWithFormat: @"Detected launch of %@", [anAppDict valueForKey:@"NSApplicationPath"]]
notificationName:@"AppLaunched"
iconData:[[[NSWorkspace sharedWorkspace] iconForFile: [anAppDict objectForKey:@"NSApplicationPath"]] TIFFRepresentation]
iconData:[self getAppIcon: [anAppDict objectForKey:@"NSApplicationPath"]]
priority:0
isSticky:NO
clickContext:nil];
Expand All @@ -54,17 +63,19 @@ -(void)appLaunched:(NSNotification*)notification {

[self updateArray];
[self didChangeValueForKey:@"currentApps"];
[pool release];
}

-(void)appQuit:(NSNotification*)notification {
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
[self willChangeValueForKey:@"currentApps"];
id anAppDict=[notification userInfo];

[GrowlApplicationBridge
notifyWithTitle:[NSString stringWithFormat: @"Quit %@", [anAppDict valueForKey:@"NSApplicationName"]]
description:[NSString stringWithFormat: @"Detected quit of %@", [anAppDict valueForKey:@"NSApplicationPath"]]
notificationName:@"AppQuit"
iconData:[[[NSWorkspace sharedWorkspace] iconForFile: [anAppDict objectForKey:@"NSApplicationPath"]] TIFFRepresentation]
iconData:[self getAppIcon: [anAppDict objectForKey:@"NSApplicationPath"]]
priority:0
isSticky:NO
clickContext:nil];
Expand All @@ -74,6 +85,7 @@ -(void)appQuit:(NSNotification*)notification {

[self updateArray];
[self didChangeValueForKey:@"currentApps"];
[pool release];
}

-(void)initAppList {
Expand Down Expand Up @@ -103,12 +115,11 @@ -(void)checkCurrentApp:(NSTimer*)timer {
}

-(void)hideApp:(NSDictionary*)app name:(NSString *)name {
NSLog(@"Hiding app (%@ growl)...", (isGrowlReady?@"with":@"without"));
[GrowlApplicationBridge
notifyWithTitle:[NSString stringWithFormat: @"Hiding %@", name]
description:[NSString stringWithFormat: @"Hiding application %@", name]
notificationName:@"Hiding"
iconData:[[[NSWorkspace sharedWorkspace] iconForFile: [app objectForKey:@"NSApplicationPath"]] TIFFRepresentation]
iconData:[self getAppIcon:[app objectForKey:@"NSApplicationPath"]]
priority:0
isSticky:NO
clickContext:name];
Expand Down Expand Up @@ -181,6 +192,7 @@ -(NSDictionary *)objectAtIndex:(int)idx {
}

-(void)awakeFromNib {
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
currentApps = [[NSMutableDictionary alloc] initWithCapacity:100];
activityTimes = [[NSMutableDictionary alloc] initWithCapacity:100];
ignored = [[NSMutableSet alloc] initWithCapacity:100];
Expand All @@ -195,10 +207,13 @@ -(void)awakeFromNib {
userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:15.0 target:self selector:@selector(checkIdleApps:)
userInfo:nil repeats:YES];

[pool release];
}

- (NSDictionary *) registrationDictionaryForGrowl
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
NSArray *allNotifications=[[NSArray alloc] initWithObjects:
@"Hiding", @"AppLaunched", @"AppQuit", @"CheckingIdle", nil];
NSArray *defaultNotifications=[[NSArray alloc] initWithObjects:
Expand All @@ -211,16 +226,14 @@ - (NSDictionary *) registrationDictionaryForGrowl

[allNotifications release];
[defaultNotifications release];

// Autorelease everything that was built to be autoreleased
[pool release];
// This is the return value, so need to set it up for autorelease after releasing the pool
[dict autorelease];
return(dict);
}

-(void)growlIsReady
{
NSLog(@"growl is ready");
isGrowlReady=YES;
}

-(NSString *)applicationNameForGrowl
{
return(@"AppHider");
Expand Down
2 changes: 2 additions & 0 deletions Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
IBOutlet NSMenu *appMenu;
IBOutlet NSPanel *prefs;

IBOutlet NSStatusItem *statusItem;

IBOutlet AppTracker *tracker;
IBOutlet SUUpdater *sparkleUpdater;
}
Expand Down
12 changes: 6 additions & 6 deletions Controller.m
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ -(void)observeValueForKeyPath:(NSString *)path ofObject:(id)object
}

-(void)initStatusBar {
NSStatusItem *newItem=[[NSStatusBar systemStatusBar] statusItemWithLength: 40.0];
[newItem setTitle: @"hide"];
[newItem setMenu: appMenu];
[newItem setEnabled:YES];
[newItem setHighlightMode:YES];
[newItem retain];
statusItem=[[NSStatusBar systemStatusBar] statusItemWithLength: 40.0];
[statusItem setTitle: @"hide"];
[statusItem setMenu: appMenu];
[statusItem setEnabled:YES];
[statusItem setHighlightMode:YES];
[statusItem retain];
}

-(void)setDefaultDefaults {
Expand Down

0 comments on commit 8ba209e

Please sign in to comment.