Convert const char * to NSString * and convert back - _NSAutoreleaseNoPool ()

I am trying to convert const char * to NSString * and then convert it back. It works, but I get:

__NSAutoreleaseNoPool(): Object 0x100550a40 of class NSCFArray autoreleased with no pool in place - just leaking __NSAutoreleaseNoPool(): Object 0x100551730 of class NSCFString autoreleased with no pool in place - just leaking __NSAutoreleaseNoPool(): Object 0x100551f10 of class NSCFData autoreleased with no pool in place - just leaking 

The code:

 const char* convert = "hello remove this: *"; NSString *input = [[NSString alloc] initWithUTF8String:convert];// convert //remove * FROM THE STRING NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"*"]; // REPLACE * WITH NOTHING input = [[input componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""]; // CONVERT BACK const char *converted_back = [input UTF8String]; 

I'm lost, please help me.

+6
source share
1 answer

If you are doing this in the background thread, add NSAutoReleasePool.

 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; const char* convert = "hello remove this: *"; NSString *input = [[[NSString alloc] initWithUTF8String:convert] autorelease];// convert //remove * FROM THE STRING NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"*"]; // REPLACE * WITH NOTHING input = [[input componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""]; // CONVERT BACK const char *converted_back = [input UTF8String]; [pool drain]; 

In addition, you need to free the input after you are done with it, or make it auto-implement.

+15
source

Source: https://habr.com/ru/post/913956/


All Articles