c - Trouble spotting memory leak cStringUsingEncoding -
Hey, I'm trying to convert NSString into a C string using cStringUsingEncoding but I have a memory leak. My understanding is that cStringUsingEncoding returns an indicator for a character array that is only guaranteed to exist for the duration of the NSString object. Like you should copy its content to another string. Here's my problem ...
I have a function that accepts an NSString and converts it into a C-string copy. Just for testing, I ran into 1,000 iterations of the following method (to ensure no leaks).
- (zero) test {NSString * test = [[NSString alloc] initWithString: @ "Hello world!"]; For (int i = 0; i <1000; i ++) {four * tmp = [self backcopy c string: test]; // Free memory-free (TMP); } [Test release]; }
Very straight forward code ... The problem is that it leaks like madness. Now before you jump to my return conclusion, the COPIEDCString function works just fine. I tested it to eliminate the possibility of declaring a Si String and making it a copy to make a copy. Inside the function I am using the following code to change it
- return () * returnCopiedCString: (NSString *) input {// Maintaining input [Maintaining input]; // Get the length of the encoded C-string int lane [[Input LengthIfByteUpping Encoding: NSUTF8StringEncoding]; // New C-string + blank memory allocated by memory to memory * toReturn = (four *) malloc ((sizeof (char) * len) +1); // Copy NSString to C string strcpy (toReturn, [input cStringUsingEncoding: NSUTF8StringEncoding]); // Release Input [Input Release]; // return indicator return to return to new copied string; }
Besides this very straight forward. So I got to think well because I declare it out of the loop for the exam, and thus it is never released, I'm going out of all these four cells * cStringUsingEncoding method so I have the first function Changed to:
- (zero) test {for (int i = 0; i
But there is still no luck, can anyone have any idea on this issue? Thanks a bunch in advance!
From the document:
Back C string only valid Is guaranteed until the receiver becomes free, or until the existing automotive pool becomes empty, whichever is earlier. You should make a copy of C string or use getCString: maxLength: encoding: If this time C string needs to be stored.
I think that cStringUsingEncoding converted allocate memory for the specified encoding for the string and so you are not the receiver that you can not use automatic Reliance pool, this memory Is never free.