This question already has answers here:
Potential leak of an object stored into 'string' [duplicate]
(3 answers)
Closed 6 years ago.
When i do "Build and analyze" XCode gives me the following warning:
Potential leak of an object stored into 'string'
- (NSString *)getUUID
{
NSString *UUID = [EA_APP_CONSTANT getUserDefaultsValueForKey:#"uniqueID"];
if (!UUID) {
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
UUID = [(__bridge NSString*)string stringByReplacingOccurrencesOfString:#"-"withString:#""];
[EA_APP_CONSTANT setUserDefaultsValue:UUID forKey:#"uniqueID"];
}
return UUID;
}
How can i resolve this Problem?
You need call CFRelease(string);
- (NSString *)getUUID
{
NSString *UUID = [EA_APP_CONSTANT getUserDefaultsValueForKey:#"uniqueID"];
if (!UUID) {
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
UUID = [(__bridge NSString*)string stringByReplacingOccurrencesOfString:#"-"withString:#""];
[EA_APP_CONSTANT setUserDefaultsValue:UUID forKey:#"uniqueID"];
CFRelease(string);
}
return UUID;
}
Related
I have defined a GUID structure as below
typedef struct _GUID {
unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[ 8 ];
} GUID;
I’m trying to convert it to NSString. However, I’m getting the reversed values for the first three (long, short,short). It may be due to the Big-endian.
Example:
GUID testGUID = { 0x865f82d0, 0x3303, 0x403d, { 0xbf, 0xcd, 0xeb, 0xb2, 0xde, 0xd9, 0xa0, 0x69 } };
NSUUID *uuid = [[NSUUID alloc] initWithUUIDBytes:(const unsigned char*)&testGUID];
NSString *uuidString = [uuid UUIDString];
NSLog(#"%#", uuidString);
Output: D0825F86-0333-3D40-BFCD-EBB2DED9A069
Expected: 865F82D0-3303-403D-BFCD-EBB2DED9A069
let me know Is there a way to correct this. Any help on this is much appreciated.
#borrrden comment fixed the issue.
Here is the solution.
GUID testGUID = { 0x865f82d0, 0x3303, 0x403d, { 0xbf, 0xcd, 0xeb, 0xb2, 0xde, 0xd9, 0xa0, 0x69 } };
testGUID.Data1 = NTOHL(testGUID.Data1);
testGUID.Data2 = NTOHS(testGUID.Data2);
testGUID.Data3 = NTOHS(testGUID.Data3);
NSUUID *uuid = [[NSUUID alloc] initWithUUIDBytes:(const unsigned char*)&testGUID];
NSString *uuidString = [uuid UUIDString];
NSLog(#"%#", uuidString)
In my app I want to display data from sqlite database into UITableView.It is working fine in ios simulator but data is not fetched when using iphone device.I dont understand what the issue is.I have added Db.sqlite file into my project.Plz help me in solving this.My code is below:
+(BOOL)copyFile{
NSString *strSourcePath=[[NSBundle mainBundle]pathForResource:#"Db" ofType:#"sqlite"];
NSString *strHome=NSHomeDirectory();
NSString *strDestPath=[NSString stringWithFormat:#"%#/Documents/NewDb.sqlite",strHome];
NSFileManager *manager=[NSFileManager defaultManager];
NSError *err;
if ([manager fileExistsAtPath:strDestPath]==NO) {
[manager copyItemAtPath:strSourcePath toPath:strDestPath error:&err];
return YES;
}
else{
NSLog(#"file already exists");
return NO;
}
return YES;
}
+(NSMutableArray*)getDetails{
NSString *strHome = NSHomeDirectory();
NSString *strDestPath = [NSString stringWithFormat:#"%#/Documents/NewDb.sqlite",strHome];
NSMutableArray *arrDetails=[[NSMutableArray alloc]init];
sqlite3 *db;
int n = sqlite3_open([strDestPath UTF8String], &db);
if (n==SQLITE_OK) {
NSString *strQuery=#"SELECT * FROM Categories";
sqlite3_stmt *stmt;
char *err_msg;
int res=sqlite3_prepare_v2(db, [strQuery UTF8String], -1, &stmt, &err_msg);
if (res == SQLITE_OK) {
while (sqlite3_step(stmt)==SQLITE_ROW) {
Categories *categories=[[Categories alloc]init];
categories.categoryId=sqlite3_column_int(stmt,0);
categories.categoryName=[NSString stringWithUTF8String:(char *) sqlite3_column_text(stmt, 1)];
categories.categoryImage=[NSString stringWithUTF8String:(char *) sqlite3_column_text(stmt, 2)];
[arrDetails addObject:categories];
}
}
}
return arrDetails;
}
I'm not sure, but it might be the problem what is happening to images displayed with uppercase letters, try changing your sqlite file into lowercase letters. like
NSString *strDestPath = [NSString stringWithFormat:#"%#/Documents/newdb.sqlite",strHome];
Changes are as follows
+(BOOL)copyFile{
//This needs to change
NSString *strDestPath = [YourClassName dbPath];
}
+(NSMutableArray*)getDetails{
//This needs to change
NSString *strDestPath = [YourClassName dbPath];
}
+(NSString *)dbPath{
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingString:#"NewDb.sqlite"];
}
I would try changing Db by db
NSString *strSourcePath=[[NSBundle mainBundle]pathForResource:#"Db" ofType:#"sqlite"];
This question already has answers here:
Objective-c iPhone percent encode a string?
(8 answers)
Closed 8 years ago.
I'm trying to encode url string using Objective-c
// 1. Get string
char res = ((char)(400));
NSString *unencodedString = [NSString stringWithFormat:#"%c",res];
// 2. Encode string
static NSString *escape = #":/?&=;+!##$()',*[]";
NSString *result = (__bridge_transfer NSString *)
CFURLCreateStringByAddingPercentEscapes(
NULL,
(__bridge CFStringRef)unencodedString,
NULL,
(__bridge CFStringRef)escape,
kCFStringEncodingUTF8);
// result = %C2%90
But result is not that I expect. Because I get %C6%90 using other programming languages.
As you see Objective-C result is %C2%90, but I expect %C6%90.
Where is my error? Am I do something wrong?
The problem is NSString *unencodedString = [NSString stringWithFormat:#"%c",res] doesn't do what you think it does. char res cannot hold a value larger than 128 (256 for unsigned char).
char res = ((char)(400));
NSString *unencodedString = [NSString stringWithFormat:#"%c",res];
XCTAssertEqualObjects(unencodedString, #"\xc2\x90", #"");
static NSString *escape = #":/?&=;+!##$()',*[]";
NSString *result = (__bridge_transfer NSString *)
CFURLCreateStringByAddingPercentEscapes(
NULL,
(__bridge CFStringRef)unencodedString,
NULL,
(__bridge CFStringRef)escape,
kCFStringEncodingUTF8);
XCTAssertEqualObjects(result, #"%C2%90", #"");
Here is an example which works.
NSString *unencodedString = #"Ɛ";
static NSString *escape = #":/?&=;+!##$()',*[]";
NSString *result = (__bridge_transfer NSString *)
CFURLCreateStringByAddingPercentEscapes(
NULL,
(__bridge CFStringRef)unencodedString,
NULL,
(__bridge CFStringRef)escape,
kCFStringEncodingUTF8);
XCTAssertEqualObjects(result, #"%C6%90", #"");
UPDATE
If you want a sample like this to work, use unichar and -stringWithCharacters:length:.
unichar res = ((unichar)(400));
NSString *unencodedString = [NSString stringWithCharacters:&res length:1];
XCTAssertEqualObjects(unencodedString, #"Ɛ", #"");
static NSString *escape = #":/?&=;+!##$()',*[]";
NSString *result = (__bridge_transfer NSString *)
CFURLCreateStringByAddingPercentEscapes(
NULL,
(__bridge CFStringRef)unencodedString,
NULL,
(__bridge CFStringRef)escape,
kCFStringEncodingUTF8);
XCTAssertEqualObjects(result, #"%C6%90", #"");
I'm nearing the end of my project, though after Analyzing my project in XCode it is indicating to me that there is a memory leak at this line:
Here is the text version of the relevant code:
- (void)displayPerson:(ABRecordRef)person
{
NSString* firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSMutableString *fullName = [NSString stringWithFormat:#"%# %#", firstName, lastName];
//NSLog(#"%#", fullName);
NSString* phoneNum = nil;
ABMultiValueRef phoneNumbers;
phoneNumbers = ABRecordCopyValue(person,
kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
phoneNum = (__bridge_transfer NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
} else {
phoneNum = #"Unknown";
}
NSLog(#"First name is %# and last name is %#", firstName, lastName);
NSLog(#"Phone is %#", phoneNum);
phoneNum = [phoneNum stringByReplacingOccurrencesOfString:#"(" withString:#""];
phoneNum = [phoneNum stringByReplacingOccurrencesOfString:#")" withString:#""];
Can anyone help me out with this? I don't believe it's crippling, but I don't want to give Apple a reason to reject my app from the store. Thank you.
Best...SL
You are using __bridge_transfer everywhere except for the phoneNumbers return value from ABRecordCopyValue.
You need to transfer ownership of phoneNumbers to ARC or manually release the memory.
UPDATE: Having looked at this issue a bit closer I'm not sure you can transfer ownership to ARC, see __bridge_transfer and ABRecordCopyValue: and ARC for more details.
Adding CFRelease(phoneNumbers) will manually release the memory.
For example:
NSString* phoneNum = nil;
ABMultiValueRef phoneNumbers;
phoneNumbers = ABRecordCopyValue(person,
kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
phoneNum = (__bridge_transfer NSString*) ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
} else {
phoneNum = #"Unknown";
}
CFRelease(phoneNumbers);
I am trying to activate APNS. Because uniqueIdentifier is deprecated, I'm trying to use CFUUIDCreate with the following code:
UIDevice *dev = [UIDevice currentDevice];
NSString *deviceUuid;
if ([dev respondsToSelector:#selector(uniqueIdentifier)])
deviceUuid = dev.uniqueIdentifier;
else {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
id uuid = [defaults objectForKey:#"deviceUuid"];
if (uuid)
deviceUuid = (NSString *)uuid;
else {
CFStringRef cfUuid = CFUUIDCreateString(NULL, CFUUIDCreate(NULL));
deviceUuid = (__bridge NSString *)cfUuid;
CFRelease(cfUuid);
[defaults setObject:deviceUuid forKey:#"deviceUuid"];
}
}
How can I replace uniqueIdentifier with CFUUIDCreate?
iOS < 6.0
// Create universally unique identifier (object)
CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);
// Get the string representation of CFUUID object.
NSString *uuidStr = (__bridge_transfer NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject);
CFRelease(uuidObject);
iOS >= 6.0
NSString* UDID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];