Home > Mobile >  New inquire of the OC code memory leak?
New inquire of the OC code memory leak?

Time:09-24

 - (nsstrings *) getRandomWord: (int) count {
Int nextNum=1;
NSStringEncoding gbkEncoding=CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
Init nsstrings * result=[[nsstrings alloc]].

for (int i=0; I & lt; The count. I++) {
@ autoreleasepool {
//[random number between 0 and 100)
NextNum=arc4random () % 100;
If (nextNum % 4==0) {
Int heightPos=176 + arc4random () % 39;
Int lowPos=161 + arc4random () % 93;
Byte wordBytes [2]={(Byte) heightPos, (Byte) lowPos};
Nsstrings * oneWord=[[nsstrings alloc] initWithBytes: wordBytes length: 2 encoding: gbkEncoding];
Result=[result stringByAppendingString: oneWord];
} else if (nextNum % 4==1) {
Char c=65 + arc4random () % 26;
Result=[result stringByAppendingFormat: @ "% c", c];
} else if (nextNum % 4==2) {
Char c=97 + arc4random () % 26;
Result=[result stringByAppendingFormat: @ "% c", c];
} else {
Int num=1 + arc4random () % 10;
Result=[result stringByAppendingFormat: @ "% d", num];
}
}
}

return result;
}

CodePudding user response:

The
refer to the original poster libolf response:
 - (nsstrings *) getRandomWord: (int) count {
Int nextNum=1;
NSStringEncoding gbkEncoding=CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
Init nsstrings * result=[[nsstrings alloc]].

for (int i=0; I & lt; The count. I++) {
@ autoreleasepool {
//[random number between 0 and 100)
NextNum=arc4random () % 100;
If (nextNum % 4==0) {
Int heightPos=176 + arc4random () % 39;
Int lowPos=161 + arc4random () % 93;
Byte wordBytes [2]={(Byte) heightPos, (Byte) lowPos};
Nsstrings * oneWord=[[nsstrings alloc] initWithBytes: wordBytes length: 2 encoding: gbkEncoding];
Result=[result stringByAppendingString: oneWord];
} else if (nextNum % 4==1) {
Char c=65 + arc4random () % 26;
Result=[result stringByAppendingFormat: @ "% c", c];
} else if (nextNum % 4==2) {
Char c=97 + arc4random () % 26;
Result=[result stringByAppendingFormat: @ "% c", c];
} else {
Int num=1 + arc4random () % 10;
Result=[result stringByAppendingFormat: @ "% d", num];
}
}
}

return result;
}

Do not know to have, but CFStringConvertEncodingToNSStringEncoding this function are CoreFunction framework, this framework no memory automatic recovery mechanism

CodePudding user response:

Have no memory leaks human flesh was ugly, with xcode analysis tool to check to know
Xcode - product - Profile - Leak

CodePudding user response:

No memory leak under the ARC because nsstrings object types can be automatically released and the distribution of memory Byte array in the stack, out of the scope of {} returned to be recycled
  •  Tags:  
  • iOS
  • Related