标签: automatic-ref-counting

iOS:如何在启用ARC的情况下从内存中删除对象?

我正在使用iOS 5 SDK开发iOS应用程序,启用了自动引用计数.但我有一个特定的对象正在大量创建,必须在一秒钟之后释放,否则设备将变得非常慢.看起来它们没有被释放,因为设备非常慢.有没有办法在启用ARC时手动释放对象?

编辑:我的代码,这被称为每秒200次生成火花.它们在0.8秒后淡出,因此在那之后它们没用了.

    int xanimationdiff = arc4random() % 30;
    int yanimationdiff = arc4random() % 30;
    if (arc4random()%2 == 0) {
        xanimationdiff = xanimationdiff * -1;
    }
    if (arc4random()%2 == 0) {
        yanimationdiff = yanimationdiff * -1;
    }

    Sparkle *newSparkle = [[Sparkle alloc] initWithFrame:CGRectMake(20 + arc4random() % 280, 20, 10, 10)];
    //[newSparkle setTransform:CGAffineTransformMakeRotation(arc4random() * (M_PI * 360 / 180))]; //Rotatie instellen (was niet mooi, net sneeuw)
    [self.view addSubview:newSparkle];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.8];
    [newSparkle setFrame:CGRectMake(newSparkle.frame.origin.x - xanimationdiff, newSparkle.frame.origin.y - yanimationdiff, …
Run Code Online (Sandbox Code Playgroud)

ios automatic-ref-counting

10
推荐指数
3
解决办法
3万
查看次数

EXC_BAD_ACCESS消息发送到解除分配的实例,但我正在使用ARC?

我有一个应用程序从SOAP Web服务获取信息,我想在一个显示结果UITableView.

我有一个以前版本的这个应用程序,我正在创建一个新版本,基本上清理东西,摆脱一堆已弃用,不再使用的遗留代码.

在之前的版本中,这很有效.在新版本中,没有那么多.

基本上,当前场景返回3个字符串,我试图用作我的数据的基础UITableView.

我正在努力解决这个问题,因为它太难以追查EXC_BAD_ACCESS错误了!

(顺便说一句,如果有人有办法使调试体验更像Visual Studio,我很乐意听到它!没有任何想法导致错误,并且无法通过我查看,这是非常令人沮丧的崩溃时的局部变量,看看是什么.我已经在异常断点中添加了,但这似乎并不多.)

无论如何,导致出错的行是:

return [[self Libraries] count];
Run Code Online (Sandbox Code Playgroud)

它发生在tableView:numberOfRowsInSection:.

错误消息我得到APPEARS引用应该存储在中的字符串NSMutableArray [self Libraries].

这里发生了什么?
我正在使用ARC,所以不应该正确处理所有内存管理吗?
ANYWHERE我的代码中没有任何手动发布声明!

请帮我解决这个问题!

memory exc-bad-access objective-c automatic-ref-counting

10
推荐指数
1
解决办法
5741
查看次数

Objective-C - ARC - NSNumber - 分段故障

我有一个Objective-C程序,我正在使用ARC(自动引用计数),它在第23行引发了一个分段错误(参见下面的程序).

问题 1)为什么会发生分段错误?

以下是该计划:

#import<Foundation/Foundation.h>

@interface Car : NSObject
@property (weak) NSNumber* doors;
@end

@implementation Car 
@synthesize doors;
@end

int main()
{
    system("clear");

    @autoreleasepool
    {    
        Car *car1 = [[Car alloc] init];

        printf("1\n");
        NSNumber *d1 = [[NSNumber alloc] initWithInteger: 4]; 

        printf("2\n");
        car1.doors = d1;   //Segmentation fault.. why ?

        printf("3\n");
    }   

    printf("---- end\n");

    return(0);
}
Run Code Online (Sandbox Code Playgroud)

输出:

1
2
Segmentation fault: 11
Run Code Online (Sandbox Code Playgroud)

weak-references objective-c segmentation-fault nsnumber automatic-ref-counting

10
推荐指数
1
解决办法
2210
查看次数

ARC和UIAlertView:发送到实例的无法识别的选择器

这是我如何显示UIAlertView和委托clickedButtonAtIndex -

UIAlertView *alert =
    [[UIAlertView alloc] initWithTitle: @"title"
                               message: @"message"
                              delegate: self
                     cancelButtonTitle: @"Cancel"
                     otherButtonTitles: @"Continue", nil];

    [alert show];


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    //something
}
Run Code Online (Sandbox Code Playgroud)

这段代码在没有ARC的情况下完美运行.但是使用ARC它会抛出此错误 - 由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSCFType alertView:clickedButtonAtIndex:]:无法识别的选择器发送到实例0x859d790'

关于代表为何抛出此错误的任何想法?

uialertview ios automatic-ref-counting

10
推荐指数
1
解决办法
4522
查看次数

如何让ARC下的OCMock停止使用弱属性来设置NSProxy子类?

在ARC,我有一个对象,Child有一个weak属性,parent.我正在尝试编写一些测试Child,并且我正在parent使用它来嘲弄它的属性OCMock.

在ARC下,NSProxy使用合成的弱属性设置器设置子类不会设置属性...设置弱属性后的行,检查它显示它已经存在nil.这是具体的例子:

@interface Child : NSObject
@property (nonatomic, weak) id <ParentInterface>parent;
@end

@implementation Child
@synthesize parent = parent_;
@end

//  ... later, inside a test class ...

