1

I have recently figured out that the screenshot code uses ~140Mb of temp memory when taking a screenshot that results in a 4MB JPEG. So I enclosed my screenshot code in a autoreleasepool as below in SWIFT

    autoreleasepool{
        UIGraphicsBeginImageContextWithOptions(cgSizeToUse, false, 0)
        webView.layer.renderInContext(UIGraphicsGetCurrentContext())
        image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }

But now when I debug, the code inside the autoreleasepool is skipped entirely. This is the only autoreleasepool in my code. Any thoughts on what is ahppening?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
shrutim
  • 1,058
  • 2
  • 12
  • 21
  • "the code inside the autoreleasepool is skipped entirely" How do you know? – matt Jun 06 '15 at 23:39
  • Walk through of the code under debugger. I can see the control reach the line "autoreleasepool{" and when I hit the next button in debugger, the control reaches the end of the autoreleasepool. And I have code to verify if the screenshot was taken – shrutim Jun 06 '15 at 23:59
  • Ah. But what if you put a breakpoint on the first line inside the autoreleasepool, and just run? Do you or do you not eventually hit that breakpoint? – matt Jun 06 '15 at 23:59

2 Answers2

4

The form statement {...} is actually a statement followed by a block closure.

The block closure really is running, but the debugger treats it as a single statement in and of itself. Set a breakpoint on the first line inside the block and you'll be able to step through the block closure.

This is the declaration for autoreleasepool:

func autoreleasepool(code: @noescape () -> ())

As it takes only a single closure as an argument, then we are free to drop the parens and use a trailing closure.

See the answers to this question for an explanation of @noescape: @noescape attribute in Swift 1.2

Edited because Objective-C graybeard got the nomenclature wrong.

Community
  • 1
  • 1
bbum
  • 162,346
  • 23
  • 271
  • 359
3

The code inside the autoreleasepool block does run. It's just that your method of determining whether or not it runs is misleading you.

matt
  • 515,959
  • 87
  • 875
  • 1,141