English 中文(简体)
目标-c 保留、转让和复制?
原标题:objective-c retain , assign and copy?

我删除了上一个职位...

我用哪一种方法,如果我有一个类 拥有一个对象 objA?我知道如果该对象不是该类拥有的, 它需要保留。

@class A

@property (retain) ObjectA objA;

@end

@implementation A

-(void) func {
   self.objA = [[ObjectA alloc] init];
}

@end

如果将A类的objA设定为保留, 并使用 init 函数在 func 中初始化。 这会给两个保留计数还是仅一个保留计数 。

最佳回答

alloc gives retain count 1. self.objA = will give retain count 2 (because of the retain property)

问题回答

我会做:

   - (void) func {objA = [[ObjectA alloc] init]; }

   - (void) func { self.objA = [[[ObjectA alloc] init] autorelease]; }

防止内存泄漏。 但我更喜欢第一个解决方案





相关问题
Windows Mobile 6 Emulator change storage?

How do i change the size of the Windows Mobile 6 Emulator. Its fixed at 32mb. I read this post: Increasing Windows Mobile 5 Emulator Storage But it only helps for the 5.0 version. Isnt there any way ...

CUDA Memory Allocation accessible for both host and device

I m trying to figure out a way to allocate a block of memory that is accessible by both the host (CPU) and device (GPU). Other than using cudaHostAlloc() function to allocate page-locked memory that ...

RAM memory reallocation - Windows and Linux

I am working on a project involving optimizing energy consumption within a system. Part of that project consists in allocating RAM memory based on locality, that is allocating memory segments for a ...

Should I send retain or autorelease before returning objects?

I thought I was doing the right thing here but I get several warnings from the Build and Analyze so now I m not so sure. My assumption is (a) that an object I get from a function (dateFromComponents: ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

doubts regarding Memory management in .net

I m learning about Memory management in C# from the book "Professional C#" The presence of the garbage collector means that you will usually not worry about objects that you no longer need; ...

Objective-C returning alloc d memory in a function == bad?

This is on the iPhone. So what if I have a function like - (SomeObject*)buildObject; Do I need to pass in a variable that I have already alloc d outside like - (void)assignObject(SomeObject** out);...

热门标签