标签: automatic-ref-counting

iOS - arc下的initWith方法

在过去,我们接受过训练,可以编写如下的init方法:

foo.h中:

-(id) initWithInteger: (NSInteger) someNumber;
Run Code Online (Sandbox Code Playgroud)

Foo.m:

-(id) initWithInteger: (NSInteger) someNumber {
    if ((self = [super init])) { 
       doSomeStuff;
    }
    return self;
 }
Run Code Online (Sandbox Code Playgroud)

但那些id演员现在已经不存在了.那么这些方法现在应该怎么样?

编辑:这是一个图书馆.因此无法确定调用代码的外观.

iphone objective-c ios automatic-ref-counting

5
推荐指数
2
解决办法
4316
查看次数

带有ARC的iOS5上的modalviewcontroller中的AVCaptureSession

我疯狂地想要在我的项目中呈现和解散AVCaptureSession(在视图控制器中).我目前在iOS5.1上并启用了ARC.

我第一次出现viewcontroller并开始会话时可以正常工作但是当我解雇并再次出现时会话将无法启动.我订阅了"AVCaptureSessionRuntimeErrorNotification"通知并收到以下错误:

"错误域= AVFoundationErrorDomain代码= -11819"无法完成操作"UserInfo = 0x1a4020 {NSLocalizedRecoverySuggestion =稍后再试.,NSLocalizedDescription =无法完成操作}"

我假设在我的会话中没有正确发布某些内容,但是ARC没有发布,而是我将所有内容都设置为nil.

我的viewDidLoad方法基本上只是触发initCamera

initCamera方法:

AVCaptureSession *tmpSession = [[AVCaptureSession alloc] init];

session = tmpSession;
session.sessionPreset = AVCaptureSessionPresetMedium;
captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
[self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];
rearCamera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

NSError *error = nil;
input = [AVCaptureDeviceInput deviceInputWithDevice:rearCamera error:&error];

if (!input) {
    // Handle the error appropriately.
    NSLog(@"ERROR: trying to open camera: %@", error);
}

[session addInput:input];

videoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt:kCVPixelFormatType_32BGRA], kCVPixelBufferPixelFormatTypeKey, …
Run Code Online (Sandbox Code Playgroud)

modalviewcontroller ios avcapturesession automatic-ref-counting

5
推荐指数
1
解决办法
2455
查看次数

使用ARC绘制渐变时,CFArrayRef和NSArray出现问题

我有一个ARC项目,我正在尝试绘制垂直线性渐变.下面的代码适用于模拟器,但EXC_BAD_ACCESS在设备上进行测试时会抛出内存/ 错误.该应用程序使用以下两行代码崩溃:

NSArray *colorArray = [NSArray arrayWithObjects:(__bridge id)topColor, (__bridge id)bottomColor, nil];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colorArray, colorLocations); 
Run Code Online (Sandbox Code Playgroud)

这两行代码取自以下代码(供参考):

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    [self createGradientForContext:context andView:self.captionView];
    [self createGradientForContext:context andView:self.linksView];
    [self createGradientForContext:context andView:self.commentView]; 

}

