Fix leaks

This commit is contained in:
rfm 2024-11-15 21:21:41 +00:00
parent 77c66e3d95
commit 088b76bed1
10 changed files with 71 additions and 65 deletions

View file

@ -6592,6 +6592,7 @@ static NSFileManager *fm = nil;
&stop); &stop);
} }
} }
ubrk_close(breakIterator);
#else #else
NSWarnLog(@"NSStringEnumerationByWords and NSStringEnumerationBySentences" NSWarnLog(@"NSStringEnumerationByWords and NSStringEnumerationBySentences"
@" are not supported when GNUstep-base is compiled without ICU."); @" are not supported when GNUstep-base is compiled without ICU.");

View file

@ -23,6 +23,8 @@ implementations of the NSString methods in NSString itself.
length: (NSUInteger)l length: (NSUInteger)l
encoding: (NSStringEncoding)encoding encoding: (NSStringEncoding)encoding
{ {
if (characters) free(characters);
characters = NULL;
if (l > 0) if (l > 0)
{ {
if (encoding == NSUnicodeStringEncoding) if (encoding == NSUnicodeStringEncoding)
@ -37,7 +39,7 @@ implementations of the NSString methods in NSString itself.
s = [[NSString alloc] initWithBytes: c s = [[NSString alloc] initWithBytes: c
length: l length: l
encoding: encoding]; encoding: encoding];
if (s == nil) return nil; if (s == nil) {RELEASE(self); return nil;}
l = [s length] * sizeof(unichar); l = [s length] * sizeof(unichar);
characters = malloc(l); characters = malloc(l);
[s getCharacters: characters]; [s getCharacters: characters];
@ -53,6 +55,8 @@ implementations of the NSString methods in NSString itself.
encoding: (NSStringEncoding)encoding encoding: (NSStringEncoding)encoding
freeWhenDone: (BOOL)freeWhenDone freeWhenDone: (BOOL)freeWhenDone
{ {
if (characters) free(characters);
characters = NULL;
if (l > 0) if (l > 0)
{ {
if (encoding == NSUnicodeStringEncoding) if (encoding == NSUnicodeStringEncoding)
@ -68,7 +72,7 @@ implementations of the NSString methods in NSString itself.
length: l length: l
encoding: encoding encoding: encoding
freeWhenDone: freeWhenDone]; freeWhenDone: freeWhenDone];
if (s == nil) return nil; if (s == nil) {RELEASE(self); return nil;}
l = [s length] * sizeof(unichar); l = [s length] * sizeof(unichar);
characters = malloc(l); characters = malloc(l);
[s getCharacters: characters]; [s getCharacters: characters];

View file

@ -39,7 +39,7 @@ Basic sanity test.
*/ */
BOOL test_initWithCString(void) BOOL test_initWithCString(void)
{ {
NSString *test1 = [[stringClass alloc] initWithCString: "ascii"]; NSString *test1 = AUTORELEASE([[stringClass alloc] initWithCString: "ascii"]);
NSString *sanity = @"ascii"; NSString *sanity = @"ascii";
if (!test1) if (!test1)
@ -117,9 +117,9 @@ test_encoding(void)
NSData *d = [[NSData alloc] initWithBytes: "foo" length: 3]; NSData *d = [[NSData alloc] initWithBytes: "foo" length: 3];
NSString *s = [[stringClass alloc] initWithData: d encoding: 0]; NSString *s = [[stringClass alloc] initWithData: d encoding: 0];
PASS(s == nil, "-initWithData:encoding: gives nil for invalid encodings")
DESTROY(d); DESTROY(d);
PASS(s == nil, "-initWithData:encoding: gives nil for invalid encodings")
DESTROY(s);
} }
test_encodings_helper(NSASCIIStringEncoding, test_encodings_helper(NSASCIIStringEncoding,

View file

@ -31,7 +31,7 @@ int main(int argc, char **argv)
h = [s hash]; h = [s hash];
PASS(h != 0, "[NSConstantString hash] does not return 0"); PASS(h != 0, "[NSConstantString hash] does not return 0");
s = [[NSString alloc] initWithString: s]; s = [NSString stringWithString: s];
h = [s hash]; h = [s hash];
PASS(h != 0, "[NSString hash] does not return 0"); PASS(h != 0, "[NSString hash] does not return 0");

View file

@ -17,25 +17,25 @@ testMutationAffectingSubsequentCall()
BOOL correctCallCount; BOOL correctCallCount;
__block NSUInteger callCount = 0; __block NSUInteger callCount = 0;
mutableString = [NSMutableString stringWithString:@"Hello World"]; mutableString = [NSMutableString stringWithString: @"Hello World"];
results = [NSMutableArray array]; results = [NSMutableArray array];
range = NSMakeRange(0, mutableString.length); range = NSMakeRange(0, mutableString.length);
expectedResults = @[ @"Hello", @"World" ]; expectedResults = @[ @"Hello", @"World" ];
[mutableString [mutableString
enumerateSubstringsInRange:range enumerateSubstringsInRange: range
options:NSStringEnumerationByWords options: NSStringEnumerationByWords
usingBlock:^(NSString *substring, NSRange substringRange, usingBlock: ^(NSString *substring, NSRange substringRange,
NSRange enclosingRange, BOOL *stop) { NSRange enclosingRange, BOOL *stop) {
[results addObject:substring]; [results addObject: substring];
callCount++; callCount++;
if ([substring isEqualToString:@"Hello"]) if ([substring isEqualToString: @"Hello"])
{ {
// Simulate a mutation that affects subsequent // Simulate a mutation that affects subsequent
// enumeration "Hello " is changed to "Hello" // enumeration "Hello " is changed to "Hello"
[mutableString [mutableString
deleteCharactersInRange:NSMakeRange( deleteCharactersInRange: NSMakeRange(
substringRange.location substringRange.location
+ substringRange.length, + substringRange.length,
1)]; 1)];
@ -44,14 +44,14 @@ testMutationAffectingSubsequentCall()
}]; }];
[mutableString [mutableString
enumerateSubstringsInRange:NSMakeRange(5, mutableString.length - 5) enumerateSubstringsInRange: NSMakeRange(5, mutableString.length - 5)
options:NSStringEnumerationByWords options: NSStringEnumerationByWords
usingBlock:^(NSString *substring, NSRange substringRange, usingBlock: ^(NSString *substring, NSRange substringRange,
NSRange enclosingRange, BOOL *stop) { NSRange enclosingRange, BOOL *stop) {
[results addObject:substring]; [results addObject: substring];
}]; }];
correctResults = [results isEqualToArray:expectedResults]; correctResults = [results isEqualToArray: expectedResults];
correctCallCount = (callCount == 1); // Ensure only one call before stopping correctCallCount = (callCount == 1); // Ensure only one call before stopping
PASS(correctResults && correctCallCount, PASS(correctResults && correctCallCount,
@ -74,11 +74,11 @@ testBasicFunctionality()
expected = @[ @"Hello", @"World" ]; expected = @[ @"Hello", @"World" ];
[string [string
enumerateSubstringsInRange:range enumerateSubstringsInRange: range
options:NSStringEnumerationByWords options: NSStringEnumerationByWords
usingBlock:^(NSString *substring, NSRange substringRange, usingBlock: ^(NSString *substring, NSRange substringRange,
NSRange enclosingRange, BOOL *stop) { NSRange enclosingRange, BOOL *stop) {
[results addObject:substring]; [results addObject: substring];
}]; }];
PASS_EQUAL(results, expected, "Should correctly enumerate words."); PASS_EQUAL(results, expected, "Should correctly enumerate words.");
@ -96,11 +96,11 @@ testEmptyRange()
range = NSMakeRange(0, 0); range = NSMakeRange(0, 0);
[string [string
enumerateSubstringsInRange:range enumerateSubstringsInRange: range
options:NSStringEnumerationByWords options: NSStringEnumerationByWords
usingBlock:^(NSString *substring, NSRange substringRange, usingBlock: ^(NSString *substring, NSRange substringRange,
NSRange enclosingRange, BOOL *stop) { NSRange enclosingRange, BOOL *stop) {
[results addObject:substring]; [results addObject: substring];
}]; }];
PASS(results.count == 0, PASS(results.count == 0,
@ -119,11 +119,11 @@ void testLocationOffset() {
expected = @[ @"World", @"Continued"]; expected = @[ @"World", @"Continued"];
[string [string
enumerateSubstringsInRange:range enumerateSubstringsInRange: range
options:NSStringEnumerationByWords options: NSStringEnumerationByWords
usingBlock:^(NSString *substring, NSRange substringRange, usingBlock: ^(NSString *substring, NSRange substringRange,
NSRange enclosingRange, BOOL *stop) { NSRange enclosingRange, BOOL *stop) {
[results addObject:substring]; [results addObject: substring];
}]; }];
PASS_EQUAL(results, expected, "Should correctly enumerate words with location offset."); PASS_EQUAL(results, expected, "Should correctly enumerate words with location offset.");
@ -143,19 +143,19 @@ testStoppingEnumeration()
__block BOOL didStop = NO; __block BOOL didStop = NO;
[string [string
enumerateSubstringsInRange:range enumerateSubstringsInRange: range
options:NSStringEnumerationByWords options: NSStringEnumerationByWords
usingBlock:^(NSString *substring, NSRange substringRange, usingBlock: ^(NSString *substring, NSRange substringRange,
NSRange enclosingRange, BOOL *stop) { NSRange enclosingRange, BOOL *stop) {
if ([substring isEqualToString:@"Hello"]) if ([substring isEqualToString: @"Hello"])
{ {
*stop = YES; *stop = YES;
didStop = YES; didStop = YES;
} }
[results addObject:substring]; [results addObject: substring];
}]; }];
PASS(didStop && [results count] == 1 && [results[0] isEqualToString:@"Hello"], PASS(didStop && [results count] == 1 && [results[0] isEqualToString: @"Hello"],
"Enumeration should stop after 'Hello'."); "Enumeration should stop after 'Hello'.");
} }
@ -168,17 +168,17 @@ main(int argc, const char *argv[])
NSString *s1 = @"Line 1\nLine 2"; NSString *s1 = @"Line 1\nLine 2";
__block NSUInteger currentIteration = 0; __block NSUInteger currentIteration = 0;
[s1 [s1
enumerateSubstringsInRange:(NSRange){.location = 0, .length = [s1 length]} enumerateSubstringsInRange: (NSRange){.location = 0, .length = [s1 length]}
options:NSStringEnumerationByLines options: NSStringEnumerationByLines
usingBlock:^(NSString *substring, NSRange substringRange, usingBlock: ^(NSString *substring, NSRange substringRange,
NSRange enclosingRange, BOOL *stop) { NSRange enclosingRange, BOOL *stop) {
// *stop = YES; // *stop = YES;
if (currentIteration == 0) if (currentIteration == 0)
PASS([substring isEqual:@"Line 1"], PASS([substring isEqual: @"Line 1"],
"First line of \"Line 1\\nLine 2\" is \"Line 1\""); "First line of \"Line 1\\nLine 2\" is \"Line 1\"");
if (currentIteration == 1) if (currentIteration == 1)
PASS( PASS(
[substring isEqual:@"Line 2"], [substring isEqual: @"Line 2"],
"Second line of \"Line 1\\nLine 2\" is \"Line 2\""); "Second line of \"Line 1\\nLine 2\" is \"Line 2\"");
currentIteration++; currentIteration++;
}]; }];
@ -190,17 +190,17 @@ main(int argc, const char *argv[])
NSString *s1 = @"Paragraph 1\nParagraph 2"; NSString *s1 = @"Paragraph 1\nParagraph 2";
__block NSUInteger currentIteration = 0; __block NSUInteger currentIteration = 0;
[s1 enumerateSubstringsInRange:(NSRange){.location = 0, .length = [s1 length]} [s1 enumerateSubstringsInRange: (NSRange){.location = 0, .length = [s1 length]}
options:NSStringEnumerationByParagraphs options: NSStringEnumerationByParagraphs
usingBlock:^(NSString *substring, NSRange substringRange, usingBlock: ^(NSString *substring, NSRange substringRange,
NSRange enclosingRange, BOOL *stop) { NSRange enclosingRange, BOOL *stop) {
// *stop = YES; // *stop = YES;
if (currentIteration == 0) if (currentIteration == 0)
PASS([substring isEqual:@"Paragraph 1"], PASS([substring isEqual: @"Paragraph 1"],
"First paragraph of \"Paragraph 1\\nParagraph " "First paragraph of \"Paragraph 1\\nParagraph "
"2\" is \"Paragraph 1\""); "2\" is \"Paragraph 1\"");
if (currentIteration == 1) if (currentIteration == 1)
PASS([substring isEqual:@"Paragraph 2"], PASS([substring isEqual: @"Paragraph 2"],
"Second paragraph of \"Paragraph 1\\nParagraph " "Second paragraph of \"Paragraph 1\\nParagraph "
"2\" is \"Paragraph 2\""); "2\" is \"Paragraph 2\"");
currentIteration++; currentIteration++;
@ -219,16 +219,16 @@ main(int argc, const char *argv[])
NSString *s1 = @"Word1 word2."; NSString *s1 = @"Word1 word2.";
__block NSUInteger currentIteration = 0; __block NSUInteger currentIteration = 0;
[s1 enumerateSubstringsInRange:(NSRange){.location = 0, .length = [s1 length]} [s1 enumerateSubstringsInRange: (NSRange){.location = 0, .length = [s1 length]}
options:NSStringEnumerationByWords options: NSStringEnumerationByWords
usingBlock:^(NSString *substring, NSRange substringRange, usingBlock: ^(NSString *substring, NSRange substringRange,
NSRange enclosingRange, BOOL *stop) { NSRange enclosingRange, BOOL *stop) {
// *stop = YES; // *stop = YES;
if (currentIteration == 0) if (currentIteration == 0)
PASS([substring isEqual:@"Word1"], PASS([substring isEqual: @"Word1"],
"First word of \"Word1 word2.\" is \"Word1\""); "First word of \"Word1 word2.\" is \"Word1\"");
if (currentIteration == 1) if (currentIteration == 1)
PASS([substring isEqual:@"word2"], PASS([substring isEqual: @"word2"],
"Second word of \"Word1 word2.\" is \"word2\""); "Second word of \"Word1 word2.\" is \"word2\"");
currentIteration++; currentIteration++;
}]; }];
@ -239,17 +239,17 @@ main(int argc, const char *argv[])
NSString *s1 = @"Sentence 1. Sentence 2."; NSString *s1 = @"Sentence 1. Sentence 2.";
__block NSUInteger currentIteration = 0; __block NSUInteger currentIteration = 0;
[s1 enumerateSubstringsInRange:(NSRange){.location = 0, .length = [s1 length]} [s1 enumerateSubstringsInRange: (NSRange){.location = 0, .length = [s1 length]}
options:NSStringEnumerationBySentences options: NSStringEnumerationBySentences
usingBlock:^(NSString *substring, NSRange substringRange, usingBlock: ^(NSString *substring, NSRange substringRange,
NSRange enclosingRange, BOOL *stop) { NSRange enclosingRange, BOOL *stop) {
// *stop = YES; // *stop = YES;
if (currentIteration == 0) if (currentIteration == 0)
PASS([substring isEqual:@"Sentence 1. "], PASS([substring isEqual: @"Sentence 1. "],
"First sentence of \"Sentence 1. Sentence 2.\" " "First sentence of \"Sentence 1. Sentence 2.\" "
"is \"Sentence 1. \""); "is \"Sentence 1. \"");
if (currentIteration == 1) if (currentIteration == 1)
PASS([substring isEqual:@"Sentence 2."], PASS([substring isEqual: @"Sentence 2."],
"Second sentence of \"Sentence 1. Sentence 2.\" " "Second sentence of \"Sentence 1. Sentence 2.\" "
"is \"Sentence 2.\""); "is \"Sentence 2.\"");
currentIteration++; currentIteration++;
@ -268,4 +268,4 @@ main(int argc, const char *argv[])
{ {
return 0; return 0;
} }
#endif #endif

View file

@ -16,7 +16,7 @@ int main(int argc, char **argv)
unichar characters[3]={'a',0,'b'}; unichar characters[3]={'a',0,'b'};
NSRange r; NSRange r;
normalString = [[NSString alloc] initWithCharacters: characters length: 3]; normalString = AUTORELEASE([[NSString alloc] initWithCharacters: characters length: 3]);
PASS([constantString length] == 3, "nuls in constant strings"); PASS([constantString length] == 3, "nuls in constant strings");
PASS([normalString length] == 3, "nuls in non-constant strings"); PASS([normalString length] == 3, "nuls in non-constant strings");

View file

@ -9,16 +9,16 @@ int main()
unichar theUniChar[1] = {0xe5}; unichar theUniChar[1] = {0xe5};
theString = [NSString stringWithCharacters:theUniChar length:1]; theString = [NSString stringWithCharacters:theUniChar length:1];
PASS([theString isEqual:[[NSString alloc] initWithCString: [theString cStringUsingEncoding: NSISOLatin1StringEncoding] encoding: NSISOLatin1StringEncoding]],"foo"); PASS([theString isEqual: AUTORELEASE([[NSString alloc] initWithCString: [theString cStringUsingEncoding: NSISOLatin1StringEncoding] encoding: NSISOLatin1StringEncoding])],"foo");
NS_DURING NS_DURING
PASS([theString isEqual:[[NSString alloc] initWithCString: [theString cString]]],"foo"); PASS([theString isEqual: AUTORELEASE([[NSString alloc] initWithCString: [theString cString]])],"foo");
NS_HANDLER NS_HANDLER
PASS(1,"bar"); PASS(1,"bar");
NS_ENDHANDLER NS_ENDHANDLER
NS_DURING NS_DURING
PASS([theString isEqual:[[NSMutableString alloc] initWithCString: [theString cString]]],"foo2"); PASS([theString isEqual: AUTORELEASE([[NSMutableString alloc] initWithCString: [theString cString]])],"foo2");
NS_HANDLER NS_HANDLER
PASS(1,"bar2"); PASS(1,"bar2");
NS_ENDHANDLER NS_ENDHANDLER

View file

@ -47,15 +47,15 @@ int main (int argc, const char * argv[])
PASS([s1 compare: s2 options: NSNumericSearch] == NSOrderedDescending, PASS([s1 compare: s2 options: NSNumericSearch] == NSOrderedDescending,
"11 is greater than 2"); "11 is greater than 2");
a1 = [[NSArray alloc] initWithObjects: a1 = [NSArray arrayWithObjects:
@"2", @"1", @"10", @"11", @"20", @"3", nil]; @"2", @"1", @"10", @"11", @"20", @"3", nil];
a = [[NSArray alloc] initWithObjects: a = [NSArray arrayWithObjects:
@"1", @"10", @"11", @"2", @"20", @"3", nil]; @"1", @"10", @"11", @"2", @"20", @"3", nil];
a2 = [a1 sortedArrayUsingSelector: @selector(compare:)]; a2 = [a1 sortedArrayUsingSelector: @selector(compare:)];
PASS_EQUAL(a2, a, "text sort"); PASS_EQUAL(a2, a, "text sort");
a = [[NSArray alloc] initWithObjects: a = [NSArray arrayWithObjects:
@"1", @"2", @"3", @"10", @"11", @"20", nil]; @"1", @"2", @"3", @"10", @"11", @"20", nil];
a2 = [a1 sortedArrayUsingSelector: @selector(numericCompare:)]; a2 = [a1 sortedArrayUsingSelector: @selector(numericCompare:)];
PASS_EQUAL(a2, a, "numeric sort"); PASS_EQUAL(a2, a, "numeric sort");

View file

@ -45,7 +45,7 @@ int main(void)
uni[0] = 0xdbff; uni[0] = 0xdbff;
uni[1] = 0xdfff; uni[1] = 0xdfff;
exp = [[NSString alloc] initWithCharacters: uni length: 2]; exp = AUTORELEASE([[NSString alloc] initWithCharacters: uni length: 2]);
buf[0] = 0xf4; buf[0] = 0xf4;
buf[1] = 0x8f; buf[1] = 0x8f;
buf[2] = 0xbf; buf[2] = 0xbf;

View file

@ -21,6 +21,7 @@ int main()
cstr = [[NSString alloc] initWithData: adat encoding: NSUTF8StringEncoding]; cstr = [[NSString alloc] initWithData: adat encoding: NSUTF8StringEncoding];
PASS((cstr != nil && [cstr isKindOfClass: [NSString class]]), PASS((cstr != nil && [cstr isKindOfClass: [NSString class]]),
"We can convert to UTF8 Encoding"); "We can convert to UTF8 Encoding");
RELEASE(cstr);
[arp release]; arp = nil; [arp release]; arp = nil;
return 0; return 0;