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);
}
}
ubrk_close(breakIterator);
#else
NSWarnLog(@"NSStringEnumerationByWords and NSStringEnumerationBySentences"
@" 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
encoding: (NSStringEncoding)encoding
{
if (characters) free(characters);
characters = NULL;
if (l > 0)
{
if (encoding == NSUnicodeStringEncoding)
@ -37,7 +39,7 @@ implementations of the NSString methods in NSString itself.
s = [[NSString alloc] initWithBytes: c
length: l
encoding: encoding];
if (s == nil) return nil;
if (s == nil) {RELEASE(self); return nil;}
l = [s length] * sizeof(unichar);
characters = malloc(l);
[s getCharacters: characters];
@ -53,6 +55,8 @@ implementations of the NSString methods in NSString itself.
encoding: (NSStringEncoding)encoding
freeWhenDone: (BOOL)freeWhenDone
{
if (characters) free(characters);
characters = NULL;
if (l > 0)
{
if (encoding == NSUnicodeStringEncoding)
@ -68,7 +72,7 @@ implementations of the NSString methods in NSString itself.
length: l
encoding: encoding
freeWhenDone: freeWhenDone];
if (s == nil) return nil;
if (s == nil) {RELEASE(self); return nil;}
l = [s length] * sizeof(unichar);
characters = malloc(l);
[s getCharacters: characters];

View file

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

View file

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

View file

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

View file

@ -16,7 +16,7 @@ int main(int argc, char **argv)
unichar characters[3]={'a',0,'b'};
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([normalString length] == 3, "nuls in non-constant strings");

View file

@ -9,16 +9,16 @@ int main()
unichar theUniChar[1] = {0xe5};
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
PASS([theString isEqual:[[NSString alloc] initWithCString: [theString cString]]],"foo");
PASS([theString isEqual: AUTORELEASE([[NSString alloc] initWithCString: [theString cString]])],"foo");
NS_HANDLER
PASS(1,"bar");
NS_ENDHANDLER
NS_DURING
PASS([theString isEqual:[[NSMutableString alloc] initWithCString: [theString cString]]],"foo2");
PASS([theString isEqual: AUTORELEASE([[NSMutableString alloc] initWithCString: [theString cString]])],"foo2");
NS_HANDLER
PASS(1,"bar2");
NS_ENDHANDLER

View file

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

View file

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

View file

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