gpt4 book ai didi

objective-c - Objective-C指针

转载 作者:行者123 更新时间:2023-12-01 19:19:56 25 4
gpt4 key购买 nike

我有一个 property :

@property(nonatomic, assign)UIView *currentView;

当我处理以下代码时,为什么会中断?
_currentView  =nil;
UIView * v1 = [[UIView alloc] initWithFrame:CGRectZero];
_currentView = v1;
NSLog(@"_currentView %@", _currentView);
NSLog(@"v1 %@", v1);
[v1 release];
NSLog(@"_currentView %@", _currentView); ///break here.
NSLog(@"v1 %@", v1);

我认为 _currentViewv1都指向相同的内存。当使用 v1实现对象,并使用 _currentView打印对象时,它将崩溃。我能理解

但是如果在 v1发布之后并在打印 _currentView之前添加跟随行。我听不懂日志。
v1 = nil;

像下面的代码
_currentView  =nil;
UIView * v1 = [[UIView alloc] initWithFrame:CGRectZero];
_currentView = v1;
NSLog(@"_currentView %@", _currentView);
NSLog(@"v1 %@", v1);
[v1 release];
v1 = nil;
NSLog(@"_currentView %@", _currentView);
NSLog(@"v1 %@", v1);

打印结果是:
> 2012-05-30 15:16:57.314 All[3068:15203] _currentView <UIView:
0x81ccbc0; frame = (0 0; 0 0); layer = <CALayer: 0xa07e5a0>>
> 2012-05-30 15:16:57.798 All[3068:15203] v1 <UIView: 0x81ccbc0; frame =
(0 0; 0 0); layer = <CALayer: 0xa07e5a0>
> 2012-05-30 15:16:59.189 All[3068:15203] _currentView <UIView: 0x81ccbc0; frame = (0 0; 0 0); transform = [0, 0, 0, 0, 0, 0]; alpha = 0; layer = (null)
> 2012-05-30 15:17:09.042 All[3068:15203] v1 (null)

为什么在调用 v1版本并登录 _currentView之后为什么会打印
_currentView &lt;UIView: 0x81ccbc0; frame = (0 0; 0 0);
transform = [0, 0, 0, 0, 0, 0]; alpha = 0; layer = (null)&gt;

最佳答案

这不一定与@property属性(分配或保留)相关,因为您没有使用访问器

这是代码中发生的情况:

@property(nonatomic, assign)UIView *currentView;

您将一个ivar声明为 assign,尽管在这种情况下这无关紧要,因为您没有使用 self.currentView[self setCurrentView:...];
_currentView = nil;
// You just made your pointer _currentView point to nil or 0

UIView *v1 = [[UIView alloc] initWithFrame:CGRectZero];
// You created an UIView object and made v1 to point to it. (v1 is NOT the real object)

_currentView = v1;
// You just made _currentView to point the same object v1 points to

NSLog(@"_currentView %@", _currentView);
// and because of that you correctly see the object here (because _currentView points to the view object)

NSLog(@"v1 %@", v1);
// also here (because v1 points to the object from the start)

[v1 release];
// now you release the object pointed by v1 , since no other is retaining it, it gets deallocated BUT note that v1 is still pointing to it, which now is garbage memory!)

//NSLog(@"_currentView %@ v1 %@", _currentView, v1);
// If above line were executed the app will crash because of v1 and _currentView both are pointing to the object that was just released and it is not valid anylonger.

v1 = nil;
// Now you made v1 to point to nothing so next time you use it terrible things will not happen (★) :)

NSLog(@"_currentView %@", _currentView);
// Oh no! you called _currentView and since it was still pointing to the object you released a bit ago the app crashes :(

NSLog(@"v1 %@", v1);
// This is fine, you set v1 to point to nil so it is not pointing to some garbage memory you simply get nil.

(★)因为在Objective-C中将方法发送到 nil是无害的,所以使用nil作为其他方法的参数是另一回事

另一件事:

即使您用 self.currentView = v1;而不是 _currentView = v1;编写,结果也一样,因为正确地声明为assign。

如果将属性声明为 retain,情况将有所不同。在这种情况下,执行 [v1 release];后,该对象将不会被释放,因为该对象已被currentView( self.currentView = v1)保留。然后,如果您这样做, v1 = nil v1将指向nil,并且该对象只能由currentView访问。
然后,如果您执行 _currentView = nil,则_currentView将指向nil,但由于未使用辅助方法(也未明确释放),因此对象本身不会被释放,因此将获得一个悬空的指针。

并非所有时候声明为保留的属性都是解决方案,具体情况视情况而定。我建议多读一些有关Obj-c中的内存管理的内容(至少是 this),也要多了解一些C指针,然后是ARC

关于objective-c - Objective-C指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10811780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com