标签: automatic-ref-counting

将 object_setInstanceVariable() 转换为 ARC?

我想转换一个实例

object_setInstanceVariable(self, [key UTF8String], *(id**)addr);
Run Code Online (Sandbox Code Playgroud)

到 ARC。当我尝试使用 Xcode 内置的 Objective-C ARC 转换器时,出现以下错误:

“object_setInstanceVariable”不可用:在自动引用计数模式下不可用

我应该如何处理这个问题?我知道我可以将 --fno-objc-arc 作为编译器标志,但如果可能的话,我非常想改用 ARC(我有很多目标,否则我必须手动更改编译器标志) .

(该代码最初来自Rackspace Mobile Apps 的 Mike Mayo 的NSObject+NSCoding 和 Archiver)

ios automatic-ref-counting

3
推荐指数
1
解决办法
1624
查看次数

如何最好地保留ARC代表的自我?

想象一下三个班级.ClassA和ClassB是我的.第三个类是UIViewController,其代码我无法修改:

在ClassA中:

- (void) aMethod
{
    ClassB *classBInstance = [[[ClassB alloc] init] goWithOptions:options];
}
Run Code Online (Sandbox Code Playgroud)

ClassBInstance只保留了一生aMethod.

在ClassB中:

- (void) goWithOptions
{
    AUIViewController *avcInstance = [[AUIViewController alloc] init];
    avcInstance.delegate = self;
    [viewController pushViewController:avcInstance animated: YES];    // this returns immediately, but we need self to be retained until the delegate is done with it
}

- (void) cleanupCalledByDelegate //
{
    // cleanup
}
Run Code Online (Sandbox Code Playgroud)

-goWithOptions调用方法时,avcInstance被调用者保留,但是self,传递给delegate不是.这意味着一旦-goWithOptions返回并aMethod完成,则classBInstance释放,并且delegatefor avcInstance不再有效.

理想情况下,我想将 …

delegates objective-c ios automatic-ref-counting

3
推荐指数
1
解决办法
493
查看次数

使用 C++ `function` 块(相对于 Objective C 块)时是否需要使用弱指针

如果您在 Objective-C 样式块中捕获对 ARC 下的强引用self,则需要使用__weak指针来避免 ARC“保留周期”问题。

// Right way:
- (void)configureBlock {
  XYZBlockKeeper * __weak weakSelf = self;
  self.block = ^{
    [weakSelf doSomething];   // capture the weak reference
                              // to avoid the reference cycle
  }
}
Run Code Online (Sandbox Code Playgroud)

我真的不知道保留周期是什么,但这个答案对此进行了一些描述。我只知道你应该__weak对 Objective-C 风格的块使用指针。请参阅捕获 self 时避免强引用循环。

self但我的问题是,在 C++ 块下捕获时是否需要创建弱指针<functional>?

- (void)configureBlock {
  self.block = [self](){
    [self doSomething]; // is this ok? It's not an objective C block.
  }
}
Run Code Online (Sandbox Code Playgroud)

block objective-c++ automatic-ref-counting

3
推荐指数
1
解决办法
1989
查看次数

__strong 限定符用于非 ARC 项目

该项目是非 ARC 启用的,但是我们(错误地)使用了符合 ARC 的代码库 - 特别是一个创建单例对象的代码库,如 GCDSingleton.h 中定义的那样:

#define DEFINE_SHARED_INSTANCE
+ (id)sharedInstance
{
  static dispatch_once_t pred = 0;
  __strong static id _sharedObject = nil;
  dispatch_once(&pred, ^{
    _sharedObject = ^{return [[self alloc] init];}();
  });
  return _sharedObject;
}
Run Code Online (Sandbox Code Playgroud)

即使共享对象是用 __strong 限定符定义的,这似乎也有效。我想知道为什么这不会导致错误或至少是警告(最新的 Xcode 4.6 和 ios 6 sdk)。此外,由于该项目未启用 ARC,那么 __strong 限定符究竟在做什么(如果有的话)?

objective-c ios automatic-ref-counting

3
推荐指数
1
解决办法
208
查看次数

如何使用 python、numpy 和 scipy 积分弧长?

在另一个线程上,我看到有人设法使用 mathematica 积分弧长。他们写道:

In[1]:= ArcTan[3.05*Tan[5Pi/18]/2.23]
Out[1]= 1.02051
In[2]:= x=3.05 Cos[t];
In[3]:= y=2.23 Sin[t];
In[4]:= NIntegrate[Sqrt[D[x,t]^2+D[y,t]^2],{t,0,1.02051}]
Out[4]= 2.53143
Run Code Online (Sandbox Code Playgroud)

究竟如何使用 numpy 和 scipy 的导入将其转移到 python 中?特别是,我被他的代码中的第 4 行“NIntegrate”函数困住了。谢谢您的帮助!

另外,如果我已经有了弧长和垂直轴长度,我如何才能反转程序以从已知值中吐出原始参数?谢谢!

python numpy ellipse scipy automatic-ref-counting

3
推荐指数
1
解决办法
5831
查看次数

为什么操作队列中不需要[weak self]或[unowned self]?

