-[NSConcreteMutableData release]:消息发送到已释放的实例

发布于 2024-10-17 15:21:24 字数 2481 浏览 1 评论 0原文

我面临 -[NSConcreteMutableData release] 的问题:消息发送到已释放的实例,我也附上了我的示例代码。

- (IBAction)uploadImage {

NSString *urlString = @"http://192.168.1.108/iphoneimages/uploadfile.php?userid=1&charid=23&msgid=3";


//if(FALSE)
for (int i=0; i<[imgArray count]; i++) {

    // setting up the request object now
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    /*
    add some header info now
    we always need a boundary when we post a file
    also we need to set the content type

    You might want to generate a random boundary.. this is just the same 
    as my output from wireshark on a valid html post
 */
    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    /*
     now lets create the body of the post
     */
    NSMutableData *body = [[NSMutableData data] autorelease];

    NSString *str = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile%d.jpg\"\r\n",i];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithString:str] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];


    NSData *imageData = UIImageJPEGRepresentation([imgArray objectAtIndex:i], 90);
    [body appendData:[NSData dataWithData:imageData]];


    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // now lets make the connection to the web
    [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    //NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    //NSLog(@"%@",returnString);
    [imageData release];

    [request release];
    //[body release];
}
}

它成功地将图像上传到文件夹,并且执行中没有任何错误,但是当它完成它的过程并尝试返回时,它给出错误 -[NSConcreteMutableData release]:消息发送到已释放的实例

请帮助我。

谢谢

I am facing a problem of -[NSConcreteMutableData release]: message sent to deallocated instance, i have attached my sample code as well.

- (IBAction)uploadImage {

NSString *urlString = @"http://192.168.1.108/iphoneimages/uploadfile.php?userid=1&charid=23&msgid=3";


//if(FALSE)
for (int i=0; i<[imgArray count]; i++) {

    // setting up the request object now
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    /*
    add some header info now
    we always need a boundary when we post a file
    also we need to set the content type

    You might want to generate a random boundary.. this is just the same 
    as my output from wireshark on a valid html post
 */
    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    /*
     now lets create the body of the post
     */
    NSMutableData *body = [[NSMutableData data] autorelease];

    NSString *str = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile%d.jpg\"\r\n",i];

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];    
    [body appendData:[[NSString stringWithString:str] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];


    NSData *imageData = UIImageJPEGRepresentation([imgArray objectAtIndex:i], 90);
    [body appendData:[NSData dataWithData:imageData]];


    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // now lets make the connection to the web
    [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    //NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    //NSLog(@"%@",returnString);
    [imageData release];

    [request release];
    //[body release];
}
}

It successfully upload the images to the folder and there is no any error in the execution but when it complete it process and try to go back it give error -[NSConcreteMutableData release]: message sent to deallocated instance

Please help me out.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

UIImageJPEGRepresentation 返回一个自动释放的对象,然后您释放它。当它自动释放时,它会爆炸。

UIImageJPEGRepresentation returns an auto released object and you release it. When this is auto released it will blow up.

玩心态 2024-10-24 15:21:24

NSMutableData *body = [[NSMutableData data] autorelease];

删除该自动释放消息

NSMutableData *body = [[NSMutableData data] autorelease];

remove that autorelease message

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文