iOS development message sent to deallocated instance problem solving

Keywords: encoding xcode

problem

Look directly at the screenshots.

Solution

1. Active Monitor Views APP Process Number PID

2. Viewing problems in terminal

Use the command malloc_history PID address |grep address
PID is replaced by process number, address is replaced by error address (xcode error address)

3. Look directly at the last line to locate the error position

Stick up the code.

    self.theWebView.frame = CGRectMake(0, 50, WIDTH1, [UIScreen mainScreen].bounds.size.height - 100);
    NSString * htmlStr = nil;  
    NSData *data=[NSData dataWithContentsOfFile:self.page.pageUrl options:0 error:NULL];  
//    NSString *result = [[[NSString alloc] initWithData:[data base64EncodedDataWithOptions:0]  encoding:NSUTF8StringEncoding] autorelease];  
    NSString *result=[GTMBase64 stringByEncodingData:data];
    htmlStr = [AESCrypt decrypt:result password:@"0123456789ABCDEF"];
//    if (self.readingBook.bookId == 0)
//    {
//        htmlStr = [[IYCCommon decodeBase64forFile:self.page.pageUrl] retain];
//    }
//    else
//    {
//        IYCSecurityManager *manager = [[IYCSecurityManager alloc] init];
//        htmlStr = [[manager decryptWithFilePath:self.page.pageUrl] retain];
//        [manager release];
//    }
    self.theWebView.dataDetectorTypes = UIDataDetectorTypeNone;
    [self.theWebView loadHTMLString:htmlStr baseURL:[NSURL fileURLWithPath:self.page.baseURL isDirectory:YES]];
    [htmlStr release];
//    [data release];
//    [result release];
    [self.view addSubview:self.theWebView];

Basically, you can see that the problem is caused by htnlStr release twice.

Note: I always thought that GTMBase64 had a problem and spent too much time on GTMBase64.

Note 2: By the way, AES encryption uses AESCrypt. In the decryption part, attention should be paid to the decoding and encoding method.

+ (NSString *)decrypt:(NSString *)base64EncodedString password:(NSString *)password
{
    NSData *encryptedData = [NSData base64DataFromString:base64EncodedString];
    NSData *decryptedData = [encryptedData AES128DecryptedDataWithKey:password];
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    return [[NSString alloc] initWithData:decryptedData encoding:enc];
}

Posted by abhikerl on Sun, 10 Feb 2019 22:42:17 -0800