Autoreleasepool for Ubuntu


(Bernardo Breder) #1

Hello,

I want to create a mini http server project and execute at Ubuntu 15. The
Xcode compile and access the function "autoreleasepool", but when i compile
the same code at Ubuntu, this function not found

For example, i can compile the code above at Xcode:

while true {
    autoreleasepool {
    var test: Data = "HTTP/1.1 200 OK\r\nContent-Length:
1\r\n\r\na".data(using: .utf8)!
    }
}

But when i try to compile at Ubuntu:

git@breder:~$ cat main.swift
import Foundation

while true {
    autoreleasepool {
    var test: Data = "HTTP/1.1 200 OK\r\nContent-Length:
1\r\n\r\na".data(using: .utf8)!
    }
}

git@breder:~$ swiftc main.swift
main.swift:4:5: error: use of unresolved identifier 'autoreleasepool'
    autoreleasepool {
    ^~~~~~~~~~~~~~~


(Joe Groff) #2

Autoreleasepools are an ObjC compatibility feature. They aren't necessary in standalone Swift.

-Joe

···

On Nov 1, 2016, at 6:40 PM, Bernardo Breder via swift-users <swift-users@swift.org> wrote:

Hello,

I want to create a mini http server project and execute at Ubuntu 15. The Xcode compile and access the function "autoreleasepool", but when i compile the same code at Ubuntu, this function not found

For example, i can compile the code above at Xcode:

while true {
    autoreleasepool {
    var test: Data = "HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\na".data(using: .utf8)!
    }
}

But when i try to compile at Ubuntu:

git@breder:~$ cat main.swift
import Foundation

while true {
    autoreleasepool {
    var test: Data = "HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\na".data(using: .utf8)!
    }
}

git@breder:~$ swiftc main.swift
main.swift:4:5: error: use of unresolved identifier 'autoreleasepool'
    autoreleasepool {
    ^~~~~~~~~~~~~~~


(Jordan Rose) #3

But they are necessary in Swift programs on Apple platforms (that don’t use RunLoop, anyway). Philippe, what do you think? What’s the right way to write cross-platform code that doesn’t use RunLoop or dispatch_main for an implicit autorelease pool?

(/me remembers +[NSAutoreleasePool drain] from the ObjC-GC days)

Jordan

···

On Nov 2, 2016, at 09:42, Joe Groff via swift-users <swift-users@swift.org> wrote:

On Nov 1, 2016, at 6:40 PM, Bernardo Breder via swift-users <swift-users@swift.org> wrote:

Hello,

I want to create a mini http server project and execute at Ubuntu 15. The Xcode compile and access the function "autoreleasepool", but when i compile the same code at Ubuntu, this function not found

For example, i can compile the code above at Xcode:

while true {
   autoreleasepool {
   var test: Data = "HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\na".data(using: .utf8)!
   }
}

But when i try to compile at Ubuntu:

git@breder:~$ cat main.swift
import Foundation

while true {
   autoreleasepool {
   var test: Data = "HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\na".data(using: .utf8)!
   }
}

git@breder:~$ swiftc main.swift
main.swift:4:5: error: use of unresolved identifier 'autoreleasepool'
   autoreleasepool {
   ^~~~~~~~~~~~~~~

Autoreleasepools are an ObjC compatibility feature. They aren't necessary in standalone Swift.


(Joe Groff) #4

If you must, you could conditionally define `autoreleasepool` to just call its argument as a compatibility shim.

-Joe

···

On Nov 2, 2016, at 10:17 AM, Jordan Rose <jordan_rose@apple.com> wrote:

On Nov 2, 2016, at 09:42, Joe Groff via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

On Nov 1, 2016, at 6:40 PM, Bernardo Breder via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hello,

I want to create a mini http server project and execute at Ubuntu 15. The Xcode compile and access the function "autoreleasepool", but when i compile the same code at Ubuntu, this function not found

For example, i can compile the code above at Xcode:

while true {
   autoreleasepool {
   var test: Data = "HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\na".data(using: .utf8)!
   }
}

But when i try to compile at Ubuntu:

git@breder:~$ cat main.swift
import Foundation

while true {
   autoreleasepool {
   var test: Data = "HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\na".data(using: .utf8)!
   }
}

git@breder:~$ swiftc main.swift
main.swift:4:5: error: use of unresolved identifier 'autoreleasepool'
   autoreleasepool {
   ^~~~~~~~~~~~~~~

Autoreleasepools are an ObjC compatibility feature. They aren't necessary in standalone Swift.

But they are necessary in Swift programs on Apple platforms (that don’t use RunLoop, anyway). Philippe, what do you think? What’s the right way to write cross-platform code that doesn’t use RunLoop or dispatch_main for an implicit autorelease pool?

(/me remembers +[NSAutoreleasePool drain] from the ObjC-GC days)


(Jens Alfke) #5

Autoreleasepools are an ObjC compatibility feature. They aren't necessary in standalone Swift.

So Swift’s ref-counting doesn’t use any autorelease mechanism? I was assuming it did (even if it’s not identical to Obj-C’s.) I implemented an ARC-like library for C++ back around 2007 and found that adding an autorelease pool helped by keeping object ref-counts from bouncing up and down so much during assignment and return value passing.

···

On Nov 2, 2016, at 09:42, Joe Groff via swift-users <swift-users@swift.org> wrote:

On Nov 2, 2016, at 11:11 AM, Bernardo Breder via swift-users <swift-users@swift.org> wrote:

I want to manager the memory, using the autoreleasepool block, with ubuntu environment, as we can do with Xcode.

Apparently you don’t need to because pure Swift objects never get autoreleased, so there’s no pool that needs draining.

—Jens


(Philippe Hausler) #6

So there are issues we have in swift-corelibs that suffer(leak) because we don't have ARPs on Linux. It would be super nice to have a retain until scope end concept for swift core libs where autorelease would be an accessor in unmanaged that would retain the object until the arp ends scope.

···

Sent from my iPhone

On Nov 2, 2016, at 10:17 AM, Jordan Rose <jordan_rose@apple.com> wrote:

On Nov 2, 2016, at 09:42, Joe Groff via swift-users <swift-users@swift.org> wrote:

On Nov 1, 2016, at 6:40 PM, Bernardo Breder via swift-users <swift-users@swift.org> wrote:

Hello,

I want to create a mini http server project and execute at Ubuntu 15. The Xcode compile and access the function "autoreleasepool", but when i compile the same code at Ubuntu, this function not found

For example, i can compile the code above at Xcode:

while true {
   autoreleasepool {
   var test: Data = "HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\na".data(using: .utf8)!
   }
}

But when i try to compile at Ubuntu:

git@breder:~$ cat main.swift
import Foundation

while true {
   autoreleasepool {
   var test: Data = "HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\na".data(using: .utf8)!
   }
}

git@breder:~$ swiftc main.swift
main.swift:4:5: error: use of unresolved identifier 'autoreleasepool'
   autoreleasepool {
   ^~~~~~~~~~~~~~~

Autoreleasepools are an ObjC compatibility feature. They aren't necessary in standalone Swift.

But they are necessary in Swift programs on Apple platforms (that don’t use RunLoop, anyway). Philippe, what do you think? What’s the right way to write cross-platform code that doesn’t use RunLoop or dispatch_main for an implicit autorelease pool?

(/me remembers +[NSAutoreleasePool drain] from the ObjC-GC days)

Jordan


(Philippe Hausler) #7

So trivially we could do something along these lines to “solve it” in swift-corelibs-foundation instead of polluting the swift standard library with objective-c-isms on Linux.

internal class NSAutoreleasePool {
    fileprivate static var _current = NSThreadSpecific<NSAutoreleasePool>()
    internal static var current: NSAutoreleasePool {
        return _current.get() {
            return NSAutoreleasePool()
        }
    }
    
    var depth: Int = 0
    var objects = [[AnyObject]]()
    
    fileprivate override init() { }
    
    fileprivate func push() {
        objects.append([AnyObject]())
        depth += 1
    }
    
    fileprivate func pop() {
        objects.removeLast()
        depth -= 1
    }
    
    func add(_ object: AnyObject) {
        objects[depth - 1].append(object)
    }
}

public func autoreleasepool(_ code: () -> ()) {
    NSAutoreleasePool.current.push()
    code()
    NSAutoreleasePool.current.pop()
}

···

On Nov 2, 2016, at 12:23 PM, Philippe Hausler via swift-users <swift-users@swift.org> wrote:

So there are issues we have in swift-corelibs that suffer(leak) because we don't have ARPs on Linux. It would be super nice to have a retain until scope end concept for swift core libs where autorelease would be an accessor in unmanaged that would retain the object until the arp ends scope.

Sent from my iPhone

On Nov 2, 2016, at 10:17 AM, Jordan Rose <jordan_rose@apple.com <mailto:jordan_rose@apple.com>> wrote:

On Nov 2, 2016, at 09:42, Joe Groff via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

On Nov 1, 2016, at 6:40 PM, Bernardo Breder via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hello,

I want to create a mini http server project and execute at Ubuntu 15. The Xcode compile and access the function "autoreleasepool", but when i compile the same code at Ubuntu, this function not found

For example, i can compile the code above at Xcode:

while true {
   autoreleasepool {
   var test: Data = "HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\na".data(using: .utf8)!
   }
}

But when i try to compile at Ubuntu:

git@breder:~$ cat main.swift
import Foundation

while true {
   autoreleasepool {
   var test: Data = "HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\na".data(using: .utf8)!
   }
}

git@breder:~$ swiftc main.swift
main.swift:4:5: error: use of unresolved identifier 'autoreleasepool'
   autoreleasepool {
   ^~~~~~~~~~~~~~~

Autoreleasepools are an ObjC compatibility feature. They aren't necessary in standalone Swift.

But they are necessary in Swift programs on Apple platforms (that don’t use RunLoop, anyway). Philippe, what do you think? What’s the right way to write cross-platform code that doesn’t use RunLoop or dispatch_main for an implicit autorelease pool?

(/me remembers +[NSAutoreleasePool drain] from the ObjC-GC days)

Jordan

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Jordan Rose) #8

I’m confused about this. Shouldn’t you be able to get away with using +1 convention everywhere? What needs to have arbitrary lifetime-extension in an ARC-ified language?

Jordan

···

On Nov 2, 2016, at 12:23, Philippe Hausler <phausler@apple.com> wrote:

So there are issues we have in swift-corelibs that suffer(leak) because we don't have ARPs on Linux. It would be super nice to have a retain until scope end concept for swift core libs where autorelease would be an accessor in unmanaged that would retain the object until the arp ends scope.

Sent from my iPhone

On Nov 2, 2016, at 10:17 AM, Jordan Rose <jordan_rose@apple.com <mailto:jordan_rose@apple.com>> wrote:

On Nov 2, 2016, at 09:42, Joe Groff via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

On Nov 1, 2016, at 6:40 PM, Bernardo Breder via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hello,

I want to create a mini http server project and execute at Ubuntu 15. The Xcode compile and access the function "autoreleasepool", but when i compile the same code at Ubuntu, this function not found

For example, i can compile the code above at Xcode:

while true {
   autoreleasepool {
   var test: Data = "HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\na".data(using: .utf8)!
   }
}

But when i try to compile at Ubuntu:

git@breder:~$ cat main.swift
import Foundation

while true {
   autoreleasepool {
   var test: Data = "HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\na".data(using: .utf8)!
   }
}

git@breder:~$ swiftc main.swift
main.swift:4:5: error: use of unresolved identifier 'autoreleasepool'
   autoreleasepool {
   ^~~~~~~~~~~~~~~

Autoreleasepools are an ObjC compatibility feature. They aren't necessary in standalone Swift.

But they are necessary in Swift programs on Apple platforms (that don’t use RunLoop, anyway). Philippe, what do you think? What’s the right way to write cross-platform code that doesn’t use RunLoop or dispatch_main for an implicit autorelease pool?

(/me remembers +[NSAutoreleasePool drain] from the ObjC-GC days)

Jordan


(Bernardo Breder) #9

I want to manager the memory, using the autoreleasepool block, with ubuntu
environment, as we can do with Xcode.

···

2016-11-02 15:34 GMT-02:00 Joe Groff <jgroff@apple.com>:

On Nov 2, 2016, at 10:17 AM, Jordan Rose <jordan_rose@apple.com> wrote:

On Nov 2, 2016, at 09:42, Joe Groff via swift-users <swift-users@swift.org> > wrote:

On Nov 1, 2016, at 6:40 PM, Bernardo Breder via swift-users < > swift-users@swift.org> wrote:

Hello,

I want to create a mini http server project and execute at Ubuntu 15. The
Xcode compile and access the function "autoreleasepool", but when i compile
the same code at Ubuntu, this function not found

For example, i can compile the code above at Xcode:

while true {
   autoreleasepool {
   var test: Data = "HTTP/1.1 200 OK\r\nContent-Length:
1\r\n\r\na".data(using: .utf8)!
   }
}

But when i try to compile at Ubuntu:

git@breder:~$ cat main.swift
import Foundation

while true {
   autoreleasepool {
   var test: Data = "HTTP/1.1 200 OK\r\nContent-Length:
1\r\n\r\na".data(using: .utf8)!
   }
}

git@breder:~$ swiftc main.swift
main.swift:4:5: error: use of unresolved identifier 'autoreleasepool'
   autoreleasepool {
   ^~~~~~~~~~~~~~~

Autoreleasepools are an ObjC compatibility feature. They aren't necessary
in standalone Swift.

But they *are* necessary in Swift programs on Apple platforms (that don’t
use RunLoop, anyway). Philippe, what do you think? What’s the right way to
write cross-platform code that doesn’t use RunLoop or dispatch_main for an
implicit autorelease pool?

(/me remembers +[NSAutoreleasePool drain] from the ObjC-GC days)

If you must, you could conditionally define `autoreleasepool` to just call
its argument as a compatibility shim.

-Joe

--
Nome : Bernardo Breder
Product : Breder Language
Site : bernardobreder.com
Email : bernardobreder@gmail.com
Email : bbreder@tecgraf.puc-rio.br
Email : contato@bernardobreder.com
Graduação : UFF - Ciência da Computação
Mestrado : UFF - Ciência da Computação
Trabalho : TecGraf - PUC-RIO


(Philippe Hausler) #10

See:

https://github.com/apple/swift-corelibs-foundation/blob/d015466450b2675037c6f1ace8e17e73050ccfb9/Foundation/NSURL.swift#L561

This is far and few between of cases that it would be useful but there are a few APIs that we have not been able to express without being able to autorelease items. Most of which we have either forbidden in Linux or redesigned because they were sub-par swift experiences. However it seems reasonable to have a minimal shim to provide cross platform code compatibility even if it does next to nothing. That way trivial code as the original issue showed can easily be directly compiled on either platform without littering gnarly #ifdefs about.

···

On Nov 2, 2016, at 12:55 PM, Jordan Rose <jordan_rose@apple.com> wrote:

I’m confused about this. Shouldn’t you be able to get away with using +1 convention everywhere? What needs to have arbitrary lifetime-extension in an ARC-ified language?

Jordan

On Nov 2, 2016, at 12:23, Philippe Hausler <phausler@apple.com <mailto:phausler@apple.com>> wrote:

So there are issues we have in swift-corelibs that suffer(leak) because we don't have ARPs on Linux. It would be super nice to have a retain until scope end concept for swift core libs where autorelease would be an accessor in unmanaged that would retain the object until the arp ends scope.

Sent from my iPhone

On Nov 2, 2016, at 10:17 AM, Jordan Rose <jordan_rose@apple.com <mailto:jordan_rose@apple.com>> wrote:

On Nov 2, 2016, at 09:42, Joe Groff via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

On Nov 1, 2016, at 6:40 PM, Bernardo Breder via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hello,

I want to create a mini http server project and execute at Ubuntu 15. The Xcode compile and access the function "autoreleasepool", but when i compile the same code at Ubuntu, this function not found

For example, i can compile the code above at Xcode:

while true {
   autoreleasepool {
   var test: Data = "HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\na".data(using: .utf8)!
   }
}

But when i try to compile at Ubuntu:

git@breder:~$ cat main.swift
import Foundation

while true {
   autoreleasepool {
   var test: Data = "HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\na".data(using: .utf8)!
   }
}

git@breder:~$ swiftc main.swift
main.swift:4:5: error: use of unresolved identifier 'autoreleasepool'
   autoreleasepool {
   ^~~~~~~~~~~~~~~

Autoreleasepools are an ObjC compatibility feature. They aren't necessary in standalone Swift.

But they are necessary in Swift programs on Apple platforms (that don’t use RunLoop, anyway). Philippe, what do you think? What’s the right way to write cross-platform code that doesn’t use RunLoop or dispatch_main for an implicit autorelease pool?

(/me remembers +[NSAutoreleasePool drain] from the ObjC-GC days)

Jordan


(Jordan Rose) #11

I don’t know, this isn’t really a good Swift API either. Some of the other bare pointer cases we changed to return arrays instead for this reason, like String’s version of cString(using:). https://github.com/apple/swift/blob/master/stdlib/public/SDK/Foundation/NSStringAPI.swift#L413

I am actually in favor of having a minimal shim, but I’d rather it do nothing like Joe’s case than to start allowing arbitrarily-delayed deinitialization like this.

Jordan

···

On Nov 2, 2016, at 13:00, Philippe Hausler <phausler@apple.com> wrote:

See:

https://github.com/apple/swift-corelibs-foundation/blob/d015466450b2675037c6f1ace8e17e73050ccfb9/Foundation/NSURL.swift#L561

This is far and few between of cases that it would be useful but there are a few APIs that we have not been able to express without being able to autorelease items. Most of which we have either forbidden in Linux or redesigned because they were sub-par swift experiences. However it seems reasonable to have a minimal shim to provide cross platform code compatibility even if it does next to nothing. That way trivial code as the original issue showed can easily be directly compiled on either platform without littering gnarly #ifdefs about.

On Nov 2, 2016, at 12:55 PM, Jordan Rose <jordan_rose@apple.com <mailto:jordan_rose@apple.com>> wrote:

I’m confused about this. Shouldn’t you be able to get away with using +1 convention everywhere? What needs to have arbitrary lifetime-extension in an ARC-ified language?

Jordan

On Nov 2, 2016, at 12:23, Philippe Hausler <phausler@apple.com <mailto:phausler@apple.com>> wrote:

So there are issues we have in swift-corelibs that suffer(leak) because we don't have ARPs on Linux. It would be super nice to have a retain until scope end concept for swift core libs where autorelease would be an accessor in unmanaged that would retain the object until the arp ends scope.

Sent from my iPhone

On Nov 2, 2016, at 10:17 AM, Jordan Rose <jordan_rose@apple.com <mailto:jordan_rose@apple.com>> wrote:

On Nov 2, 2016, at 09:42, Joe Groff via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

On Nov 1, 2016, at 6:40 PM, Bernardo Breder via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Hello,

I want to create a mini http server project and execute at Ubuntu 15. The Xcode compile and access the function "autoreleasepool", but when i compile the same code at Ubuntu, this function not found

For example, i can compile the code above at Xcode:

while true {
   autoreleasepool {
   var test: Data = "HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\na".data(using: .utf8)!
   }
}

But when i try to compile at Ubuntu:

git@breder:~$ cat main.swift
import Foundation

while true {
   autoreleasepool {
   var test: Data = "HTTP/1.1 200 OK\r\nContent-Length: 1\r\n\r\na".data(using: .utf8)!
   }
}

git@breder:~$ swiftc main.swift
main.swift:4:5: error: use of unresolved identifier 'autoreleasepool'
   autoreleasepool {
   ^~~~~~~~~~~~~~~

Autoreleasepools are an ObjC compatibility feature. They aren't necessary in standalone Swift.

But they are necessary in Swift programs on Apple platforms (that don’t use RunLoop, anyway). Philippe, what do you think? What’s the right way to write cross-platform code that doesn’t use RunLoop or dispatch_main for an implicit autorelease pool?

(/me remembers +[NSAutoreleasePool drain] from the ObjC-GC days)

Jordan


(Joe Groff) #12

In the fullness of time, the borrow model will hopefully give us a way to represent those kinds of "returns inner pointer" APIs safely in Swift without relying on dynamic lifetime extension, or awkward 'with { ... }' callbacks.

-Joe

···

On Nov 2, 2016, at 1:00 PM, Philippe Hausler <phausler@apple.com> wrote:

See:

https://github.com/apple/swift-corelibs-foundation/blob/d015466450b2675037c6f1ace8e17e73050ccfb9/Foundation/NSURL.swift#L561

This is far and few between of cases that it would be useful but there are a few APIs that we have not been able to express without being able to autorelease items. Most of which we have either forbidden in Linux or redesigned because they were sub-par swift experiences. However it seems reasonable to have a minimal shim to provide cross platform code compatibility even if it does next to nothing. That way trivial code as the original issue showed can easily be directly compiled on either platform without littering gnarly #ifdefs about.


(Greg Parker) #13

Not to mention using autoreleasepool to handle returns-inner-pointer isn't memory safe anyway. If the autorelease pool pops too soon then you end up with a dangling pointer that no amount of compiler analysis can detect.

···

On Nov 2, 2016, at 1:08 PM, Joe Groff via swift-users <swift-users@swift.org> wrote:

On Nov 2, 2016, at 1:00 PM, Philippe Hausler <phausler@apple.com <mailto:phausler@apple.com>> wrote:

See:

https://github.com/apple/swift-corelibs-foundation/blob/d015466450b2675037c6f1ace8e17e73050ccfb9/Foundation/NSURL.swift#L561

This is far and few between of cases that it would be useful but there are a few APIs that we have not been able to express without being able to autorelease items. Most of which we have either forbidden in Linux or redesigned because they were sub-par swift experiences. However it seems reasonable to have a minimal shim to provide cross platform code compatibility even if it does next to nothing. That way trivial code as the original issue showed can easily be directly compiled on either platform without littering gnarly #ifdefs about.

In the fullness of time, the borrow model will hopefully give us a way to represent those kinds of "returns inner pointer" APIs safely in Swift without relying on dynamic lifetime extension, or awkward 'with { ... }' callbacks.

--
Greg Parker gparker@apple.com Runtime Wrangler


(Bernardo Breder) #14

In my http server i want to manager the memory all the time that we close a
socket, like the example of manager in this link:
http://stackoverflow.com/questions/25860942/is-it-necessary-to-use-autoreleasepool-in-a-swift-program

Algorithm that show the ideia:

*func request(content) { ... }*

*let server = myserver()*
*while let client = server.accept() {*
* autoreleasepool {*
* client.send(request(client.read()))*
* client.close()*
* }*
*}*

···

2016-11-02 18:08 GMT-02:00 Joe Groff <jgroff@apple.com>:

On Nov 2, 2016, at 1:00 PM, Philippe Hausler <phausler@apple.com> wrote:

See:

https://github.com/apple/swift-corelibs-foundation/blob/
d015466450b2675037c6f1ace8e17e73050ccfb9/Foundation/NSURL.swift#L561

This is far and few between of cases that it would be useful but there are
a few APIs that we have not been able to express without being able to
autorelease items. Most of which we have either forbidden in Linux or
redesigned because they were sub-par swift experiences. However it seems
reasonable to have a minimal shim to provide cross platform code
compatibility even if it does next to nothing. That way trivial code as the
original issue showed can easily be directly compiled on either platform
without littering gnarly #ifdefs about.

In the fullness of time, the borrow model will hopefully give us a way to
represent those kinds of "returns inner pointer" APIs safely in Swift
without relying on dynamic lifetime extension, or awkward 'with { ... }'
callbacks.

-Joe

--
Nome : Bernardo Breder
Product : Breder Language
Site : bernardobreder.com
Email : bernardobreder@gmail.com
Email : bbreder@tecgraf.puc-rio.br
Email : contato@bernardobreder.com
Graduação : UFF - Ciência da Computação
Mestrado : UFF - Ciência da Computação
Trabalho : TecGraf - PUC-RIO


(Joe Groff) #15

Is `client` really getting autoreleased somewhere? autoreleasepool shouldn't normally be necessary. The client will be released when you go out of scope.

-Joe

···

On Nov 2, 2016, at 1:16 PM, Bernardo Breder via swift-users <swift-users@swift.org> wrote:

In my http server i want to manager the memory all the time that we close a socket, like the example of manager in this link: http://stackoverflow.com/questions/25860942/is-it-necessary-to-use-autoreleasepool-in-a-swift-program

Algorithm that show the ideia:

func request(content) { ... }

let server = myserver()
while let client = server.accept() {
  autoreleasepool {
    client.send(request(client.read()))
    client.close()
  }
}


(Jordan Rose) #16

The problem is that on Apple platforms, the Foundation APIs used to implement the client autorelease things all over the place, so you need an autorelease pool somewhere to clean up the mess intermediate objects returned at +0.

Jordan

···

On Nov 2, 2016, at 13:36, Joe Groff via swift-users <swift-users@swift.org> wrote:

On Nov 2, 2016, at 1:16 PM, Bernardo Breder via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

In my http server i want to manager the memory all the time that we close a socket, like the example of manager in this link: http://stackoverflow.com/questions/25860942/is-it-necessary-to-use-autoreleasepool-in-a-swift-program

Algorithm that show the ideia:

func request(content) { ... }

let server = myserver()
while let client = server.accept() {
  autoreleasepool {
    client.send(request(client.read()))
    client.close()
  }
}

Is `client` really getting autoreleased somewhere? autoreleasepool shouldn't normally be necessary. The client will be released when you go out of scope.


(Bernardo Breder) #17

In my http server i want to manager the memory all the time that we close
a socket, like the example of manager in this link:
http://stackoverflow.com/questions/25860942/is-it-nece
ssary-to-use-autoreleasepool-in-a-swift-program

Algorithm that show the ideia:

*func request(content) { ... }*

*let server = myserver()*
*while let client = server.accept() {*
* autoreleasepool {*
* client.send(request(client.read()))*
* client.close()*
* }*
*}*

The algorithm that i send is a example of server code and this code will
execute in a ubuntu environment. When i install in the ubuntu, the swiftc
show me that the *autoreleasepool *is "use of unresolved identifier
'autoreleasepool'". I want to clean the memory all the time when a client
close. I will rewrite the example replacing the client for socket:

func request(content) { ... }

let server = myserver()
while let *socket* = server.accept() {
  *autoreleasepool* {
    *socket*.send(request(*socket*.read()))
    *socket*.close()
  }
}

···

2016-11-02 18:36 GMT-02:00 Joe Groff <jgroff@apple.com>:

On Nov 2, 2016, at 1:16 PM, Bernardo Breder via swift-users < > swift-users@swift.org> wrote:

Is `client` really getting autoreleased somewhere? autoreleasepool
shouldn't normally be necessary. The client will be released when you go
out of scope.

-Joe

--
Nome : Bernardo Breder
Product : Breder Language
Site : bernardobreder.com
Email : bernardobreder@gmail.com
Email : bbreder@tecgraf.puc-rio.br
Email : contato@bernardobreder.com
Graduação : UFF - Ciência da Computação
Mestrado : UFF - Ciência da Computação
Trabalho : TecGraf - PUC-RIO


(Jens Alfke) #18

You don’t need to on Ubuntu. Autorelease pools are only to clean up Objective-C object references. Those don’t exist when you’re not running on an Apple platform; you only have Swift objects.

That SO question you linked to is talking about code running on macOS or iOS. It doesn’t apply to other platforms.

—Jens

···

On Nov 2, 2016, at 1:42 PM, Bernardo Breder via swift-users <swift-users@swift.org> wrote:

The algorithm that i send is a example of server code and this code will execute in a ubuntu environment. When i install in the ubuntu, the swiftc show me that the autoreleasepool is "use of unresolved identifier 'autoreleasepool'". I want to clean the memory all the time when a client close.