Garbage Collector triggering when deleting an object

233 views
Skip to first unread message

Sean

unread,
Nov 22, 2020, 7:13:22 AM11/22/20
to golang-nuts
I am writing a Golang wrapper for OpenAL-Soft.
I need to create some buffers. These operations are happening on the
OpenAL-Soft side (So C functions). I am loading PCM data into buffers.

Can I set up a trigger mechanism like "x.__del__" as in Python?

I can set the garbage collector to follow some objects.
But I have to call some C functions (alDeleteBuffers) to delete buffers
in OpenAL.

I want garbage collector to run this function while deleting the object.

Is this possible?

Robert Engels

unread,
Nov 22, 2020, 8:05:11 AM11/22/20
to Sean, golang-nuts
Read about Go finalizers.

> On Nov 22, 2020, at 6:13 AM, Sean <s.tolst...@gmail.com> wrote:
>
> I am writing a Golang wrapper for OpenAL-Soft.
> --
> 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...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/1bbb276a-e090-7cb9-5a92-ae69dd06b67c%40gmail.com.

tokers

unread,
Nov 22, 2020, 10:17:18 PM11/22/20
to golang-nuts
There is a runtime.SetFinalizer function.

Tamás Gulácsi

unread,
Nov 23, 2020, 6:28:31 AM11/23/20
to golang-nuts
There is a runtime.SetFinalizer, but it is not guaranteed to be run, anytime.
The most idiomatic and safe solution is to have a Close() method on the Go side, which frees the C side.
You may even add a Finalizer that panics if Close haven't been called before...

tokers

unread,
Nov 23, 2020, 6:31:50 AM11/23/20
to golang-nuts
Are there any documents?

Robert Engels

unread,
Nov 23, 2020, 8:48:19 AM11/23/20
to tokers, golang-nuts
Finalizers are essential in a highly concurrent system - otherwise you are doing manual resource management with automatic memory management (a resource) and its ugly and error prone. 

Even though they are no guaranteed to be called they work very well to manage shared resources. 

On Nov 23, 2020, at 5:32 AM, tokers <zcha...@gmail.com> wrote:

Are there any documents?
--
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...@googlegroups.com.

jake...@gmail.com

unread,
Nov 24, 2020, 12:06:31 PM11/24/20
to golang-nuts
On Monday, November 23, 2020 at 6:31:50 AM UTC-5 tokers wrote:
Are there any documents?
 

Tyler Compton

unread,
Nov 24, 2020, 12:13:54 PM11/24/20
to tokers, golang-nuts
If you're using finalizers to clean up C memory, it's important to know that the Go garbage collector has no understanding of how much C memory a given resource holds. This prevents the garbage collector from making smart decisions about when to release your resources. I've never personally seen this become an issue, but it's something to keep in mind.

Reply all
Reply to author
Forward
0 new messages