有没有办法枚举所有属性并一一释放?

发布于 2024-12-05 11:18:38 字数 925 浏览 0 评论 0原文

典型的 dealloc

好吧,这很糟糕。如果我忘记要释放的财产怎么办?

为什么我们不能拥有像 dealloc 所有属性这样的东西?

最糟糕的是,我有点喜欢现在处理 viewController 的方式。我们放置了一个属性,然后dealloc就被放置了。正常上课就不能这样吗?

- (void)dealloc
{
    [_window release];
    [__managedObjectContext release];
    [__managedObjectModel release];
    [__persistentStoreCoordinator release];
    [_MainBadgerApplication release];
    [_SettingsMiscelaneous release];
    [_theNearByIsiKota release];
    [Customcell release];
    [PhoneCC release];
    [searchViewController release];
    [_superTabBar release];
    [_SuperNavBar release];
    [pinNumberView release];
    [_lblForPinNumber release];
    [navController release];
    [NearbyShortcut release];
    [_searchListView release];
    [_searchNearView release];
    [LoadingView release];
    [super dealloc];
}

我正在考虑一个宏,我可以在其中执行

myDealloc

,它将枚举所有属性并一一释放它们或将它们设置为 nil (这几乎是等效的)

Typical dealloc

Well it sucks. What about if I forget a property to dealloc?

Why can't we have something like dealloc All properties

Worst of all, I sort of like the way viewControllers are handled now. We put a property and poof the dealloc is put. Can't have that on normal classes ha?

- (void)dealloc
{
    [_window release];
    [__managedObjectContext release];
    [__managedObjectModel release];
    [__persistentStoreCoordinator release];
    [_MainBadgerApplication release];
    [_SettingsMiscelaneous release];
    [_theNearByIsiKota release];
    [Customcell release];
    [PhoneCC release];
    [searchViewController release];
    [_superTabBar release];
    [_SuperNavBar release];
    [pinNumberView release];
    [_lblForPinNumber release];
    [navController release];
    [NearbyShortcut release];
    [_searchListView release];
    [_searchNearView release];
    [LoadingView release];
    [super dealloc];
}

I am thinking of a macro where I can just do

myDealloc

and that will enumerate all properties and release them one by one or set them to nil (which is almost equivalent)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

べ映画 2024-12-12 11:18:38

这看起来是一个可怕的想法,所以这里是代码:D 这将逐步遍历代码中的每个对象属性并发送发布。我没有编译它,但它或多或少是这样的。

#import <objc/runtime.h>

unsigned int propertyCount;
objc_property_t *properties = class_copyPropertyList([self class], &propertyCount);
for (unsigned int i = 0; i < propertyCount; i++)
{
    SEL sel = @selector(release);
    const char *attr = property_getAttributes(properties[i]);
    switch (attr[1]) {
        case '@':
            property_getName(properties[i]), objc_msgSend(self, sel)]
            break;
        default:
            break;
    }
}
free(properties);

您可以将其包装在 C 函数中并从 dealloc 中调用它。

This looks like a horrible idea, so here is the code :D This will step through every object property on your code and send a release. I didn't compile it, but it's more or less like this.

#import <objc/runtime.h>

unsigned int propertyCount;
objc_property_t *properties = class_copyPropertyList([self class], &propertyCount);
for (unsigned int i = 0; i < propertyCount; i++)
{
    SEL sel = @selector(release);
    const char *attr = property_getAttributes(properties[i]);
    switch (attr[1]) {
        case '@':
            property_getName(properties[i]), objc_msgSend(self, sel)]
            break;
        default:
            break;
    }
}
free(properties);

You can wrap it in a C function and call it from your dealloc.

雪花飘飘的天空 2024-12-12 11:18:38

您在这里不是枚举属性,而是实例变量(当然,这可能是与属性关联的支持变量,但也可能不是,您没有给出声明属性的代码,所以我们无法知道)

我的建议:

  • 使用现代运行时(自 iOS 存在以来就被 iOS 使用,而 OSX 仅在 64 位中使用)自动生成属性的后备存储。因此,您不需要在头文件中为属性声明实例变量,也不会想直接使用实例变量来代替
  • dealloc 中的属性以及 < code>viewDidUnload 方法用于绑定到 IBOutlets 的属性 - 将您的 @properties 设置为 nil (而不是释放 ivar
  • 最后,一旦您以这种方式管理属性,这将是可以设置你的所有属性通过内省将其归零

一旦您按照上面的说明进行操作,您就可以使用内省:使用Objective-C 运行时的函数 获取每个给定类的 @property ,然后查看它们并将它们设置为 nil (这将释放与 @property 关联的支持变量的内存代码>)

You are not enumerating properties here, but instance variables (that may be the backing variables associated by properties, sure, but maybe not, you didn't give the code that declares the properties so we cannot know)

My suggestion:

  • Use the ability of the modern runtime (used by iOS since iOS exists -- and by OSX only in 64 bits) to automatically generate the backing store of properties. Thus you won't need to declare instance variables for your properties in the header file, and won't be tempted to use the instance variable directly instead of the property
  • In the dealloc -- and in the viewDidUnload method for properties that are bound to IBOutlets -- set your @properties to nil (instead of releasing the ivar
  • Finally, once you are managing your properties this way, this will be possible to set all of your properties to nil by introspection.

Once your are doing like explained above, You can use introspection: use the functions of the Objective-C Runtime to get every @property of a given class, then look thru them and set them to nil (which will release the memory of the backing variable associated with the @property)

空‖城人不在 2024-12-12 11:18:38

我建议采用另一种方法。首先,在 init 或 dealloc 中将属性设置为 nil 违反了 Apple 的准则。这是因为您可能正在调用 setter 方法。例如,如果开发人员覆盖了标准属性设置器并定义了自定义设置器方法。

在这种情况下,在 init 或 dealloc 期间调用此类方法可能会产生不可预测的结果。

最好调用 [myIvar release];和平常一样。

要追踪缺失的发布声明,请运行分析器。或者,更好的是,将目标的构建设置设置为在构建时始终运行分析器。

最后,定期在 Instruments 中运行您的应用程序是值得的。这将发现泄漏,并节省您追踪奇怪错误的时间,否则泄漏的内存可能会发生这些错误。

I would suggest an alternative approach. Firstly, setting properties to nil in either the init or the dealloc goes against Apple's guidelines. This is because you could be invoking a setter method. For example if the developer has overridden the standard property setter and defined a custom setter method.

In that case, invoking such a method during an init or dealloc can give unpredictable results.

Better to invoke [myIvar release]; as normal.

To track down missing release statements, run the Analyzer. Or, even better, set the target's build settings to always run Analyzer when you do a build.

Finally, it's worthwhile periodically running your app in Instruments. That will pick up leaks and will save you time in tracking down strange bugs that would otherwise happen with the leaked memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文