标签: automatic-ref-counting

为什么在比较ARC下的桥接CGColorRefs时遇到崩溃?

我有一个用于在我的应用程序中使用的阴影表视图的类.

转换到ARC时,我不得不改变一些事情.对于新类,它崩溃在以下几行:(id)(inverse ? darkColor : lightColor)to (__bridge id)(inverse ? lightColor : darkColor).控制台说*** -[Not A Type retain]: message sent to deallocated instance 0x4cee70

之前:

- (CAGradientLayer *)shadowAsInverse:(BOOL)inverse
{
    CAGradientLayer *newShadow = [[[CAGradientLayer alloc] init] autorelease];
    CGRect newShadowFrame =
        CGRectMake(0, 0, self.frame.size.width,
            inverse ? SHADOW_INVERSE_HEIGHT : SHADOW_HEIGHT);
    newShadow.frame = newShadowFrame;
    CGColorRef darkColor =
        [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:
            inverse ? (SHADOW_INVERSE_HEIGHT / SHADOW_HEIGHT) * 0.25 : 0.25].CGColor;
    CGColorRef lightColor =
        [self.backgroundColor colorWithAlphaComponent:0.0].CGColor;
    newShadow.colors =
        [NSArray arrayWithObjects:
            (id)(inverse ? …
Run Code Online (Sandbox Code Playgroud)

iphone memory-management objective-c ios automatic-ref-counting

4
推荐指数
1
解决办法
1663
查看次数

OS X应用程序的自动释放池在哪里创建并耗尽?

我正在为ARC重构我的OS X应用程序.打开main.m文件,我确信我会找到Autorelease Pool实例化和排空(比如iOS项目),但令我惊讶的是它不在那里.

所以我的第一个问题是:

  • 主要的Autorelease Pool在哪里?

我的下一个问题是:

  • 如果未创建主Autorelease Pool,是否需要创建它?或者以某种自动方式释放自动释放的对象?

macos cocoa autorelease nsautoreleasepool automatic-ref-counting

4
推荐指数
1
解决办法
942
查看次数

如果我将指针设置为nil,自动引用计数是否释放对象?

如果我将指针设置为nil或将指针指定给另一个对象,自动引用计数是否释放对象?

例如做类似的事情:

//in .h file

@interface CustomView : UIView
{
   UIView *currentView;
}


// in .m file:

-(void)createView1
{
   currentView = [[UIView alloc] init];
   [self addSubview:currentView];
}

-(void)createView2
{
   [currentView removeFromSuperview];

   // does the former view get released by arc
   // or does this leak?
   currentView = [[UIView alloc] init];
   [self addSubview:currentView];
}
Run Code Online (Sandbox Code Playgroud)

如果此代码泄漏,我将如何正确声明*currentView?或者我如何让ARC"释放"当前视图?谢谢!

cocoa-touch ios automatic-ref-counting

4
推荐指数
1
解决办法
4368
查看次数

如何在ARC中释放内存以获得高内存使用率的图形渲染?

首先,感谢本网站上的所有人...这对于深入了解iOS编程非常有帮助.

我目前的问题:

我有一个应用程序,呈现一个非常风格化的照片版本.它为其中一些使用了一些CoreImage过滤器,但需要一堆CoreGraphics来完成繁重的图像处理.

代理大小渲染效果很好,但是当我渲染我的图像的完整分辨率版本时,它有时因内存使用率过高而崩溃.问题是我需要能够在渲染时在内存中有几个全分辨率(3264x2448)缓冲区.我不知道什么或如何释放更多的记忆.我一直非常小心地匹配CGImageRelease到处都可以.

使用ARC,我如何知道某些内容是否真正被释放并释放?将对象设置为nil实际上并没有做任何事情.

我怀疑我可以以任何方式将其传输到磁盘.

任何建议都将非常感谢!

谢谢!

memory core-graphics core-image ios automatic-ref-counting

4
推荐指数
1
解决办法
9872
查看次数

使用UIImagePickerController减少内存使用量

在我的应用程序中,用户可以使用UIImagePickerController拍摄多个图像,然后在视图中逐个显示这些图像.

我在内存管理方面遇到了一些麻烦.随着今天手机上的摄像头以百万像素迅速上升,从UIImagePickerController返回的UIImages是记忆猪.在我的iPhone 4S上,UIImages约为5MB; 我很难想象它们在新的和未来的模型上会是什么样的.

我的一位朋友说,处理UIImages的最佳方法是立即将它们保存到我应用程序文档目录中的JPEG文件中,并尽快释放原始的UIImage.所以这就是我一直在努力做的事情.不幸的是,即使将UIImage保存为JPEG并且在我的代码中没有引用它,它也不会被垃圾收集.

以下是我的代码的相关部分.我正在使用ARC.

// Entry point: UIImagePickerController delegate method
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    // Process the image.  The method returns a pathname.
    NSString* path = [self processImage:[info objectForKey:UIImagePickerControllerOriginalImage]];

    // Add the image to the view
    [self addImage:path];
}

-(NSString*) processImage:(UIImage*)image {

    // Get a file path
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];
    NSString* filename = [self makeImageFilename]; // implementation omitted
    NSString* imagePath = [documentsDirectory stringByAppendingPathComponent:filename];

    // Get the image data …
Run Code Online (Sandbox Code Playgroud)

memory-management uiimagepickercontroller uiimage ios automatic-ref-counting

4
推荐指数
1
解决办法
3485
查看次数

使用Instruments跟踪内存泄漏Xcode5和iOS7

我开发了我的第一个应用程序,仍在学习 刚刚更新了Xcode 5,我的iPhone 5正在运行iOS 7.1.我现在正致力于理解"仪器"工具来分析我的应用程序.我正在使用仪器中的泄漏配置文件,并且遇到了一个我无法弄清楚的保留周期和泄漏.感谢我能理解的任何帮助a)如何查明错误和b)我做错了什么以及如何在将来避免它.

