On Wed, Feb 19, 2025 at 1:12 PM twp...@gmail.com <twpa...@gmail.com> wrote: > > The documentation for runtime.AddCleanup says: > > > There is no specified order in which cleanups will run. > > Given the following types: > > type Parent struct { > parentResource int > } > > type Child struct { > parent *Parent > childResource int > } > > and the following code: > > parentResource := 0 > parent := &Parent{ > parentResource: parentResource, > } > runtime.AddCleanup(parent, func (int) {}, parentResource) > childResource := 1 > child := &Child{ > parent: parent, > childResource: childResource > } > runtime.AddCleanup(child, func(int) {}, childResource) > > is it guaranteed that the cleanup for childResource will run before the > cleanup for parentResource?
No. There is no specified order in which cleanups will run. The fact that child points to parent doesn't really matter here. They can both be collected as garbage in the same GC pass, and that GC pass may schedule both cleanups. There is no guarantee as to which cleanup will be scheduler first or which will run first. Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcVYGPpa79WjPJwh9PCXcav9J%3Db-vSrhvOCU6zV-4ZbG_g%40mail.gmail.com.