标签: automatic-ref-counting

如何使用+1保留计数(ARC)修复Core Foundation对象?

这是我的方法:

CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(aSampleBuffer);

CIImage *ciImage = [CIImage imageWithCVPixelBuffer:imageBuffer];

CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef myImage = [context
                      createCGImage:ciImage
                      fromRect:CGRectMake(0, 0,
                                          CVPixelBufferGetWidth(imageBuffer),
                                          CVPixelBufferGetHeight(imageBuffer))];

return [UIImage imageWithCGImage:myImage];
Run Code Online (Sandbox Code Playgroud)

但是最后一行显示的是Potential leak of an object stored into 'myimage',myImage的行是Method returns a Core Foundation object with a +1 retain count.但我的应用程序是启用ARC的,所以我不能release做什么.我该如何解决?谢谢.

objective-c ios automatic-ref-counting

0
推荐指数
2
解决办法
1282
查看次数

在启用ARC的情况下通过ccmenuitem传递userData

我正在尝试使用以下代码通过menuitem传递NSString

CCMenuItem * buyButton = [CCMenuItemLabel itemWithLabel:buyLabel target:self selector:@selector(buyItem:)];
buyButton.userData = (__bridge void *)((NSString*)(itemName));
Run Code Online (Sandbox Code Playgroud)

到以下选择器

-(void) buyItem:(CCMenuItemLabel*)sender {
   NSString * itemName = (NSString *)sender.userData;
     }
Run Code Online (Sandbox Code Playgroud)

但我在选择器中崩溃了.我正在使用启用了arc的cocos2d,因此是userdata分配中的桥接器.(kobold2d).有任何想法吗?

cocos2d-iphone automatic-ref-counting kobold2d

0
推荐指数
1
解决办法
190
查看次数

[CustomViewController respondsToSelector:]:发送到解除分配的实例的消息

这用于我的预ARC代码工作正常,但由于重构所有项目是ARC兼容,我开始得到这个崩溃:

[CustomViewController respondsToSelector:]: message sent to deallocated instance
Run Code Online (Sandbox Code Playgroud)

我的项目是一个具有拆分视图的iPad应用程序,但与苹果文档相反,之前的开发人员已经在拆分视图之前在应用程序启动时显示另一个视图控制器.所以我知道这不是正确的方法,但正如我所说它在ARC集成之前曾经工作过,所以我需要解决这个问题.

根视图控制器包含项目菜单,每个项目显示要填充的详细信息表单,然后单击下一个按钮移动到下一个详细信息屏幕,等等.

当我点击根视图上的主页按钮返回主视图时,问题就开始了,这是将用户移动到主屏幕的相关代码:

//this method is in the appdelegate, and it gets called when clikc on home button located on the root view
- (void) showHome
{
    homeController.delegate = self;

    self.window.rootViewController = homeController;
    [self.window makeKeyAndVisible];
}
Run Code Online (Sandbox Code Playgroud)

然后,当我单击按钮返回拆分视图(根/详细信息视图的位置)时,应用程序将崩溃并显示上述说明.我用应用程序分析了应用程序,并且负责的代码行位于RootViewControllerdidSelectRowAtIndexPath方法中,这里是相关的代码:

    if(indexPath.row == MenuCustomViewController){
            self.customVC=[[CustomViewController alloc] initWithNibName:@"CustomVC"
                                                                      bundle:nil];
            [viewControllerArray addObject:self.customVC];
            self.appDelegate.splitViewController.delegate = self.customVC;
}
Run Code Online (Sandbox Code Playgroud)

customVC是一个强大的属性,我试图直接分配并分配给实例变量,但这没有帮助修复崩溃.有什么想法吗 ?

编辑: 这是由仪器给出的堆栈跟踪:

[self.appDelegate.splitViewController setViewControllers:viewControllerArray];//this line caused the crash

[viewControllerArray addObject:self.appDescVC];//this statement is called before the …
Run Code Online (Sandbox Code Playgroud)

memory-management uisplitviewcontroller ios automatic-ref-counting

0
推荐指数
1
解决办法
5943
查看次数

使用ARC时声明委托

在fastpdfkit中有这样的委托声明

@interface BookmarkViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

//Delegate to get the current page and tell to show a certain page. It can also be used to
//  get a list of bookmarks for the current document. 

NSObject<BookmarkViewControllerDelegate> *delegate; 
 }

@property (nonatomic, assign) NSObject<BookmarkViewControllerDelegate> *delegate;

@synthesize delegate;
Run Code Online (Sandbox Code Playgroud)

因为我使用ARC所以委托的声明是这样的

@interface BookmarkViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

 id __unsafe_unretained <BookmarkViewControllerDelegate> delegate;
 }

@property (unsafe_unretained) id <BookmarkViewControllerDelegate> delegate;

 @synthesize delegate;
Run Code Online (Sandbox Code Playgroud)

这是正确的原因当即时调试我得到

 currentPage    NSUInteger  0
delegate    objc_object *   0x00000000
Run Code Online (Sandbox Code Playgroud)

iphone delegates automatic-ref-counting ios6

0
推荐指数
1
解决办法
516
查看次数

Objective-C,ARC:使用__weak参数是否正确?

这是使用弱参数的一个小例子:

@interface MYTestObject : NSObject

@end

@implementation MYTestObject {
   void(^_block)(void);
}

- (void)dealloc {
   NSLog(@"DEALLOC!");
}

- (id)init {
   if (self = [super init]) {
      [self doSomethingWithObject:self];
   }
   return self;
}

