Email or username:

Password:

Forgot your password?
6 comments
prozacchiwawa

@abcdw you should close ports just to ensure they don't linger. some schemes have a library function that will wrap code and close the port automatically, so use it if available.

Ramin Honary

@abcdw As I read this documentation, my understand is that pipes are ordinary POSIX file descriptors, so you are probably responsible for closing them if you create them with the POSIX system calls.

However if you create a Scheme "port" object from a pipe, it seems like (from the manual) that the port object will automatically close the pipe when it is garbage collected:

>

> "A file descriptor can be extracted from a port and a new port can be created from a file descriptor. However a file descriptor is just an integer and the garbage collector doesn’t recognize it as a reference to the port. If all other references to the port were dropped, then it’s likely that the garbage collector would free the port, with the side-effect of closing the file descriptor prematurely.

> "To assist the programmer in avoiding this problem, each port has an associated revealed count which can be used to keep track of how many times the underlying file descriptor has been stored in other places. If a port’s revealed count is greater than zero, the file descriptor will not be closed when the port is garbage collected. A programmer can therefore ensure that the revealed count will be greater than zero if the file descriptor is needed elsewhere."

@abcdw As I read this documentation, my understand is that pipes are ordinary POSIX file descriptors, so you are probably responsible for closing them if you create them with the POSIX system calls.

However if you create a Scheme "port" object from a pipe, it seems like (from the manual) that the port object will automatically close the pipe when it is garbage collected:

Andrew Tropin

@ramin_hal9001 Oh, I missed those two paragraphs, thank you! :)

Felix Lechner

@abcdw Hi, there seem to be exceptions. "Ports should be closed when they are not needed .... Any error that occurs while writing out that buffered data would also be raised promptly at the close-port, and not later when the port is closed by the garbage collector." gnu.org/software/guile/manual/ "Buffered writes defer error detection (and defer the side effects to the mutable store), perhaps indefinitely if the port type does not need to be closed at GC." gnu.org/software/guile/manual/

@abcdw Hi, there seem to be exceptions. "Ports should be closed when they are not needed .... Any error that occurs while writing out that buffered data would also be raised promptly at the close-port, and not later when the port is closed by the garbage collector." gnu.org/software/guile/manual/ "Buffered writes defer error detection (and defer the side effects to the mutable store), perhaps indefinitely if the port type does not need to be closed at GC."

yourWife

@abcdw simply use call-with-input-file and Guile will close for you like RAII style

Andrew Tropin

@yourWife I'm not sure if call-with-input-file works with (pipe). Moreover, I'm almost sure it doesn't :)

Go Up