【问题标题】:MGTwitterEngine - Status updatesMGTwitterEngine - 状态更新
【发布时间】:2010-10-07 01:44:18
【问题描述】:

我发现在更新状态时,iPhone 上随机出现 EXC_BAD_ACCESS 错误。这是随机发生的。

有人知道如何解决这个问题吗?

#import "TwitterViewController.h"

NSString *_testUID = nil;
NSString *sImageName;

@implementation TwitterViewController

//Turns NSLogs into comments
//#define NSLog //

- (void)viewDidLoad {

    self.title = @"Twitter";

    arrayEmotes = [[NSMutableArray alloc] init];
    [arrayEmotes addObject:@"Happy"];
    [arrayEmotes addObject:@"Sad"];
    [arrayEmotes addObject:@"Tongue"];
    [arrayEmotes addObject:@"Drunk"];
    [arrayEmotes addObject:@"Bored"];
    [arrayEmotes addObject:@"Love"];
    [arrayEmotes addObject:@"Sleepy"];
    [arrayEmotes addObject:@"Sick"];
    [arrayEmotes addObject:@"Awake"];
    [arrayEmotes addObject:@"Shocked"];
    [arrayEmotes addObject:@"Angry"];
    [arrayEmotes addObject:@"Laughing"];
    [arrayEmotes addObject:@"Dancing"];
    [arrayEmotes addObject:@"Confused"];

    [activityView startAnimating];
    [currentActivity setText:@"Logging In"];

    NSString *username = [[NSUserDefaults standardUserDefaults] stringForKey:@"username_preference"];
    NSString *password = [[NSUserDefaults standardUserDefaults] stringForKey:@"password_preference"];

    // Make sure you entered your login details before running this code... ;)
    if ([username isEqualToString:@""] || [password isEqualToString:@""]) {
        //Show the UIAlert if no username or password is stored in the settings
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Incorrect username/password stored in the settings." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];        
        NSLog(@"You forgot to specify your username/password in settings.bundle!");
    }

    // Create a TwitterEngine and set our login details.
    twitterEngine = [[[MGTwitterEngine alloc] initWithDelegate:self] retain];
    [twitterEngine setUsername:username password:password];

    // Get updates from people the authenticated user follows.
    //[twitterEngine getFollowedTimelineFor:username since:nil startingAtPage:0];
    _testUID = [twitterEngine testService];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
    return 1;
}


- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    return [arrayEmotes count];
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [arrayEmotes objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSLog(@"Selected Color: %@. Index of selected color: %i", [arrayEmotes objectAtIndex:row], row);
    [btnUpdateMood setEnabled:YES];
}

- (IBAction)updateMood:(id)sender
{   

    NSLog(@"Tried to send status.");

    NSInteger selectedindex = [pickerView selectedRowInComponent:0];
    switch(selectedindex){
        case 0:
            //Happy
            sImageName = @"Happy";
            break;
        case 1:
            //Sad
            sImageName = @"Sad";
            break;
        case 2:
            //Tongue
            sImageName = @"Tounge";
            break;
        case 3:
            //Drunk
            sImageName = @"Drunk";
            break;
        case 4:
            //Bored
            sImageName = @"Bored";
            break;
        case 5:
            //Love
            sImageName = @"Love";
            break;
        case 6:
            //Sleepy
            sImageName = @"Sleepy";
            break;
        case 7:
            //Sick
            sImageName = @"Sick";
            break;
        case 8:
            //Awake
            sImageName = @"Awake";
            break;
        case 9:
            //Shocked
            sImageName = @"Shocked";
            break;
        case 10:
            //Angry
            sImageName = @"Angry";          
            break;
        case 11:
            //Laughing
            sImageName = @"Laughing";           
            break;
        case 12:
            //Dancing
            sImageName = @"Dancing";
            break;
        case 13:
            //Confused
            sImageName = @"Confused";
            break;
        default: break;
    }   

    [twitterEngine sendUpdate:[@"has changed his/her iMood to " stringByAppendingString:sImageName]];

}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)dealloc {
    //Releasing seems to sometimes cause complete errors.
    //[twitterEngine release];
    [super dealloc];
}

#pragma mark MGTwitterEngineDelegate methods


- (void)requestSucceeded:(NSString *)requestIdentifier
{
    [activityView stopAnimating];

    //Some animations
    [UIView beginAnimations:@"redToWhite" context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:1];
    [UIView setAnimationRepeatCount:0];

    //Change background colour of the activity area over 1 second
    [activityArea setBackgroundColor:[UIColor whiteColor]];
    [currentActivity setText:@"Logged In"];

    [UIView commitAnimations];
    //End of animations

    NSLog(@"Request succeeded (%@)", requestIdentifier);

    if ([requestIdentifier isEqualToString:_testUID])
    {
        NSLog(@"[TWITTER UP]");
    }

}


- (void)requestFailed:(NSString *)requestIdentifier withError:(NSError *)error
{

    currentActivity.text = [NSString stringWithFormat:@"Error: %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSErrorFailingURLStringKey]];
    NSLog(@"Twitter request failed! (%@) Error: %@ (%@)", 
          requestIdentifier, 
          [error localizedDescription], 
          [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);

}


- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)identifier
{
    NSLog(@"Got statuses:\r%@", statuses);
}


- (void)directMessagesReceived:(NSArray *)messages forRequest:(NSString *)identifier
{

    NSLog(@"Got direct messages:\r%@", messages);
}


- (void)userInfoReceived:(NSArray *)userInfo forRequest:(NSString *)identifier
{
    NSLog(@"Got user info:\r%@", userInfo);
}


- (void)miscInfoReceived:(NSArray *)miscInfo forRequest:(NSString *)identifier
{
    NSLog(@"Got misc info:\r%@", miscInfo);
}


- (void)imageReceived:(UIImage *)image forRequest:(NSString *)identifier
{
    NSLog(@"Got an image: %@", image);
}




@end

【问题讨论】:

    标签: cocoa-touch twitter


    【解决方案1】:

    您有一些泄漏(twitterEngine(当 alloc/init 工作正常时您保留它)和 arrayEmotes(永远不会被释放)来命名我立即发现的两个),您可能还有另一个与 sImageName 相关的问题当您发送更新时(注意它是如何在 switch 语句之外的范围内定义的 - 我真的不知道这是否肯定会导致问题,这只是我一直试图避免的事情。我可能会看到它导致 EXC_BAD_ACCESS)。

    无论如何,请尝试使用 GDB 逐步执行您的程序,以确定哪些对象正在被提前释放。很多令人头疼的问题,只要用 GDB 就可以解决。

    【讨论】:

      【解决方案2】:

      每当您遇到 EXC_BAD_ACCESS 错误时,它始终是保留/释放问题。你的代码也至少有一个漏洞(看看你在哪里创建了 twitterengine)。

      我建议你阅读Cocoa Memory Management Rules。他们会帮助您找到问题。

      【讨论】:

        【解决方案3】:

        我也有这个问题,很奇怪;即使我正确地清理了我的 MGTwitterEngine 对象,在我释放对象后它的回调之一仍然会被调用。当回调以某种方式从死机中调用时,这会导致某种访问冲突。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-01-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-08
          • 2023-03-16
          • 1970-01-01
          相关资源
          最近更新 更多