- (void)doSomethingWithObject:(id __weak /* <- weak argument! */)obj {
   _block = ^{
      NSLog(@"%p", obj);
   };
}

@end
Run Code Online (Sandbox Code Playgroud)

它有效:-dealloc被称为!此外,如果你删除__weak你将获得一个保留周期,这是绝对正确的.

不知道,如果这只是一个副作用,使用弱论点是完全不安全的吗?或者它是一个特定的行为,我只是一个糟糕的谷歌用户?

arguments weak-references objective-c automatic-ref-counting

0
推荐指数
1
解决办法
4417
查看次数

如何在ARC下使用`-retainCount`方法和`-dealloc`选择器强制执行?

ARC,编译器将禁止使用任何方法或选择器-retainCount,-retain,-dealloc,-release,和-autorelease.

但有时我想知道运行时的保留计数,或者使用方法调配来交换-deallocNSObject 的方法来做某事.

是否有可能抑制(或绕过)编译器抱怨几行代码?我不想为整个项目或整个文件修改ARC环境.我认为预处理器可以做到,但是怎么做?


加法:

谢谢你们给我一个关于使用的教​​训-retainCount.但我想知道是否可以强制调用/使用那些禁止的方法/选择器.

我知道这Instruments是完成这项工作的有力工具.但我对这些问题仍然很好奇.

我为什么要使用-retainCount:

使用块时,如果未__weak在外部变量上指定标识符,则在将块复制到堆中后,块将自动保留块中的外部对象.因此,您需要使用弱自我来避免保留周期,例如:

__weak typeof(self) weakSelf = self;
self.completionBlock = ^{
    [weakSelf doSomething];
};
Run Code Online (Sandbox Code Playgroud)

但是,当您仅在复制的块中使用实例变量时,它仍将导致保留周期(YES,尽管您未self在块中使用任何关键字).

例如,在非ARC下:

// Current self's retain count is 1
NSLog(@"self retainCount: %d", [self retainCount]);

// Create a completion block
CompletionBlock completionBlock = ^{
    // Using instance vaiable in …
Run Code Online (Sandbox Code Playgroud)

objective-c automatic-ref-counting

0
推荐指数
2
解决办法
1486
查看次数

方法可能缺少[super dealloc]调用

我有一个正在构建并抛出此警告的库,因为ARC已关闭.但是项目本身是启用ARC的.忽视这个警告有什么意义?

- (void)dealloc {
   if (_framesetter) CFRelease(_framesetter);
   if (_highlightFramesetter) CFRelease(_highlightFramesetter);
}
Run Code Online (Sandbox Code Playgroud)

objective-c ios automatic-ref-counting xcode5

0
推荐指数
2
解决办法
3201
查看次数

iOS ARC ram只能成长

您好我的问题可能是一般我不是要求代码等我只开发iOS6.1及以上版本的iPhone

当我运行我的应用程序时,它使用的RAM只会增长(当我在视图之间切换时(我有15个视图)).

然而,在我用分析仪进行测试后,它没有发现任何泄漏.仪器泄漏也没有发现泄漏.

尽管我的应用程序不超过20 MB的RAM,但我仍然担心某些事情可能不太好.

我正在使用ARC,但是ram仍在上升.

有什么方法可以检查什么会导致单侧ram分配?

iphone ram ios automatic-ref-counting

0
推荐指数
1
解决办法
410
查看次数

Dealloc和ARC

我读到在Xcode中使用ARC进行Objective-C编程时,dealloc编译器会自动调用该方法.在什么情况下它被称为?

为了避免使用太多的变量名,当我需要重复使用相同的类来执行多个操作(并且每次都重置变量)时,我经常声明变量,将它们设置为nil,然后在我去的时候为它们赋值.最终看起来像这样:

MyClass mc;

mc = [[MyClass alloc] init];
[mc doThis:someOption]

mc = [[MyClass alloc] init];
[mc doThis:someOtherOption];

//etc...
Run Code Online (Sandbox Code Playgroud)

方法名称alloc是"allocate"的缩写,因为它是将内存分配给变量的方法.sc每次为其分配新值时,编译器是否会自动释放内存?我计划在我的一个项目中使用这个方法,并且我不希望在我调用的所有时间分配大量内存alloc来分配新值mc.

memory-management objective-c automatic-ref-counting

0
推荐指数
1
解决办法
457
查看次数

无法访问NSTimer的userInfo

我有这个代码。

- (void)scheduleTimerAfterDelay:(NSTimeInterval)delay {
    dispatch_async(dispatch_get_main_queue(), ^{
        _timer = [NSTimer scheduledTimerWithTimeInterval:delay
                                                  target:self
                                                selector:@selector(triggerTimer:)
                                                userInfo:[NSString stringWithFormat:@"%f", delay]
                                                 repeats:NO];
    });
}

- (void)triggerTimer:(NSTimer *)timer {
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"Triggered timer after %@ s.", _timer.userInfo); // <-- Exception thrown!
        // Do stuff
    });
}
Run Code Online (Sandbox Code Playgroud)

但是,当计时器触发时,_timer.userInfo会导致Exception: EXC_BAD_ACCESS (code=1, address=0xc))

我在这里错过了什么?在异常行的断点处打印_timer表示_timer为<__NSCFTimer: 0x14ec8cb0>。但是我也无法通过lldb访问userInfo。我正在使用ARC。

objective-c nstimer automatic-ref-counting ios7

0
推荐指数
1
解决办法
338
查看次数