@@ -25,40 +25,42 @@ - (void) addAppAsLoginItem;
2525- (void ) deleteAppFromLoginItem ;
2626@end
2727
28+ #pragma mark - Implemetation
2829@implementation ISHAppDelegate (LoginItem)
2930// I copied the codes from the following blog. And a little modification.
3031// http://cocoatutorial.grapewave.com/2010/02/creating-andor-removing-a-login-item/
3132
3233-(LSSharedFileListItemRef) findLoginItem : (LSSharedFileListRef)loginItems {
34+ LSSharedFileListItemRef retVal = NULL ;
35+ if (!loginItems) return retVal;
36+
3337 NSString *appPath = [[NSBundle mainBundle ] bundlePath ];
3438
3539 // This will retrieve the path for the application
3640 // For example, /Applications/test.app
37- CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath: appPath];
38-
39- LSSharedFileListItemRef retVal = NULL ;
41+ NSURL *url = [NSURL new ];
4042
41- if (loginItems) {
42- UInt32 seedValue;
43- // Retrieve the list of Login Items and cast them to
44- // a NSArray so that it will be easier to iterate.
45- NSArray *loginItemsArray = (__bridge NSArray *) LSSharedFileListCopySnapshot (loginItems, &seedValue) ;
46- int i = 0 ;
47- for (; i< [loginItemsArray count ]; i++){
48- LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)[loginItemsArray
49- objectAtIndex: i];
50- // Resolve the item with URL
51- if ( LSSharedFileListItemResolve (itemRef, 0 , (CFURLRef*) &url, NULL ) == noErr) {
52- NSString * urlPath = [(__bridge NSURL *) url path ];
53- if ([urlPath compare: appPath] == NSOrderedSame){
54- retVal = itemRef;
55- break ;
56- }
57- }
58- }
59- // WARNING! Fix this for ARC.
60- [loginItemsArray release ];
61- }
43+ UInt32 seedValue;
44+ // Retrieve the list of Login Items and cast them to
45+ // a NSArray so that it will be easier to iterate.
46+ NSArray *loginItemsArray = (__bridge NSArray *) LSSharedFileListCopySnapshot (loginItems, &seedValue);
47+ int i = 0 ;
48+ for (; i< [loginItemsArray count ]; i++){
49+ LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)[loginItemsArray
50+ objectAtIndex: i];
51+ // Resolve the item with URL
52+ if ( LSSharedFileListItemResolve (itemRef, 0 , (__bridge CFURLRef*) &url, NULL ) == noErr) {
53+ NSString * urlPath = [url path ];
54+ [ url release ]; // The resolved url must be released!
55+ if ([urlPath compare: appPath] == NSOrderedSame){
56+ CFRetain ( itemRef) ;
57+ retVal = itemRef ;
58+ break ;
59+ }
60+ }
61+ }
62+ // WARNING! Fix this for ARC.
63+ [loginItemsArray release ];
6264
6365 return retVal;
6466}
@@ -67,7 +69,13 @@ -(BOOL) isLoginItem {
6769 // Create a reference to the shared file list.
6870 LSSharedFileListRef loginItems = LSSharedFileListCreate (NULL ,
6971 kLSSharedFileListSessionLoginItems , NULL );
70- return [self findLoginItem: loginItems] ? YES : NO ;
72+ LSSharedFileListItemRef listItemRef = [self findLoginItem: loginItems];
73+ CFRelease (loginItems);
74+
75+ BOOL retVal = [self findLoginItem: loginItems] ? YES : NO ;
76+
77+ CFRelease (listItemRef);
78+ return retVal;
7179}
7280
7381-(void ) addAppAsLoginItem {
@@ -106,7 +114,10 @@ -(void) deleteAppFromLoginItem{
106114
107115 if (itemRef) {
108116 LSSharedFileListItemRemove (loginItems,itemRef);
117+ CFRelease (itemRef);
109118 }
119+
120+ CFRelease (loginItems);
110121}
111122@end
112123
@@ -199,6 +210,8 @@ - (NSString *)getLongestInputSourceName {
199210 }
200211 }
201212
213+ [inputSources release ];
214+
202215 GHKLOG (@" The input method with the longest name is: %@ " , nameForMaxLength);
203216 return nameForMaxLength;
204217}
@@ -302,6 +315,7 @@ - (void)inputSourceChanged:(NSNotification *) notification {
302315 TISInputSourceRef inputSource = TISCopyCurrentKeyboardInputSource ();
303316 NSString *name = (__bridge NSString *)TISGetInputSourceProperty (inputSource, kTISPropertyLocalizedName );
304317 GHKLOG (@" The im name is: %@ " , name);
318+ CFRelease (inputSource);
305319
306320 static NSString *previousIsName = nil ;
307321
0 commit comments