Crashing in MFMailComposeViewController implementation - mfmailcomposer

My app crashes when I try to perform sent or cancel button actions in MFMailComposeViewController.
here is my code.
I have imported message framework and added delegates in .h file.
NSString *emailTitle = #"Test Email";
NSString *messageBody = #"Welcome Guest";
NSArray *recipentsArray = [[NSArray alloc]initWithObjects:#"xxx#gmail.com", nil];
[[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:nil];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:recipentsArray];
[self presentViewController:mc animated:YES completion:NULL];(void) mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(#"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail sent failure: %#", [error localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}
This is how my mf mail composer looks like :----

Just make the MFMailComposeViewController *mc in global.
I mean declare it outside this method. It's crashing because at the end of method Your mc gets deallocated.
Interface
#interface Demo()
{
MFMailComposeViewController *mc;
}
#end
Implementation
-(void)ShareViaEmail {
objMailComposeController = [[MFMailComposeViewController alloc] init];
objMailComposeController.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]) {
[objMailComposeController setSubject:#"Hello"];
[objMailComposeController setMessageBody:#"This is Body of Message" isHTML:NO];
NSData *ImageData = [NSData dataWithContentsOfFile:self.aStrSourceName];
NSString *mimeType;
if ([[self CheckExtensionOfURL:self.aStrSourceName]isEqualToString:#"Image"]) {
mimeType = #"image/png";
}
else if ([[self CheckExtensionOfURL:self.aStrSourceName]isEqualToString:#"PDF"]) {
mimeType = #"application/pdf";
} else if ([[self CheckExtensionOfURL:self.aStrSourceName]isEqualToString:#"Audio"]) {
mimeType = #"audio/mpeg";
} else if ([[self CheckExtensionOfURL:self.aStrSourceName]isEqualToString:#"Video"]) {
mimeType = #"video/mp4";
}
[objMailComposeController addAttachmentData:ImageData mimeType:mimeType fileName:#"attachement"];
}
[self.navigationController presentViewController:objMailComposeController animated:YES completion:Nil];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
switch (result) {
case MFMailComposeResultCancelled:
[self ShowAlertView:kMsgMailCancelled];
break;
case MFMailComposeResultFailed:
[self ShowAlertView:kMsgMailFailed];
break;
case MFMailComposeResultSaved:
[self ShowAlertView:kMsgMailSaved];
break;
case MFMailComposeResultSent:
[self ShowAlertView:kMsgSent];
break;
default:
break;
}
// CHECK OUT THIS, THIS MIGHT BE IN YOUR CASE
[self.navigationController dismissViewControllerAnimated:controller completion:nil];
}

Related

How do I open an NSSheet in Mavericks?

In Mavericks, the methods to open and close NSSheets has changed. And to make it tougher, the Release Notes do not match the current documentation (see below).
I'm trying to do this:
MainSheetController (NSWindowController):
-(IBAction)callSheet:(id)sender {
[sheetController openSheet];
}
SheetController (NSObject):
(void)openSheet {
[[NSBundle mainBundle] loadNibNamed:sheetName owner:self topLevelObjects:nil];
NSLog(#"1");
[self.mainWindowController.window beginSheet:self.sheet completionHandler:nil];
NSLog(#"2");
}
I get to 2, with no errors or warnings, but no sheet..
Current Documentation:
#if NS_BLOCKS_AVAILABLE
- (void)beginSheet:(NSWindow *)sheetWindow completionHandler:(void (^)(NSModalResponse returnCode))handler NS_AVAILABLE_MAC(10_9);
- (void)beginCriticalSheet:(NSWindow *)sheetWindow completionHandler:(void (^)(NSModalResponse returnCode))handler NS_AVAILABLE_MAC(10_9);
#endif
- (IBAction)userButtonPressed:(id)sender {
UserLoginWindowController * wc = [UserLoginWindowController new];
// we keep a reference, so the WC doesn't deallocate
self.modalWindowController = wc;
[[self window] beginSheet:[wc window] completionHandler:^(NSModalResponse returnCode) {
self.modalWindowController = nil;
}];
}
in the UserLoginWindowController
- (IBAction)cancelButtonPressed:(id)sender {
[[[self window] sheetParent] endSheet:[self window] returnCode:NSModalResponseCancel];
}
- (IBAction)showSheet:(id)sender
{
if (_windowController == nil)
{
_windowController = [MyWindowController new];
}
[[self window] beginSheet:[_windowController window] completionHandler:^(NSModalResponse returnCode)
{
}];
}
// And inside your MyWindowController class:
- (id)init
{
self = [super initWithWindowNibName:#"MyWindowNibName"]; // TODO: Change the name of your NIB
return self;
}
In your nib file, make sure the "Visible At Launch" flag is unticked for the window.
I figured out how to do this. Hope it's ok to post..
MainWindow.h
#interface MainWindowController : NSWindowController {
NSString *sheetName;
IBOutlet NSWindow *sheet;
id result1;
id result2;
...
id resultn;
}
#property (strong) NSString *sheetName;
#property (strong, nonatomic) IBOutlet NSWindow *sheet;
-(IBAction)callSheet0:(id)sender;
-(IBAction)callSheet1:(id)sender;
-(IBAction)callSheetn:(id)sender;
- (void)openSheet;
- (IBAction)save:(id)sender;
- (IBAction)cancel:(id)sender;
MainWindow.m
- (void)windowDidLoad
{
NSLog(#"%s", __FUNCTION__);
[super windowDidLoad];
sheetName = [[NSString alloc] init];
}
-(IBAction)callSheet0:(id)sender {
NSLog(#"%s", __FUNCTION__);
sheetName = #"Sheet0";
[self openSheet];
}
....
-(IBAction)callSheetn:(id)sender {
NSLog(#"%s", __FUNCTION__);
sheetName = #"Sheetn";
[self openSheet];
- (void)openSheet {
NSLog(#"%s", __FUNCTION__);
NSLog(#"sheetName: %#",sheetName );
[[NSBundle mainBundle] loadNibNamed:sheetName owner:self topLevelObjects:nil];
[self.window beginSheet:self.sheet completionHandler:nil];
}
- (void)save:(NSButton*)sender {
switch ([sender tag]) {
case 0:
[self doSave1];
result = #"1";
break;
case 1:
[self doSave2];
result = #"2";
break;
case n:
[self doSaven];
result = #"x";
break;
}
[self endSheet:self.sheet returnCode:result];
}
- (IBAction)cancel:(id)sender {
NSLog(#"%s", __FUNCTION__);
result = #"0";
[self endSheet:self.sheet returnCode:result];
// returnCode is optional
}
//endSheet:(NSWindow *)sheetWindow {
- (void)endSheet:(NSWindow *)sheetWindow returnCode:returnCode {
//NSLog(#"%s", __FUNCTION__);
[sheetWindow orderOut:self];
}
- (void)save:(NSButton*)sender {
switch ([sender tag]) {
case 0:
[self doSave1];
result = #"1";
break;
case n:
[self doSave3];
result = #"3";
break;
}
[self endSheet:self.sheet returnCode:result];
}
With this method, new in 10.9,I don't need a special sheet controller, and control remains quote local.

Make new entry for NSOutlineView focused and editable

When a new item is created in the NSOutlineView, I would like to have the item selected and editable, I'm currently using the following code within my NSOutlineview delegate/datasource, the new item is created, however it isn't focused/ editable.
NSWindow *w = [[self outlineView] window];
BOOL endEdit = [w makeFirstResponder:w];
if (!endEdit) {
return;
}
NSUInteger row = [[self outlineView] rowForItem:newGoal];
[[self outlineView] scrollRowToVisible:row];
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:row];
[[self outlineView] selectRowIndexes:indexSet byExtendingSelection:NO];
[[self outlineView] editColumn:0
row:row
withEvent:nil
select:YES];
NSError *anyError = nil;
BOOL success = [[self myContext] save:&anyError];
if (!success) {
NSLog(#"Error = %#", anyError);
}
My complete file is
//
// SBOutlineViewController.m
// Allocator
//
// Created by Cory Sullivan on 2013-04-21.
//
//
#import "SBOutlineViewController.h"
#import "SBAppDelegate.h"
#interface SBOutlineViewController ()
#end
NSString * const SBOutlineSelectionChangedNotification = #"SBSelectionChanged";
#implementation SBOutlineViewController
- (void)awakeFromNib
{
[self setTopLevelItems:[NSArray arrayWithObjects:#"Retirement", #"Education", nil]];
[self setMyContext:[[NSApp delegate] managedObjectContext]];
[self updateOutlineView];
}
- (void)updateOutlineView
{
NSFetchRequest *myRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *goalEntity = [NSEntityDescription entityForName:#"Goal" inManagedObjectContext:[self myContext]];
[myRequest setEntity:goalEntity];
NSError *anyError = nil;
[self setGoalItems:[[self myContext] executeFetchRequest:myRequest error:&anyError]];
NSMutableArray *retirementArray = [[NSMutableArray alloc] init];
NSMutableArray *educationArray = [[NSMutableArray alloc] init];
for (NSManagedObject *goalObject in [self goalItems]) {
if ([[goalObject valueForKey:#"goalType"] isEqual: #"Retirement"]) {
[retirementArray addObject:goalObject];
} else {
if ([[goalObject valueForKey:#"goalType"] isEqual:#"Education"]) {
[educationArray addObject:goalObject];
}
}
}
[self setMyOutlineDictionary:[[NSMutableDictionary alloc] initWithObjectsAndKeys:retirementArray, #"Retirement", educationArray, #"Education", nil]];
[[self outlineView] reloadData];
}
#pragma mark - Delegate and DataSoure Methods
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
return [[self _childrenForItem:item] objectAtIndex:index];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
if ([outlineView parentForItem:item] == nil) {
return YES;
} else {
return NO;
}
}
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
{
if ([outlineView parentForItem:item] == nil) {
return NO;
} else {
return YES;
}
}
- (NSInteger) outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
return [[self _childrenForItem:item] count];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item {
return [_topLevelItems containsObject:item];
}
- (void)outlineViewSelectionDidChange:(NSNotification *)notification {
if ([_outlineView selectedRow] != -1) {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:SBOutlineSelectionChangedNotification object:self];
}
}
- (NSArray *)_childrenForItem:(id)item {
NSArray *children;
if (item == nil) {
children = _topLevelItems;
} else {
children = [_myOutlineDictionary objectForKey:item];
}
return children;
}
- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
NSTableCellView *view = nil;
if ([_topLevelItems containsObject:item]) {
view = [outlineView makeViewWithIdentifier:#"HeaderCell" owner:self];
NSString *value = [item uppercaseString];
[[view textField] setStringValue:value];
return view;
} else {
view = [outlineView makeViewWithIdentifier:#"DataCell" owner:self];
[[view imageView] setImage:[NSImage imageNamed:NSImageNameActionTemplate]];
NSManagedObject *goalItem = [_goalItems objectAtIndex:[_goalItems indexOfObject:item]];
[[view textField] setStringValue:[goalItem valueForKey:#"goalName"]];
return view;
}
}
#pragma mark - Custom Actions
- (IBAction)goalActionPullDown:(id)sender
{
[self modifyGoalItems: [[sender selectedItem] tag]];
}
- (void)modifyGoalItems:(NSInteger)whichMenuTag
{
if (whichMenuTag == 5) {
NSAlert *alert = [NSAlert alertWithMessageText:#"Are you sure you want to delete goal and all related accounts?"
defaultButton:#"Remove"
alternateButton:#"Cancel"
otherButton:nil
informativeTextWithFormat:#"This can't be undone"];
[alert beginSheetModalForWindow:[_outlineView window]
modalDelegate:self
didEndSelector:#selector(alertEnded:code:context:)
contextInfo:NULL];
} else {
NSManagedObject *newGoal = [NSEntityDescription insertNewObjectForEntityForName:#"Goal" inManagedObjectContext:[self myContext]];
switch (whichMenuTag) {
case 1:
{
[newGoal setValue:#"New Goal" forKey:#"goalName"];
[newGoal setValue:#"Retirement" forKey:#"goalType"];
[self updateOutlineView];
}
break;
case 2:
{
[newGoal setValue:#"New Goal" forKey:#"goalName"];
[newGoal setValue:#"Education" forKey:#"goalType"];
[self updateOutlineView];
}
break;
case 3:
NSLog(#"You selected Item number 3");
break;
case 4:
NSLog(#"You selected Item number 4");
break;
default:
break;
}
NSWindow *w = [[self outlineView] window];
BOOL endEdit = [w makeFirstResponder:w];
if (!endEdit) {
return;
}
NSUInteger row = [[self outlineView] rowForItem:newGoal];
[[self outlineView] scrollRowToVisible:row];
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:row];
[[self outlineView] selectRowIndexes:indexSet byExtendingSelection:NO];
[[self outlineView] editColumn:0
row:row
withEvent:nil
select:YES];
NSError *anyError = nil;
BOOL success = [[self myContext] save:&anyError];
if (!success) {
NSLog(#"Error = %#", anyError);
}
}
}
- (BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
return YES;
}
- (void)alertEnded:(NSAlert *)alert
code:(int)choice
context:(void *)v
{
if (choice == NSAlertDefaultReturn) {
NSManagedObject *selectedGoal = [[self outlineView] itemAtRow:[[self outlineView] selectedRow]];
[[self myContext] deleteObject:selectedGoal];
NSError *anyError = nil;
BOOL success = [[self myContext] save:&anyError];
if (!success) {
NSLog(#"Error = %#", anyError);
}
[self updateOutlineView];
}
}
#end
Any attempt to do make the field editable within the current pass through the run loop will fail. What you need to do is to cue the selection up for the next time around the loop. Something like this:
[self performSelector:#selector(selectText:) withObject:textField afterDelay:0];
You might want to do something other than selectText on a textfield, but the technique is what you need.

MFMailComposeViewController crash when pressing "Delete Draft"

I have a button that call a MFMailComposeViewController. Sometimes, when I click "delete draft", the app crashes but sometimes it correctly dismiss the view. I can' understand why. Here is my code:
- (IBAction)openEmail:(id)sender {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
composer.mailComposeDelegate = (id)self;
NSArray *myEmail = [[NSArray alloc] initWithObjects:#"me#gmail.com", nil];
[composer setToRecipients:myEmail];
[self presentViewController:composer animated:YES completion:nil];
[[composer navigationBar] setTintColor:[UIColor colorWithRed:0.843 green:0.435 blue:0.435 alpha:1]];
}
else {
}
}
and this method:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(#"Mail send");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail failed");
break;
default:
NSLog(#"Mail not sent.");
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
Can anyone help me?
I used to experience this as well, and the potential problematic area in your code may be this line:
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
It's likely that by the time MFMailComposeViewControllerDelegate methods are called, your composer object is deallocated. Instead, try initiating MFMailComposeViewController on a strong property, like so:
self.composer = [MFMailComposeViewController new];
You can set self.composer to nil once you have acted on the delegate methods to release the MFMailComposeViewController.
Have you tried:
[self presentModalViewController:_picker animated:YES];
Notice 'MODAL'?
With also:
[self dismissModalViewControllerAnimated:YES];

UICollection View & performBatchUpdate

I a trying to build an application where i can use UICollectionviewcontroller & NSFetchresultcontroller , i found the following link "https://github.com/AshFurrow/UICollectionView-NSFetchedResultsController/blob/master/AFMasterViewController.m
Here you can find the Code
#pragma mark - UICollectionVIew
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
return [sectionInfo numberOfObjects];
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
AFCollectionViewCell *cell = (AFCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
#warning Unimplement Cell Configuration
return cell;
}
#pragma mark - Fetched results controller
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
#warning Unimplemented fetched results controller
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:<#entity#>inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:<#batch size#>];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:<#descriptor#> ascending:NO];
NSArray *sortDescriptors = #[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:#"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
NSMutableDictionary *change = [NSMutableDictionary new];
switch(type) {
case NSFetchedResultsChangeInsert:
change[#(type)] = #[#(sectionIndex)];
break;
case NSFetchedResultsChangeDelete:
change[#(type)] = #[#(sectionIndex)];
break;
}
[_sectionChanges addObject:change];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
NSMutableDictionary *change = [NSMutableDictionary new];
switch(type)
{
case NSFetchedResultsChangeInsert:
change[#(type)] = newIndexPath;
break;
case NSFetchedResultsChangeDelete:
change[#(type)] = indexPath;
break;
case NSFetchedResultsChangeUpdate:
change[#(type)] = indexPath;
break;
case NSFetchedResultsChangeMove:
change[#(type)] = #[indexPath, newIndexPath];
break;
}
[_objectChanges addObject:change];
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
if ([_sectionChanges count] > 0)
{
[self.collectionView performBatchUpdates:^{
for (NSDictionary *change in _sectionChanges)
{
[change enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, id obj, BOOL *stop) {
NSFetchedResultsChangeType type = [key unsignedIntegerValue];
switch (type)
{
case NSFetchedResultsChangeInsert:
[self.collectionView insertSections:[NSIndexSet indexSetWithIndex:[obj unsignedIntegerValue]]];
break;
case NSFetchedResultsChangeDelete:
[self.collectionView deleteSections:[NSIndexSet indexSetWithIndex:[obj unsignedIntegerValue]]];
break;
case NSFetchedResultsChangeUpdate:
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:[obj unsignedIntegerValue]]];
break;
}
}];
}
} completion:nil];
}
if ([_objectChanges count] > 0 && [_sectionChanges count] == 0)
{
[self.collectionView performBatchUpdates:^{
for (NSDictionary *change in _objectChanges)
{
[change enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, id obj, BOOL *stop) {
NSFetchedResultsChangeType type = [key unsignedIntegerValue];
switch (type)
{
case NSFetchedResultsChangeInsert:
[self.collectionView insertItemsAtIndexPaths:#[obj]];
break;
case NSFetchedResultsChangeDelete:
[self.collectionView deleteItemsAtIndexPaths:#[obj]];
break;
case NSFetchedResultsChangeUpdate:
[self.collectionView reloadItemsAtIndexPaths:#[obj]];
break;
case NSFetchedResultsChangeMove:
[self.collectionView moveItemAtIndexPath:obj[0] toIndexPath:obj[1]];
break;
}
}];
}
} completion:nil];
}
[_sectionChanges removeAllObjects];
[_objectChanges removeAllObjects];
}
#end
I have the Following error
NSFetchedResultsController during a call to -controllerDidChangeContent:. -[__NSArrayI unsignedIntegerValue]
The NS Log for the _sectionsChange give me the following output
1 = (
6
);
I am trying to know how to solve this problem , so any help would be appreciated
Thank you
NO , the Error was fixed after i updated the following code:
{
case NSFetchedResultsChangeInsert:
[self.collectionView insertItemsAtIndexPaths:#[obj]];
[self.collectionView.window endEditing:YES];
break;
}
Instead of case NSFetchedResultsChangeInsert:
[self.collectionView insertItemsAtIndexPaths:#[obj]];
break;
Then delete all objects , create everything from scratch , problem solved !

When I use NSRunLoop Instruments gave a leak

When I use the following code I was told there is a leak:
- (void)dealloc
{
[connection release], connection = nil;
[responseData release],responseData = nil;
[cityCode release], cityCode = nil;
[requestUrlString release], requestUrlString = nil;
[returnDataDic release], returnDataDic = nil;
[super dealloc];
}
- (id)initWithCityCode:(NSString *)aCityCode
requestURL:(NSString*)urlString
responseType:(SWEngineRequestType)theResponsetype
target:(id)theTarget
action:(SEL)theAction
{
if ((self = [super init]))
{
_isExecuting = NO;
_isFinished = NO;
target = theTarget;
action = theAction;
cityCode = [aCityCode retain];
requestUrlString = [urlString copy];
responseType = theResponsetype;
returnDataDic = [[NSMutableDictionary alloc] initWithCapacity:1];
if (cityCode)
{
[returnDataDic setObject:cityCode forKey:SWEATHER_CITYCODE];
}
[returnDataDic setObject:[NSNumber numberWithInt:responseType] forKey:SWEATHER_DOWNTYPE];
}
return self;
}
- (BOOL)isConcurrent
{
return YES;
}
- (void)finish
{
[self willChangeValueForKey:#"isExecuting"];
[self willChangeValueForKey:#"isFinished"];
_isExecuting = NO;
_isFinished = YES;
[self didChangeValueForKey:#"isExecuting"];
[self didChangeValueForKey:#"isFinished"];
[connection release], connection = nil;
[responseData release],responseData = nil;
[cityCode release], cityCode = nil;
[requestUrlString release], requestUrlString = nil;
[returnDataDic release], returnDataDic = nil;
done = YES;
}
- (BOOL)isExecuting
{
return _isExecuting;
}
- (BOOL)isFinished
{
return _isFinished;
}
- (void)main
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
done = NO;
if ([self isCancelled])
{
[self willChangeValueForKey:#"isFinished"];
_isFinished = YES;
[self didChangeValueForKey:#"isFinished"];
[pool release];
return;
}
[self willChangeValueForKey:#"isExecuting"];
_isExecuting = YES;
[self didChangeValueForKey:#"isExecuting"];
NSURL * urlToDownLoad = [NSURL URLWithString:[requestUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:urlToDownLoad cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection)
{
responseData = [[NSMutableData alloc] init];
[connection start];
}
else
{
[self finish];
}
if (connection != nil)
{
do
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
while (!done);
}
[pool release], pool = nil;
}
#pragma mark -
#pragma mark - NSURLConnectionDataDelegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[returnDataDic setObject:#"error" forKey:...];
[target performSelectorOnMainThread:action withObject:returnDataDic waitUntilDone:NO];
[self finish];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[returnDataDic setObject:responseData forKey:...];
[target performSelectorOnMainThread:action withObject:returnDataDic waitUntilDone:NO];
[self finish];
}
#end
the instrument gave me a leak at: [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; Why? Thanks!
I just want to have a Asynchronous download at a operation but I use the NSAutoreleasePool then the instrument gave a leak at the:[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];.
Try putting your
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
in an autorelease pool.
What object did Instruments identify as having leaked? Where was it allocated? What was the stack trace?
When you run a run loop, all of the run loop sources and run loop observers may fire. So those few lines of code hide a near infinite set of possible things happening as the run loop runs. Any one of those may have a leak, or Instruments may be mistaken.
It is usually a bad idea to run the run loop in the default mode in an inner loop. It's not clear what you're trying to do with that loop, but generally you should schedule any of your own run loop sources in a private run loop mode and then run the run loop in that mode. That way, you're sure that only your own sources get run, which is usually what you want.

Resources