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

•  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

--
•  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
--
•  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)

--
•  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 Overview
 group golang-nuts categories go posted Oct 1, '12 at 4:04p active Oct 1, '12 at 4:35p posts 5 users 3 website golang.org

### 3 users in discussion

Content

People

Support

Translate

site design / logo © 2021 Grokbase