- (void)createGradientForContext:(CGContextRef)context andView:(UIView *)view
{

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGFloat colorLocations[] = { 0.0f, 1.0f };

    CGColorRef topColor = [[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] CGColor];
    CGColorRef bottomColor = [[UIColor colorWithRed:48.0f/255.0f green:48.0f/255.0f blue:48.0f/255.0f alpha:1.0f] CGColor];
    NSArray *colorArray = [NSArray arrayWithObjects:(__bridge id)topColor, (__bridge …
Run Code Online (Sandbox Code Playgroud)

core-graphics objective-c core-foundation ios automatic-ref-counting

5
推荐指数
2
解决办法
2976
查看次数

在执行期间将块设置为nil时会发生什么?

假设我有一个对块有强引用的对象.在执行该块的某个时间,强引用设置为nil.块是否保证完成执行,或者这会导致崩溃?我已经看到了exc-bad-access错误,但是我无法可靠地生成它们,所以我不知道它们为什么会弹出.

例如:

-(void)method
{
    self.block = ^{
        //code
        self.block = nil;
        //more code - crash here?
    }
}

-(void)otherMethod
{
    block();
}
Run Code Online (Sandbox Code Playgroud)

objective-c objective-c-blocks automatic-ref-counting

5
推荐指数
1
解决办法
2314
查看次数

MKMapView initWithFrame不使用ARC释放内存

我一直在使用工具来查找未正确发布的分配.我有MKMapViewDelegate,它在实例化后通过实例方法添加地图.我可以在调用树中看到,在释放ViewDelegate之后,此方法保留了大约300KB的内存.我评论了代码的内容,它仍然只用这一行维护内存:

self.map = [[MKMapView alloc] initWithFrame:CGRectMake(10, 210, 300, 125)];
Run Code Online (Sandbox Code Playgroud)

我查看对象列表并且MKMapView本身不存在,但随着我不断创建新的ViewDelegates,该内存不断增加.以下是地图的定义方式:

@property (strong, nonatomic)        MKMapView *map;
Run Code Online (Sandbox Code Playgroud)

地图的委托设置为nil,以及ViewDelegate的dealloc上的引用

self.map.delegate = nil;
self.map = nil;
Run Code Online (Sandbox Code Playgroud)

iphone memory-leaks mkmapview ios automatic-ref-counting

5
推荐指数
1
解决办法
1812
查看次数

MKMapView内存使用量稳步增长

在我的应用程序中,我注意到当我按下并弹出一个包含MKMapView内存的视图控制器时,内存使用量会稳步增加.我已经确定MapKit本身存在问题,而不是我的代码.我做了一个测试项目,其中只包含一个导航控制器,我可以反复推送和弹出一个只包含地图视图的视图控制器 - 没有注释或叠加.当我一遍又一遍地推动和弹出视图控制器时,每个推/弹循环的内存使用量增加约0.6 MB.有关为什么会发生这种情况的任何想法?我正在使用ARC.谢谢!

编辑:

我在iOS 5.1模拟器中做了一些测试(我知道在模拟器中有关于性能分析的问题,但我没有任何可用于测试的iOS 5设备),我的结果表明这是一个错误iOS 6; 在iOS 6模拟器中,我看到内存使用量逐渐增加,但在iOS 5模拟器中,内存使用率没有上升.我认为这是Apple新的内部地图的问题.

memory mapkit mkmapview automatic-ref-counting

5
推荐指数
0
解决办法
2753
查看次数

一旦最后一个强引用消失,ARC是否保证一个对象立即被释放?

特别是,这种代码总是按预期工作(其中MyResourceGuard是一个对象,它在其init方法中锁定独占资源并释放其dealloc方法中的锁):

NSLog(@"About to capture some exclusive resource.");
{
  MyResourceGuard* guard = [MyResourceGuard new];
  // Do something with the exclusive resource here.
}
// guard is out of scope, therefore its dealloc should have
// been called right away and the resource should already
// be free again at this point.
Run Code Online (Sandbox Code Playgroud)

我已阅读书籍和博客,与例如Java垃圾收集相反,ARC一旦引用计数减少到零就会销毁对象(而不是在它自己方便的某个时间),但我没有在任何官方读过这个Apple提供的文档.如果这是真的,为什么我们需要在ARC中引入新的@autoreleasepool关键字?

从调试开始,我总是看到对象dealloc'ed立即,除非在try-catch-block中引发异常,在这种情况下dealloc实际上从未被调用(是Mac bug,或者只是这些可怕的目标之一) C古怪?).

macos objective-c ios xcode4 automatic-ref-counting

5
推荐指数
1
解决办法
1157
查看次数

ARC非常奇怪的内存泄漏

下午好,

我正在我的iOS应用程序上进行一些仪器测试,我即将发布,但我注意到一些非常奇怪的内存泄漏,似乎来自系统库.

当我运行仪器时,我得到了这个泄漏: 在此输入图像描述

从我的角度来看,这是非常奇怪的!我去了这些泄漏的调用树,它们似乎都是深层次的C++系统调用.

在此输入图像描述

令我更加困惑的是,这个项目启用了ARC,我通过分析器运行它并没有出现任何问题.更奇怪的是,这不会发生在模拟器上,也不会发生在我真正的iPad上,但在我的iPhone上会出现这个问题.

当我检查"仅显示Obj-C"和"隐藏系统库"时,调用跟踪消失,这使我相信我的代码不负责泄漏.

为什么会发生这种情况,我该怎么做才能解决这个问题?

多谢你们.

memory-leaks objective-c ios automatic-ref-counting

5
推荐指数
1
解决办法
355
查看次数

理解弱参考

我有以下启用ARC的代码

@property (nonatomic, weak) NSArray *a;
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.a = @[@1, @2];
    NSLog(@"ab is %@", self.a); //prints details of array
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{

    [super didReceiveMemoryWarning];
    for (id element in self.a) { //empty here
        NSLog(@"blah");
    }
    // Dispose of any resources that can be recreated.
}
Run Code Online (Sandbox Code Playgroud)

这是我用过的唯一的地方self.a.这是我编写的一个测试程序,用于调试我的一个问题.

当我模拟内存警告self.a消失?为什么?

objective-c automatic-ref-counting

5
推荐指数
1
解决办法
1万
查看次数

避免保留周期,NSNotificationCenter?

这种类别方法是否允许我避免NSNotificationCenter观察员的额外保留?

#import "NSNotificationCenter+Util.h"

@implementation NSNotificationCenter (Util)

- (void)addWeakObserver:(nonnull NSObject*)observer selector:(nonnull SEL)aSelector name:(nullable NSString *)aName object:(nullable id)anObject {
    __weak NSObject *weakObserver = observer;
    [self addObserver:weakObserver selector:aSelector name:aName object:anObject];
}

@end
Run Code Online (Sandbox Code Playgroud)

目标是让对象死掉而不必将其移除.


注意:我试图避免这导致保留周期:

一定要调用removeObserver:name:object:之前 notificationObserver或在指定的任何对象 addObserver:selector:name:object:被释放.

objective-c nsnotificationcenter automatic-ref-counting

5
推荐指数
2
解决办法
2589
查看次数