From 59c2bf0fe8329e2653c12a95e486fdfe8a373389 Mon Sep 17 00:00:00 2001 From: stuconnolly Date: Fri, 7 Aug 2009 16:44:51 +0000 Subject: - Fix a memory leak when filtering the server variables - Fix a memory leak in MCPResult.m's stringFromCString: - Display the number of server variables being display when a filter is being applied --- Frameworks/MCPKit/MCPFoundationKit/MCPResult.m | 7 ++- Interfaces/English.lproj/DBView.xib | 70 +++++++++++++++++++++++--- Source/TableDocument.h | 2 +- Source/TableDocument.m | 64 +++++++++++++---------- 4 files changed, 104 insertions(+), 39 deletions(-) diff --git a/Frameworks/MCPKit/MCPFoundationKit/MCPResult.m b/Frameworks/MCPKit/MCPFoundationKit/MCPResult.m index 2fb15a88..333c8327 100644 --- a/Frameworks/MCPKit/MCPFoundationKit/MCPResult.m +++ b/Frameworks/MCPKit/MCPFoundationKit/MCPResult.m @@ -1105,12 +1105,11 @@ const OUR_CHARSET our_charsets60[] = return @""; } - theData = [NSData dataWithBytes:theCString length:(strlen(theCString))]; + theData = [[NSData alloc] initWithBytes:theCString length:(strlen(theCString))]; theString = [[NSString alloc] initWithData:theData encoding:mEncoding]; - if (theString) { - [theString autorelease]; - } + if (theData) [theData release]; + if (theString) [theString autorelease]; return theString; } diff --git a/Interfaces/English.lproj/DBView.xib b/Interfaces/English.lproj/DBView.xib index 70d80409..1db91c7e 100644 --- a/Interfaces/English.lproj/DBView.xib +++ b/Interfaces/English.lproj/DBView.xib @@ -8,6 +8,7 @@ 353.00 YES + YES @@ -185,6 +186,7 @@ {212, 26} + 6 @@ -220,6 +222,7 @@ {212, 26} + 0 @@ -334,6 +337,7 @@ {212, 354} + 6 @@ -365,6 +369,7 @@ {212, 354} + 528 @@ -489,6 +494,7 @@ {212, 145} + 6 @@ -515,6 +521,7 @@ {212, 145} + 512 @@ -2121,6 +2128,7 @@ {{1, 17}, {688, 455}} + 2 @@ -2154,6 +2162,7 @@ {{1, 0}, {688, 17}} + 4 @@ -2162,6 +2171,7 @@ {{6, 32}, {690, 473}} + 562 @@ -7319,9 +7329,9 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 View {3.40282e+38, 3.40282e+38} - {300, 200} + {350, 200} - + 256 YES @@ -7598,11 +7608,28 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 CAAAAA + + + 266 + {{108, 316}, {116, 14}} + + YES + + 68288064 + 71435264 + 0 of 0 + + + + + + {411, 341} + {{0, 0}, {1440, 878}} - {300, 222} + {350, 222} {3.40282e+38, 3.40282e+38} @@ -14887,6 +14914,14 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 6346 + + + variablesCountTextField + + + + 6349 + @@ -15464,6 +15499,7 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 + @@ -21019,6 +21055,20 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 + + 6347 + + + YES + + + + + + 6348 + + + @@ -22276,6 +22326,8 @@ IGRvIHlvdSB3YW50IHRvIGFkZCBmb3IgdGhpcyBmaWVsZD8 6343.ImportedFromIB2 6344.IBPluginDependency 6344.ImportedFromIB2 + 6347.IBPluginDependency + 6348.IBPluginDependency 654.IBPluginDependency 654.ImportedFromIB2 655.IBPluginDependency @@ -24081,6 +24133,8 @@ aGUgYWN0aXZlIHNlbGVjdGlvbiAo4oyl4oyYUik com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -24160,14 +24214,14 @@ Y2hhbmdlIHRoZSBvcmRlcg com.apple.InterfaceBuilder.CocoaPlugin - {{520, 273}, {411, 341}} - {{520, 273}, {411, 341}} + {{356, 287}, {411, 341}} + {{356, 287}, {411, 341}} {{321, 508}, {411, 341}} {3.40282e+38, 3.40282e+38} - {300, 200} + {350, 200} com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -24300,7 +24354,7 @@ Y2hhbmdlIHRoZSBvcmRlcg - 6346 + 6349 @@ -25152,6 +25206,7 @@ Y2hhbmdlIHRoZSBvcmRlcg titleAccessoryView titleImageView titleStringView + variablesCountTextField variablesSearchField variablesSheet variablesTableView @@ -25197,6 +25252,7 @@ Y2hhbmdlIHRoZSBvcmRlcg id id id + NSTextField NSSearchField id id diff --git a/Source/TableDocument.h b/Source/TableDocument.h index 7df1155d..42be9e5a 100644 --- a/Source/TableDocument.h +++ b/Source/TableDocument.h @@ -77,6 +77,7 @@ enum { IBOutlet NSTabView *tableTabView; IBOutlet NSButton *saveVariablesButton; IBOutlet NSSearchField *variablesSearchField; + IBOutlet NSTextField *variablesCountTextField; IBOutlet NSTableView *tableInfoTable; IBOutlet NSButton *tableInfoCollapseButton; @@ -121,7 +122,6 @@ enum { NSMutableArray *allDatabases; NSString *queryEditorInitString; - } - (NSString *)getHTMLforPrint; diff --git a/Source/TableDocument.m b/Source/TableDocument.m index 320c4d01..4670740c 100644 --- a/Source/TableDocument.m +++ b/Source/TableDocument.m @@ -1358,26 +1358,24 @@ - (void)showVariables:(id)sender { int i; - NSMutableArray *tempResult = [NSMutableArray array]; - if (variablesFiltered) { - [variablesFiltered release]; - variablesFiltered = nil; - } + [variablesCountTextField setStringValue:@""]; + + if (variables) [variables release], variables = nil; // Get variables MCPResult *theResult = [mySQLConnection queryString:@"SHOW VARIABLES"]; if ([theResult numOfRows]) [theResult dataSeek:0]; + variables = [[NSMutableArray alloc] init]; + for ( i = 0 ; i < [theResult numOfRows] ; i++ ) { - [tempResult addObject:[theResult fetchRowAsDictionary]]; + [variables addObject:[theResult fetchRowAsDictionary]]; } - - variablesFiltered = [[NSArray arrayWithArray:tempResult] retain]; - + // Weak reference - variables = variablesFiltered; + variablesFiltered = variables; [variablesTableView reloadData]; @@ -1395,6 +1393,13 @@ [NSApp endSheet:variablesSheet]; [variablesSheet orderOut:nil]; + + // If the filtered array is allocated and its not a reference to the variables array get rid of it + if ((variablesFiltered) && (variablesFiltered != variables)) { + [variablesFiltered release], variablesFiltered = nil; + } + + if (variables) [variables release], variables = nil; } - (void)closeConnection @@ -1984,10 +1989,10 @@ - (void)savePanelDidEnd:(NSSavePanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo { if (returnCode == NSOKButton) { - if ([variables count] > 0) { + if ([variablesFiltered count] > 0) { NSMutableString *variablesString = [NSMutableString stringWithFormat:@"# MySQL server variables for %@\n\n", [self host]]; - for (NSDictionary *variable in variables) + for (NSDictionary *variable in variablesFiltered) { [variablesString appendString:[NSString stringWithFormat:@"%@ = %@\n", [variable objectForKey:@"Variable_name"], [variable objectForKey:@"Value"]]]; } @@ -2498,12 +2503,12 @@ - (int)numberOfRowsInTableView:(NSTableView *)aTableView { - return [variables count]; + return [variablesFiltered count]; } - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex { - id theValue = [[variables objectAtIndex:rowIndex] objectForKey:[aTableColumn identifier]]; + id theValue = [[variablesFiltered objectAtIndex:rowIndex] objectForKey:[aTableColumn identifier]]; if ([theValue isKindOfClass:[NSData class]]) { theValue = [[NSString alloc] initWithData:theValue encoding:[mySQLConnection encoding]]; @@ -2524,7 +2529,6 @@ if (connectionController) [connectionController release]; if (mySQLConnection) [mySQLConnection release]; if (variables) [variables release]; - if (variablesFiltered) [variablesFiltered release]; if (selectedDatabase) [selectedDatabase release]; if (mySQLVersion) [mySQLVersion release]; [allDatabases release]; @@ -2544,39 +2548,45 @@ { [saveVariablesButton setEnabled:NO]; - filterString = [filterString lowercaseString]; + filterString = [[filterString lowercaseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; - filterString = [filterString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + // If the filtered array is allocated and its not a reference to the variables array + // relase it to prevent memory leaks upon the next allocation. + if ((variablesFiltered) && (variablesFiltered != variables)) { + [variablesFiltered release], variablesFiltered = nil; + } - variables = [[NSMutableArray alloc] init]; + variablesFiltered = [[NSMutableArray alloc] init]; if ([filterString length] == 0) { - [variables release]; - variables = variablesFiltered; + [variablesFiltered release]; + variablesFiltered = variables; [saveVariablesButton setEnabled:YES]; [saveVariablesButton setTitle:@"Save As..."]; + [variablesCountTextField setStringValue:@""]; [variablesTableView reloadData]; return; } - for (NSDictionary *variable in variablesFiltered) + for (NSDictionary *variable in variables) { if (([[variable objectForKey:@"Variable_name"] rangeOfString:filterString options:NSCaseInsensitiveSearch].location != NSNotFound) || ([[variable objectForKey:@"Value"] rangeOfString:filterString options:NSCaseInsensitiveSearch].location != NSNotFound)) { - [variables addObject:variable]; + [variablesFiltered addObject:variable]; } } [variablesTableView reloadData]; + [variablesCountTextField setStringValue:[NSString stringWithFormat:NSLocalizedString(@"%d of %d", "filtered server variables count"), [variablesFiltered count], [variables count]]]; - if ([variables count] > 0) { - [saveVariablesButton setEnabled:YES]; - [saveVariablesButton setTitle:@"Save View As..."]; - } + if ([variablesFiltered count] == 0) return; + + [saveVariablesButton setEnabled:YES]; + [saveVariablesButton setTitle:@"Save View As..."]; } -@end \ No newline at end of file +@end -- cgit v1.2.3