FAQ
Hi all,

I have a question about the time to call C.free() to release the memory
from C. Suppose we have the following codes:

type Node struct {
left *Node
right *Node
}

func A() *Node {
p := C.malloc(C.size_t(unsafe.Sizeof(Node{})))
defer C.free(p)
node := (*Node)p
return node
//Does C.free(p) will happen here?
}

func B() {
node := A()
// at this time, can I continue using "node"?
// When will the "C.free(p)" in func A() be executed, after func B()
finishes?
}

Thanks.

--

Search Discussions

  • Jan Mercl at Oct 1, 2012 at 4:09 pm

    On Mon, Oct 1, 2012 at 6:04 PM, Robert Sandra wrote:
    I have a question about the time to call C.free() to release the memory from
    C. Suppose we have the following codes:

    type Node struct {
    left *Node
    right *Node
    }

    func A() *Node {
    p := C.malloc(C.size_t(unsafe.Sizeof(Node{})))
    defer C.free(p)
    node := (*Node)p
    return node
    //Does C.free(p) will happen here?
    }

    func B() {
    node := A()
    // at this time, can I continue using "node"?
    // When will the "C.free(p)" in func A() be executed, after func B()
    finishes?
    }
    Between `return node` in A and actually continuing B's execution after
    `node := A()` in B was invoked. So the returned *Node is invalid
    *before* assigned to `node` in B.

    -j

    --
  • Robert Sandra at Oct 1, 2012 at 4:28 pm
    Thanks, So in this case when should I call C.free() to release the memory?
    Can I call C.free(node) in B? Is this the same as the C.free(p) in A? I
    mean these two will release the same memory?

    On Monday, October 1, 2012 12:09:32 PM UTC-4, Jan Mercl wrote:

    On Mon, Oct 1, 2012 at 6:04 PM, Robert Sandra
    <robert.s...@gmail.com <javascript:>> wrote:
    I have a question about the time to call C.free() to release the memory from
    C. Suppose we have the following codes:

    type Node struct {
    left *Node
    right *Node
    }

    func A() *Node {
    p := C.malloc(C.size_t(unsafe.Sizeof(Node{})))
    defer C.free(p)
    node := (*Node)p
    return node
    //Does C.free(p) will happen here?
    }

    func B() {
    node := A()
    // at this time, can I continue using "node"?
    // When will the "C.free(p)" in func A() be executed, after func B()
    finishes?
    }
    Between `return node` in A and actually continuing B's execution after
    `node := A()` in B was invoked. So the returned *Node is invalid
    *before* assigned to `node` in B.

    -j
    --
  • Minux at Oct 1, 2012 at 4:32 pm

    On Tue, Oct 2, 2012 at 12:28 AM, Robert Sandra wrote:

    Thanks, So in this case when should I call C.free() to release the memory?
    Can I call C.free(node) in B? Is this the same as the C.free(p) in A? I
    mean these two will release the same memory?
    Do what you would do if you were using C
    (that is, freeing after you've finished using that object, and free it only
    once)

    --
  • Jan Mercl at Oct 1, 2012 at 4:35 pm

    On Mon, Oct 1, 2012 at 6:28 PM, Robert Sandra wrote:
    Thanks, So in this case when should I call C.free() to release the memory?
    Ideally as soon as that mem is not needed (reachable) anymore.
    Can I call C.free(node) in B? Is this the same as the C.free(p) in A? I mean
    these two will release the same memory?
    That should be the case modulo the probably required type conversion
    to unsafe.Pointer before passing the value (of B's `node` variable) to
    C.free.

    -j

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 1, '12 at 4:04p
activeOct 1, '12 at 4:35p
posts5
users3
websitegolang.org

3 users in discussion

Jan Mercl: 2 posts Robert Sandra: 2 posts Minux: 1 post

People

Translate

site design / logo © 2021 Grokbase