2012-04-23 17 views
6

ich versuche const char * in NSString * zu konvertieren und dann zurück zu konvertieren. Es funktioniert, aber ich erhalte:conver char * in NSString * konvertieren und zurück konvertieren - _NSAutoreleaseNoPool()

__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 

Der Code ist:

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]; 

Ich bin verloren, bitte helfen Sie mir.

+0

Zeigen Sie alle Ihren Code, ich denke, ich weiß, was das Problem ist, aber ich muss zuerst Ihren gesamten Code überprüfen. –

Antwort

15

Wenn Sie dies in einem Hintergrundthread tun, fügen Sie einen NSAutoReleasePool hinzu.

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]; 

Außerdem müssen Sie input freizugeben, nachdem Sie mit ihm fertig sind, oder es Autoreleased zu machen.

+0

Ich mache es in einem Headerfile dann ich es in der NSApplicationDelegate Souce einbinden/importieren, können Sie mir ein Beispiel zeigen, becouse ich bin neu zu diesem .. – user1341993

+0

im Erhalten des Programms empfangenes Signal: "EXC_BAD_ACCESS". – user1341993

+0

@ user1341993 - siehe bearbeiten – MByD