在了解了Swift 的捕获列表以及如何使用它来避免保留循环之后,我不禁注意到一些令人费解的事情OperationQueue:它不需要[weak self]或 来[unowned self]防止内存泄漏。

class SomeManager {
    let queue = OperationQueue()
    let cache: NSCache = { () -> NSCache<AnyObject, AnyObject> in
        let cache = NSCache<AnyObject, AnyObject>()
        cache.name = "huaTham.TestOperationQueueRetainCycle.someManager.cache"
        cache.countLimit = 16
        return cache
    }()

    func addTask(a: Int) {
        queue.addOperation { // "[unowned self] in" not needed?
            self.cache.setObject(a as AnyObject, forKey: a as AnyObject)
            print("hello \(a)")
        }
    }
}

class ViewController: UIViewController {

    var someM: SomeManager? = SomeManager()

    override func viewDidLoad() …
Run Code Online (Sandbox Code Playgroud)

memory-leaks nsoperationqueue automatic-ref-counting retain-cycle swift

3
推荐指数
1
解决办法
2022
查看次数

尽管没有强引用,但内存泄漏?

我正在进行性能测试,尝试测量NSOutlineViewMac 应用程序中重要部分的渲染性能。在此过程中,我循环多次,创建视图,将其嵌入虚拟窗口中,并将其渲染为图像。我概括了一点,但大致是这样的:

// Intentionally de-indented these for easier reading in this narrow page
class MyPerformanceTest: XCTestCase { reading
func test() { 
measure() {
// autoreleasepool {

    let window: NSWindow = {
        let w = NSWindow(
            contentRect: NSRect.init(x: 100, y: 100, width: 800, height: 1200),
            styleMask: [.titled, .resizable, .closable, .miniaturizable],
            backing: .buffered,
            defer: false
        )
        w.tabbingMode = .disallowed
        w.cascadeTopLeft(from: NSPoint(x: 200, y: 200))
        w.makeKeyAndOrderFront(nil)
        w.contentView = testContentView // The thing I'm performance testing
        return w
    }()

    let bitmap = …
Run Code Online (Sandbox Code Playgroud)

instruments nsoutlineview appkit automatic-ref-counting swift

3
推荐指数
1
解决办法
1168
查看次数

Swift 中的值类型自动引用计数如何工作?

我了解到 Swift 使用自动引用计数(ARC)进行内存管理。我想知道structSwift 中的值类型 () 是如何工作的。

automatic-ref-counting swift swift5

3
推荐指数
1
解决办法
674
查看次数

当我克隆一个内部有 Arc 的结构时会发生什么?

我想知道当我克隆struct具有 . 内部属性的a 时会发生什么Arc。

#[derive(Clone)]
pub struct Foo {
  bar: Arc<String>
}
Run Code Online (Sandbox Code Playgroud)

当我打电话时clone,Foo会发生什么?在线程之间共享Foo结构并使用底层是否安全Arc,或者我应该这样做吗?

#[derive(Clone)]
pub struct Foo
where
    Foo: Sync + Send,
{
    bar: Arc<String>,
}
Run Code Online (Sandbox Code Playgroud)

我本质上只是试图将一堆结构嵌套到一个结构中,这样我就可以更轻松地与不同的事物共享它们。Arc将包装器结构体作为 an并解开属性会更有意义吗?如果有必要,我可以提供更多背景信息,但现在想保持抽象。

我直接在需要的地方使用了所有弧的非分组版本,这也很好但很麻烦。我不太确定如何测试这个......

thread-safety rust automatic-ref-counting

3
推荐指数
1
解决办法
527
查看次数

内存问题或其他什么?

我正在iPad上运行一个应用程序,将图像加载到旋转木马中.总共有138张图片.如果我将该数字减少到100,应用程序加载正常.然而,在138,应用程序暂停,而不是崩溃.

for正在到达viewDidLoad和第一个语句,如断点所示.问题出在第二个for声明中.

    // loading images into the queue

loadImagesOperationQueue = [[NSOperationQueue alloc] init];
NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithCapacity:14];

for (int i = 0; i < 137; i++) {
    [tmpArray addObject:[NSString stringWithFormat:@"cover_%d.jpg", i]];
}

for (int i = 0; i < 137; i++) {
    int index = arc4random() % [tmpArray count];
    NSString *imageName = [tmpArray objectAtIndex:index];
    [tmpArray removeObjectAtIndex:index];
    [(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:imageName] forIndex:i];
}
Run Code Online (Sandbox Code Playgroud)

我猜测存在内存问题,虽然因为我使用带有ARC的 iOS 5,所以我不应该进行任何手动内存管理.

可能是因为在记忆中存在太多东西吗?这是138张图片@~146 KB每张.这大概是20 MB,但我不认为仅此一项可能导致问题.

GDB退出时没有任何有用的输出,实际上根本没有输出.运行仪器显示,当暂停发生时,实际内存使用量仅为6.11 MB,77.4%CPU但175 MB虚拟内存.

我关心的是没有内存警告甚至实际崩溃的事实,线程只是暂停,无法自动恢复或终止,你必须从xcode中删除它.

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

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