Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A simple tcp server doesn't work with -autofree #7841

Closed
medvednikov opened this issue Jan 3, 2021 · 2 comments
Closed

A simple tcp server doesn't work with -autofree #7841

medvednikov opened this issue Jan 3, 2021 · 2 comments
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Unit: Memory Management Bugs/feature requests, that are related to the memory management of the compiler.

Comments

@medvednikov
Copy link
Member

import net
import cli { Command, Flag }
import os

fn main() {
    mut cmd := Command{
        name: 'server'
        description: 'sErVeR'
        version: '0.0.1'
        execute: serve
        usage: '<name>'
    }
    cmd.add_flag(Flag{
        flag: .string
        required: false
        name: 'port'
        abbrev: 'P'
        value: '54123'
        description: 'Set the port the server is runing on. Standard is 54123.'
    })
    cmd.parse(os.args)
}

fn serve(cmd Command) {
    port := cmd.flags.get_string('port') or { panic(err) }
    println(port)
    listener := net.listen_tcp(port.int()) or { panic(err) }
    for {
        conn := listener.accept() or { panic(err) }
        go handle_conn(conn)
    }
}

fn handle_conn(conn net.TcpConn) {
    mut arr := []string{}
    for {
        received := conn.read_line().trim_space()
        if received == '' {
        } else {
            arr << received
            println(arr)
        }
    }
}
@medvednikov medvednikov added the Bug This tag is applied to issues which reports bugs. label Jan 3, 2021
@medvednikov medvednikov self-assigned this Jan 3, 2021
@danieldaeschle danieldaeschle added the Unit: Memory Management Bugs/feature requests, that are related to the memory management of the compiler. label Jan 8, 2021
@leftslash
Copy link

leftslash commented Feb 6, 2021

This one really hurts. Any advice on how to address this until the bug is fixed? unsafe { something.free() }?

@nm17
Copy link

nm17 commented Apr 30, 2022

@medvednikov this seems to be fixed in modern versions of V, So you can close it now.

--- orig-issue.v        2022-04-30 23:25:03.205320820 +0400
+++ new-code.v     2022-04-30 23:24:20.301956220 +0400
@@ -1,3 +1,5 @@
+module main
+
 import net
 import cli { Command, Flag }
 import os
@@ -15,23 +17,23 @@
         required: false
         name: 'port'
         abbrev: 'P'
-        value: '54123'
+        default_value: ['54123']
         description: 'Set the port the server is runing on. Standard is 54123.'
     })
     cmd.parse(os.args)
 }
 
-fn serve(cmd Command) {
+fn serve(cmd Command) ? {
     port := cmd.flags.get_string('port') or { panic(err) }
     println(port)
-    listener := net.listen_tcp(port.int()) or { panic(err) }
+    mut listener := net.listen_tcp(net.AddrFamily.ip, ":$port") or { panic(err) }
     for {
-        conn := listener.accept() or { panic(err) }
-        go handle_conn(conn)
+        mut conn := listener.accept() or { panic(err) }
+        go handle_conn(mut conn)
     }
 }
 
-fn handle_conn(conn net.TcpConn) {
+fn handle_conn(mut conn net.TcpConn) {
     mut arr := []string{}
     for {
         received := conn.read_line().trim_space()
```-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Unit: Memory Management Bugs/feature requests, that are related to the memory management of the compiler.
Projects
None yet
Development

No branches or pull requests

5 participants