-3

I know, that i can use autorelease block in swift with NSObject and other subclass, but why i can't use it with SwiftObject(see links and stop minus me =))? What fundamental difference between SwiftObject and NSObject? How SwiftObject work with ARC, if autoreleasepool does not work?

Links:

  1. stackoverflow.com/a/25880106/3527461

  2. https://devstreaming-cdn.apple.com/videos/wwdc/2014/418xxtihju1a7v4/418/418_improving_your_app_with_instruments.pdf

1 Answers1

1

By "SwiftObject" I assume you mean "a Swift object that is not bridged to Objective-C."

For those objects, there's little need for an autorelease pool. The concept of autorelease was created to simplify certain manual retain counting situations, most importantly, when you return a value, but you don't want to force the caller to call -release on it because that would be a hassle. So you schedule a -release to be called "at some future time," an "auto-release."

But with ARC, this isn't really necessary. The compiler is happy to do the tedious work of adding extra release calls in all the places they'd be needed, and the optimizer is happy to take them back out from all the places they aren't really needed. Since you cannot create manual retain-counted code in Swift, there's no particular need to have special language support for a convenience system like autorelease.

This isn't to say that non-ObjC Swift objects won't wind up on the autorelease pool; they still can (though generally because of some kind of implicit bridging). But the programmer doesn't need to worry about it.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • No! It is not correct! According this answer https://stackoverflow.com/a/25880106/3527461 and WDDC 14, we don't get some benefirt if use autorelease pool with Swift Native Object. And autorelease pool it is not only release / retain! Autorelease pool also invoke drain() function, that need to help to minimize the peak memory footprint. And therefore we need to use autoreleasepool in some situation. – Stanislav Kramarenko Mar 08 '19 at 06:26
  • I think you're misunderstanding that answer and the discussions at WWDC.You don't get the benefits in pure Swift types because you don't have the cost. The `-drain` method just sends the pending `-release` messages to all the objects that had previously received `-autorelease` messages. Swift objects typically do not receive `-autorelease` messages (though in some cases they do, because they contain internal NSObject subclasses; and in those cases, they participate in the autorelease pool via their internal NSObject subclasses). – Rob Napier Mar 08 '19 at 14:35
  • Note how in the linked slides, the statement is "Working with Objective-C? Still have to manage autorelease pools." When you're not working with ObjC, you don't need to manage autorelease pools (generally, unless you're implicitly working with ObjC under the covers). – Rob Napier Mar 08 '19 at 14:37
  • Do you have an example of Swift code where you high-water mark is rising in a way that you believe the autorelease pool would address? You should add that example to the question so we can talk through it and how the high-water mark works in Swift. – Rob Napier Mar 08 '19 at 14:39