在我的应用程序中,我正在使用相机拍摄照片.当相机启动时,我轻触屏幕进行对焦,这时我注意到仪器中出现了泄漏.看起来我的代码中没有保留周期(如果我理解正确)和我的代码中的根漏洞,我不明白.

我正在使用ARC,所以我不应该在我指向我的代码部分中发布"picker"(下面的第2部分,带代码).

CALayer保留周期

  1. 我该如何处理保留周期.它不在我的代码中,但我仍然是它的原因吗?它是否源于"根漏"?如果我不负责,我该怎么办呢?

根泄漏

  1. 我在堆栈跟踪中的唯一代码无法帮助我理解泄漏的来源.该行是仪器指向我的代码是[picker dismissViewControllerAnimated:YES completion:nil]; 在我的imagePickerController中:didFinishPickingMediaWithInfo:方法.我正在粘贴下面的代码.
 - (void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *pickerImage = [info objectForKey:
                            UIImagePickerControllerOriginalImage];

    _imageView.image = pickerImage;

    [picker dismissViewControllerAnimated:YES completion:nil]; // <--- mem-leak
}
Run Code Online (Sandbox Code Playgroud)

编辑 - 我创建我的UIImagePickerController的代码.

- (IBAction)snapPicture:(id)sender
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

    // If the camera button was tapped AND we have a camera, use it...
    if ((sender == _cameraButton) && [UIImagePickerController
         isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    } else {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    }

    imagePicker.delegate = …
Run Code Online (Sandbox Code Playgroud)

camera memory-leaks calayer ios automatic-ref-counting

4
推荐指数
1
解决办法
964
查看次数

我应该从Swift的NSObject继承吗?

我在这里找到了很好的讨论,但还有一个问题:

我知道通过NSObject的Objective-C实现引用计数器。如果NSObject不是基类,是否仍支持ARC?

ios automatic-ref-counting swift

4
推荐指数
1
解决办法
1130
查看次数

在可能的块/闭包保留周期中获取swift_unknownWeakRelease()

我有一个视图控制器,显示在我的应用程序的开头,以准备我正在使用Core Data的UIManagedDocument.问题:swift_unknownWeakRelease()当我在iOS 4.1的iPhone 4S设备上运行应用程序时,我一直在崩溃.这是代码:

class SetupViewController: UIViewController {

    @IBOutlet weak var loadingView: UIActivityIndicatorView!

    override func viewDidAppear(animated: Bool)  {
        super.viewDidAppear(animated)
        let document = SPRManagedDocument.sharedDocument()

        document.prepareWithCompletionHandler {[unowned self] success in
            if success {
                self.dismissViewControllerAnimated(false, completion: nil)
            } else {
                self.loadingView.stopAnimating()
                UIAlertView(title: "Error", message: "Categories can't be loaded.", delegate: nil, cancelButtonTitle: "OK").show()
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

我怀疑这个区块之间有一个强大的参考周期,self因为这是我唯一可以看到的可能发生的地方.确实如此,如果我将捕获列表更改[unowned self]为[weak self]或完全删除它(保持self对闭包内部的强引用),程序继续正常.当我在带有iOS 8的iPhone 5模拟器或带有iOS 7.1的5S模拟器上运行应用程序时,不会发生此错误.

如何以不同方式编写代码,以便在运行iOS 7.0+的所有设备上避免崩溃?我确信这unowned是正确的修饰符.我希望self在completionHandler完成之前仍然存在,所以weak感觉不对.如果有帮助,这是Debug …

iphone cocoa-touch ios automatic-ref-counting swift

4
推荐指数
1
解决办法
911
查看次数

原子引用计数和自动引用计数有什么区别?

我看到ARC是不时使用的首字母缩写,但是例如在Rust中,它是“原子引用计数”,而在Swift或ObjC中,它们称为“自动引用计数”。到底有什么区别?据我所知,它们的行为方式似乎相同。

rust automatic-ref-counting swift

4
推荐指数
1
解决办法
522
查看次数

如何解决Swift 4中的内存循环URLSession.downloadTask?

我有一个基本viewController的单个按钮,当点击时,调用一个方法开始从给定的有效网址下载图像.

我非常小心地将任何强大的指针传递给viewcontroller.而且我可以将视图控制器解散掉并且回到演示控制器而没有任何问题.但是,Web()即使deinit调用了viewcontroller,viewController创建的对象实例也不会被释放.

我究竟做错了什么?

BTW:启动文件下载,报告进度并报告文件位置.但是,一旦文件下载完毕,该对象永远不会被释放.

viewController.swift

@IBAction func buttonTapped(_ sender: UIButton) {
   //first create an instance of Web class (download helper)
   let web = Web(url: "https://www.google.com.sa/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png")

   // all call back closures have no reference to self    
   web.downloadFile(
       finishedHandler: {fileLocation in print("file stored to \(fileLocation)")},
       progressHandler: {bytes,total in print("written \(bytes) out of \(total)")},
       errorHandler: {msg in print("error: \(msg)")}
  )

}
Run Code Online (Sandbox Code Playgroud)

我已经定义Web()了NSObject哪些符合URLSessionDownloadDelegate利用委托事件方法(didFinishDownload/ didWriteBytes等).

Web.swift

import UIKit

class Web …
Run Code Online (Sandbox Code Playgroud)

ios automatic-ref-counting swift

4
推荐指数
1
解决办法
421
查看次数