- (void)testParentExists
{
    // `mockForProtocol` returns an `NSProxy` subclass
    //
    OCMockObject *aParent = [OCMockObject mockForProtocol:@protocol(ParentInterface)];
    assertThat(aParent, notNilValue());

    // `Child` is the class under test
    //
    Child *child = [[Child alloc] init];
    assertThat(child, notNilValue());

    assertThat(child.parent, nilValue()); …
Run Code Online (Sandbox Code Playgroud)

unit-testing objective-c ocmock nsproxy automatic-ref-counting

10
推荐指数
2
解决办法
1962
查看次数

将自释放对象转换为ARC

好的,Apple给我们带来了ARC,这很棒.在将我的应用程序重构为AR​​C之后,几乎所有工作都正常,现在开发和维护起来要容易得多.

只有一个问题我仍然无法弄清楚.

我的工作管理程序在自己的窗口中显示提案,订单等的不同详细信息.所以我有一个特殊的类,其中WindowControllers被分配并使用initWithWindowNibName启动,然后窗口显示为showWindow:

DetailWindowController *proposalWindowController = [[DetailWindowController alloc] initWithWindowNibName:@"ThePorposalWindow"];
[proposalWindowController showWindow:nil];
Run Code Online (Sandbox Code Playgroud)

在ARC之前,WindowController的实例执行了如文档中所示的发布:

- (void)windowWillClose:(NSNotification *)notification
{
   [self autorelease];
}
Run Code Online (Sandbox Code Playgroud)

但是现在使用ARC这是不可能的,更糟糕​​的是,在我分配和启动WindowController的特殊类中,ARC释放了相同的windowController,因为没有指向windowController的指针.

我的想法是将windowController复制到一个可变数组中:

[proposalWindowArray addObject:proposalWindowController];
[[proposalWindowArray lastObject] showWindow:nil];
Run Code Online (Sandbox Code Playgroud)

并在windowControllers委托方法windowWillClose中我向我的特殊类发布通知:

- (void)windowWillClose:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ProposalWindowWillClose" object:[[self window] windowController] userInfo:nil];
}
Run Code Online (Sandbox Code Playgroud)

在我的特殊类中,我听取通知并从数组中删除对象:

- (void) proposalWindowWasClosed: (NSNotification *) notification
{
    [proposalWindowArray removeObjectIdenticalTo:[notification object]];
}
Run Code Online (Sandbox Code Playgroud)

它有效,但我仍然不相信这是正确的方法.

有没有人有同样的问题或提示让它变得更好?

macos cocoa design-patterns memory-management automatic-ref-counting

10
推荐指数
1
解决办法
1655
查看次数

-fno-objc-arc无法禁用ARC

我已经尝试过这篇文章中的解决方案来禁用AFNetworking文件中的ARC,但无济于事:

在此输入图像描述

我失败的任何想法?显然答案越简单越好.我还读到创建静态库可能有所帮助,但这看起来很复杂.

EDITS

我已经尝试删除派生数据和清理和构建并重新启动Xcode.没有交易:(.

此外,我的工作区中没有其他项目.

xcode memory-management objective-c automatic-ref-counting afnetworking

10
推荐指数
2
解决办法
8648
查看次数

iOS:如何在ARC中销毁Singleton?我是不是该?

我有一个单例类,它会累积数据,直到数据写入我的数据库(如果你想知道为什么我用这种方式实现,请看这里).保存数据后,我想破坏单例.我怎么能在ARC中这样做?或者我是偏执狂,我是否需要销毁它?

*您可能会说这是此问题的副本,但此处接受的答案并不具体,无法提供帮助.它说"你可以声明你明确调用的方法/函数." 这个代码可能是什么样的?如果我无法在方法之外释放对象,我怎么可能在方法中将其拉出来?它还说" 最简单的方法是让一个静态C++类保存它,然后在它的析构函数中释放它." 我不懂C++,但是 - 你真的可以在你的应用程序代码中加入C++类吗?

我的单身实现如下:

+(NHCFamilyStatus *)familyStatus
{
  static dispatch_once_t pred;
  static NHCFamilyStatus *familyStatusSharedObject=nil;

    dispatch_once(&pred, ^
    {
        familyStatusSharedObject = [[NHCFamilyStatus alloc] init];
    });

  return familyStatusSharedObject;
}
Run Code Online (Sandbox Code Playgroud)

singleton dealloc ios automatic-ref-counting

10
推荐指数
1
解决办法
7701
查看次数

Objective-C:ARC禁止显式消息发送'retain'

我是Objective-C的新手,我尝试将旧版Objective-C中编写的旧Objective-C项目移植到新版本,但是我收到以下编译器错误:

ARC forbids explicit message send of 'retain'

in 
color = [aColor retain];
or 
color = [[NSColor blackColor] retain];
Run Code Online (Sandbox Code Playgroud)

我正在阅读clang正在使用的新自动引用计数.
我也试过使用Xcode的重构函数,但没有运气......需要替换这个旧代码的正确的Objective-C代码是什么?

objective-c retain automatic-ref-counting

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

弱IBOutleCollection总是零

我的问题是为什么弱IBOutletCollection总是为零?如果改变弱到强,我的所有按钮都在那里,这真的很奇怪.我试图理解苹果的逻辑,我可以看到单个按钮和一系列按钮在内存管理方面没有区别.我错过了什么吗?

memory-management interface-builder storyboard ios automatic-ref-counting

10
推荐指数
2
解决办法
1270
查看次数