Skip to content

It is a GC issue? #44317

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

Closed
Walnux opened this issue Feb 17, 2021 · 1 comment
Closed

It is a GC issue? #44317

Walnux opened this issue Feb 17, 2021 · 1 comment

Comments

@Walnux
Copy link

Walnux commented Feb 17, 2021

Meet a weird issue, someone can have a look at it. Thanks!

What version of Go are you using (go version)?

$ go version
go version go1.16 linux/amd64

Does this issue reproduce with the latest release?

go1.16 is the latest stable version I can get now

What operating system and processor architecture are you using (go env)?

$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/martin/.cache/go-build"
GOENV="/home/martin/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/martin/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/martin/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build901155952=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Please see the code below

package main
import "fmt"

func getSubset(nums, combine []int, re *[][]int){
    *re = append(*re, combine)
    fmt.Println(*re)
    if len(combine) > 0 {
            fmt.Println(&combine[len(combine) - 1])
    }

    if len(nums) == 0 {
        return
    }

    for i, _ := range nums {
            getSubset(nums[i + 1:], append(combine, nums[i]), re)
    }

    return
}

func subsets(nums []int) [][]int {
    var re [][]int
    getSubset(nums, []int{}, &re)
    return re
}

func main() {
        re := subsets([]int{1,2,3,4,5})
        fmt.Println(re)
}

What did you expect to see?

[[]]
[[] [1]]
0xc0000140d0
[[] [1] [1 2]]
0xc0000140f8
[[] [1] [1 2] [1 2 3]]
0xc00001c130
[[] [1] [1 2] [1 2 3] [1 2 3 4]]
0xc00001c138
[[] [1] [1 2] [1 2 3] [1 2 3 4] [1 2 3 4 5]]
0xc00001e0e0
[[] [1] [1 2] [1 2 3] [1 2 3 4] [1 2 3 4 5] [1 2 3 5]]
0xc0000????  any memory space which is not used
...

What did you see instead?

[[]]
[[] [1]]
0xc0000140d0
[[] [1] [1 2]]
0xc0000140f8
[[] [1] [1 2] [1 2 3]]
0xc00001c130
[[] [1] [1 2] [1 2 3] [1 2 3 4]]
0xc00001c138
this address is used to save the last item of slice []int{1,2,3,4}. Since this slice is appended to *re which will be accessed later, according to my understanding address 0xc00001c138 should not be released and used by others before *re is released.

[[] [1] [1 2] [1 2 3] [1 2 3 4] [1 2 3 4 5]]
0xc00001e0e0
[[] [1] [1 2] [1 2 3] [1 2 3 5] [1 2 3 4 5] [1 2 3 5]]
0xc00001c138

but looks like here 0xc00001c138 is released and used to save the last items of slice[]int{1,2,3,5}.
it can be prvoed that the last item of slice []int{1,2,3,4} is changed from 4 to 5
@Walnux Walnux changed the title A potential GC issue A potential GC issue? Feb 17, 2021
@Walnux Walnux changed the title A potential GC issue? It is a GC issue? Feb 17, 2021
@ianlancetaylor
Copy link
Contributor

Please see https://blog.golang.org/slices.

@golang golang locked and limited conversation to collaborators Feb 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants