Update:
I believe the problem lies somewhere in how I am storing the reference to ABRecordRef. I am currently just hanging onto the value as delivered to peoplePickerNavigationController:shouldContinueAfterSelectingPerson:property:identifier: and not CFRetaining it or anything. It's unclear from the documentation if it needs to be retained.
I'm working on an iPhone app and it interfaces with the address book using the AddressBook and AddressBookUI frameworks. I'm using the ABPeoplePickerNavigationController to present a contact list to the user to choose, and am capturing the resultant ABRecordRef as an instance variable on a custom class.
This is all working fine on the first use. However, the second time I pick someone from the contacts (even a different person), my app blows up with EXC_BAD_ACCESS on a call to ABRecordCopyValue. I am logging the pointers and they are definitely different each time a contact is selected (even if the same contact twice).
I fail to understand how this reference could be deallocated. A memory leak sure, but why does it work fine the first time and not the second?
Here's the actual call it's dying on:
- (NSString*)displayName {
return CFBridgingRelease( ABRecordCopyValue( self.contact, kABPersonFirstNameProperty ) );
}
Here's some debug output if it's helpful at all:
Printing description of self->_contact:
(ABRecordRef) _contact = 0x1f582dc0
(lldb) expr (CFTypeRef)ABRecordCopyValue(self.contact, kABPersonFirstNameProperty)
error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0xd1f57cc1).
The process has been returned to the state before execution.
Turns out all I needed was to CFRetain( person ) and everything's happy go lucky again. I also added a dealloc to my class to clean up the pointer when the object goes away:
- (void)dealloc {
CFRelease( _contact );
}
My code now runs smoothly and the static analyser is happy (not that it caught the leak anyway).
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
[self displayPerson:person];
[self dismissViewControllerAnimated:YES completion:nil];
return NO;
}
you returning NO ?
Try checking to see if the value exists maybe
ie
- (void)displayPerson:(ABRecordRef)person
{
NSString* companyName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty);
NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString* display = #"";
if (companyName) {
display = companyName;
} else if (name) {
display = name;
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"No Details For Contact"
message:#"Please update contact with company and/or first name"
delegate:nil
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[alertView show];
}
}
Related
I am making iOS 9 Safari AdBlocker app.
I am using the module of AdBlockPlusSafari.
App works good on simulator.
But when try to run it on device(iPhone6), it fails to reload contentBlocker.
[SFContentBlockerManager
reloadContentBlockerWithIdentifier:self.contentBlockerIdentifier
completionHandler:^(NSError *error) {
if (error) {
NSLog(#"Error in reloadContentBlocker: %#", error);
}
dispatch_async(dispatch_get_main_queue(), ^{
wSelf.reloading = NO;
[wSelf checkActivatedFlag];
if (completion) {
completion(error);
}
});
}];
it gives error
Error Domain=ContentBlockerErrorDomain Code=3 "(null)"
It caused by accessing the values in NSUserDefault (App Group).
- (instancetype)init
{
if (self = [super init])
{
_bundleName = [[[[[NSBundle mainBundle] bundleIdentifier] componentsSeparatedByString:#"."] subarrayWithRange:NSMakeRange(0, 2)] componentsJoinedByString:#"."];
NSString *group = [NSString stringWithFormat:#"group.%#.%#", _bundleName, #"AdBlockerPro"];
NSLog(#"Group name: %#", group);
_adblockProDetails = [[NSUserDefaults alloc] initWithSuiteName:group];
[_adblockProDetails registerDefaults:
#{ AdblockProActivated: #NO,
AdblockProEnabled: #YES
}];
_enabled = [_adblockProDetails boolForKey:AdblockProEnabled];
_activated = [_adblockProDetails boolForKey:AdblockProActivated];
}
return self;
}
The App Group name in host app and safari extension is same.
But in Safari extension, when app accesses the setting in NSUserDefault, it gives me the error.
In Project setting/Capabilities, I did all for App Group. In app id, it involves app group name exactly.
This happens on only device. On simulator, it works good. I can't find the reason of this error.
Please help me if you are experienced this.
Looking forward to your help.
I found the reason myself.
I have put something (NSMutableDictionary) in app group container and did something to write file to extension bundle.
It is prohibited by Apple.
So I deleted all from AdBlockManager (interacts with app group) except flag variables (Boolean type).
And I proceeded the file management using NSFileManager.
http://www.atomicbird.com/blog/sharing-with-app-extensions
Finally, app extension is working for me on device.
Good luck!
I've an observer pattern on the UI that checks what's the status of an object that handles a server connection that's trying to update a certain field on a database.
The UI's update method receives an object containing data pairs containing the information of what's happening with the connection. The problem is that I'm getting tangled with a lot of ifs checking for different possibilities.
- (void) update:(Bundle *)arg
{
if ([[arg getData:#"updatee"] isEqualToString:#"email"]){
UITableViewCell *emailCell = [[self tableView] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
if ([[arg getData:#"connecting"] isEqualToString:#"true"]) {
//Email is being posted
[_emailLabel_email setText:#"Connecting..."];
[_emailLabel_set setHidden:YES];
emailCell.accessoryType = UITableViewCellAccessoryNone;
[_emailActivityIndicator startAnimating];
}else{
if ([[arg getData:#"succesfull"] isEqualToString: #"false"])
//Email was posted unsuccesfully
[[[UIAlertView alloc] initWithTitle:#"Taken Email Address"
message:#"The email address that you entered is already in use, please double check it"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] show];
else{
//Email was posted succesfully.
[_emailLabel_set setText:#"Change"];
}
[_emailActivityIndicator stopAnimating];
[_emailLabel_email setText:[mng getEmail]];
[_emailLabel_set setHidden:NO];
emailCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
}
//Password cases
}
}
As the server responds with a string I'm finding difficult to avoid this spaggetti of code.
Which would be the smarter object to send on the update method?
You could keep the data how it is and put all of the values you expect for [arg getData:#"updatee"] as the keys in a dictionary. The values would be the string representation of method selectors, where each of the methods handles one of the cases in your current if statement. Now your update: method just gets the selector string from the dictionary, converts it to a real selector (NSSelectorFromString) and calls the method (passing the arg as a parameter, all methods used here must have matching parameter listing). If you receive an unexpected update type nothing would happen so you should log / assert that. If you get an exception while translating the selector you probably have a typo (good candidate for a test which executes all of the methods in the configuration).
Of course you could basically do the same thing with all the methods and a dirty big if / else statement.
in my code I am retrieving the users reminders and displaying them a tableview.
Whenever I print NSLog in my code, the the tables array gets displayed properly. But whenever I leave out NSLog in my code, my data isn't displayed properly anymore. The value for "title" of the reminder returns (null).
I can reproduce the issue every time. Anyone have any idea what is happening here?
- (void)shareReminderButtonAction:(UIButton *)buttonEvent {
EKEventStore *store = [[EKEventStore alloc] init];
NSPredicate *predicate = [store predicateForRemindersInCalendars:nil];
[store requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {
[store fetchRemindersMatchingPredicate:predicate completion:^(NSArray *reminders) {
ReminderView *shareRem = [[ReminderView alloc]init];
NSLog(#"Reminders: %#", reminders);
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:shareRem];
[navigation setNavigationBarHidden:YES];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:navigation animated:YES
completion:^(void){[shareRem setArray:reminders];}];
}];
}];
}
Log data with NSLog in my code above:
"EKReminder <0x1d5b3630> {title = Gdghv; dueDate = 2013-03-06 22:00:00 +0000; completionDate = (null); priority = 0; calendarItemIdentifier = 0BD2CB76-588B-4103-86B0-D71D22317DC0}"
Log data at the UITableViewController end when receiving data without the NSLog in my code above:
CADObjectGetInlineStringProperty failed fetching title for EKPersistentReminder with error Error Domain=NSMachErrorDomain Code=268435459 "The operation couldn\342\200\231t be completed. (Mach error 268435459 - (ipc/send) invalid destination port)"
"EKReminder <0x1c5e6fd0> {title = (null); dueDate = (null); completionDate = (null); priority = 0; calendarItemIdentifier = (null)}"
Thanks alot!
The EKEventStore object needs to be kept around for the entire time you are using Event Kit objects. It's normally best to house it within a singleton. From Apple's docs:
An EKEventStore object requires a relatively large amount of time to
initialize and release. Consequently, you should not initialize and
release a separate event store for each event-related task. Instead,
initialize a single event store when your app loads, and use it
repeatedly to ensure that your connection is long-lived.
An event store instance must not be released before other Event Kit
objects; otherwise, undefined behavior may occur.
Consider the following method and the caller code block. The method analyses a NSString
and extracts a "http://" string which it passes out by reference as an auto release object.
Without releasing g_scan_result, the program works as expected. But according to non-arc rules, g_scan_result should be released since a retain has been called against it.
My question are :
Why g_scan_result cannot be released ?
Is there anything wrong the way g_scan_result is handled in the posted coding below ?
Is it safe not to release g_scan_result as long as the program runs correctly and the XCode Memory Leak tool does not show leakage ?
Which XCode profile tools should I look into to check and under which subtitle ?
Hope somebody knowledgeable could help.
- (long) analyse_scan_result :(NSString *)scan_result target_url :(NSString **)targ_url {
NSLog (#" RES analyse string : %#", scan_result);
NSRange range = [scan_result rangeOfString:#"http://"
options:NSCaseInsensitiveSearch];
if (range.location == NSNotFound) {
*targ_url = #"";
NSLog(#"fnd string not found");
return 0;
}
NSString *sub_string = [scan_result substringFromIndex : range.location];
range = [sub_string rangeOfString : #" "];
if (range.location != NSNotFound) {
sub_string = [sub_string substringToIndex : range.location];
}
NSLog(#" fnd sub_string = %#", sub_string);
*targ_url = sub_string;
return [*targ_url length];
}
The following is the caller code block, also note that g_scan_result has been declared and initialized (on another source file) as :
NSString *g_scan_result = nil;
Please do send a comment or answer if you have suggestions or find possible errors in code posted here (or above). Xcode memory tools does not seem to show any memory leak. But it may be because I do not know where to look as am new to the memory tools.
{
long url_leng = [self analyse_scan_result:result target_url:&targ_url];
NSLog(#" TAR target_url = %#", targ_url);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Scanned Result"
message:result
delegate:g_alert_view_delegate
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
if (url_leng) {
// ****** The 3 commented off statements
// ****** cannot be added without causing
// ****** a crash after a few scan result
// ****** cycles.
// ****** NSString *t_url;
if (g_system_status.language_code == 0)
[alert addButtonWithTitle : #"Open"];
else if (g_system_status.language_code == 1)
[alert addButtonWithTitle : #"Abrir"];
else
[alert addButtonWithTitle : #"Open"];
// ****** t_url = g_scan_result;
g_scan_result = [targ_url retain];
// ****** [t_url release];
}
targ_url = nil;
[alert show];
[alert release];
[NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:#selector(activate_qr_scanner:)
userInfo:nil
repeats:NO
];
return;
}
I think the mystery has been solved. Thanks for all who are kind enough to have looked into this. The reason is before adding the UIAlertView, coding had been done (on another source file) to assign a raw output string to g_scan_result and display it directly on the current view. So when g_scan_result got released, it is releasing the wrong NSString assigned by some outdated code.
In summary, the wrong NSString got released which is the source of the problem.
The solution is just to remove a single outdated statement. The statement from the old implementation was left there as I thought it wouldn't do any harm (and may even helped to make the variable populated continuously). But it turned out to be a very silly mistake. The only excuse is having very little sleep lately. Being able to find an excuse does serve a purpose. Just hope that it doesn't have to be done very often ...
I've looked over the site for a while and I saw a few things but nothing worked for me. Extreme newbie here though.
I'm trying to make a 2 component picker, populated from arrays, populated from a dictionary, populated from a plist. The components populate, and I have a button that spins one component. The second button is meant to check answers (the first component has states which they try to match with capitals in the second component), but this is where it always crashes.
The error I get is as follows:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSPlaceholderString initWithFormat:locale:arguments:]: nil argument'
* First throw call stack:
(0x14b3022 0xeb3cd6 0x145ba48 0x145b9b9 0x916169 0x916ab4 0x3036 0x14b4e99 0x1914e 0x190e6 0xbfade 0xbffa7 0xbf266 0x3e3c0 0x3e5e6 0x24dc4 0x18634 0x139def5 0x1487195 0x13ebff2 0x13ea8da 0x13e9d84 0x13e9c9b 0x139c7d8 0x139c88a 0x16626 0x27ad 0x2715)
terminate called throwing an exception(lldb)
I'm not sure where I'm going wrong. I've done this with hard coded arrays, and it works fine. The code for the button is like so (please ignore the switch code, I haven't quite figured out how to make that work yet, I want the screen to turn green for correct answers and red for incorrect, but I'm more concerned with the app crashing!):
-(IBAction)checkAnswer;
{
NSInteger StateRow = [pickerCapital selectedRowInComponent:karrayStateComponent];
NSInteger CapitalRow =[pickerCapital selectedRowInComponent:karrayCapitalComponent];
NSString *state = [arrayState objectAtIndex:StateRow];
NSString *capital = [arrayCapital objectAtIndex:CapitalRow];
NSLog (#"%i",[pickerCapital selectedRowInComponent:karrayCapitalComponent]);
NSLog(#"%i", [pickerCapital selectedRowInComponent:karrayStateComponent]);
NSString *Title = [[NSString alloc]initWithFormat:#"You have selected %# as the capital of %#.", capital, state];
NSString *Message =[[NSString alloc]initWithFormat:lblMsg];
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:Title message:Message delegate:#"" cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
if (StateRow == CapitalRow) {
lblMsg = #"You are correct!";
UIColor *myColor;
switch ([not sure what to use here]) {
case 0:
myColor = [UIColor greenColor];
break;
default:
break;
}
}
else {
lblMsg = #"Incorrect";
UIColor *myColor;
switch ([not sure what to use here]) {
case 0:
myColor = [UIColor redColor];
break;
default:
break;
}
}
}
If anyone can offer any sort of help on this, I would appreciate it greatly! Thank you!
Two things to check:
Where is lblMsg declared or assigned a value?
Are you sure capital and state are not nil from the calls to [arrayCapital objectAtIndex]?
Specifically: the NSString documentation states "Important: Raises an NSInvalidArgumentException if format is nil." for initWithFormat.