Skip to content
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

Use destructors for automaticaly closing of resources #13

Open
planetis-m opened this issue Jan 2, 2020 · 8 comments
Open

Use destructors for automaticaly closing of resources #13

planetis-m opened this issue Jan 2, 2020 · 8 comments

Comments

@planetis-m
Copy link

planetis-m commented Jan 2, 2020

in order to do these are the steps that need to be taken:

  1. types and procs interfacing directly with libcairo need to keep their original names
    example type example proc

  2. The "higher level" user api of this library will get shorter names like Surface, imageSurfaceCreate.

  3. destructors will be attached to these types. it will look like this:

type
  Surface = object
    data: PSurface

`=destroy`(surface: var Surface)
`=`(dst: var Surface, src: Surface) {.error.}

These changes will break the api of this library but will allow the user to forget the closing of resources.

@Araq
Copy link
Member

Araq commented Jan 2, 2020

I like it but maybe it should be under nim-lang/cairo2?

@treeform
Copy link
Contributor

treeform commented Jan 3, 2020

This would not be a big win for me. Usually one creates 1 surface for the entire app. Its not hard to destroy it at the end of your program ... or event just forget and quit and have the OS do it for you.

I never had a problem with leaking Surfaces

@Araq
Copy link
Member

Araq commented Jan 3, 2020

@treeform But are surfaces the only exposed resource? The surface is only an example here.

@treeform
Copy link
Contributor

treeform commented Jan 3, 2020

@Araq Here are all of the destroys in the library:

proc destroy*(cr: ptr Context)
proc destroy*(options: ptr FontOptions)
proc destroy*(font_face: ptr FontFace)
proc destroy*(scaled_font: ptr ScaledFont)
proc destroy*(path: ptr Path)
proc destroy*(surface: ptr Surface)
proc destroy*(pattern: ptr Pattern)

I never had an issue with any of them, so I feel like it will not impact me in any way. It might be a cool thing to do, even if it's not a useful thing to do.

Will the =destroy only work with --gc:arc?

@planetis-m
Copy link
Author

planetis-m commented Jan 3, 2020

I made a minimal example. Maybe this is more correct, because cairo_reference increments the ref count and cairo_destroy dec its, and iff it's equal to zero then deallocs the memory?

type
  Context* = object
    impl: PContext

proc `=`(cr: var Context, original: Context) =
  if cr.impl != nil: cairo_destroy(cr.impl)
  cr.impl = cairo_reference(original.impl)
proc `=destroy`(cr: var Context) =
  if cr.impl != nil:
    cairo_destroy(cr.impl)
    cr.impl = nil
proc `=sink`(cr: var Context; original: Context) =
  `=destroy`(cr)
  cr.impl = original.impl

@Araq
Copy link
Member

Araq commented Jan 4, 2020

Will the =destroy only work with --gc:arc?

No, it's always active, however the old seq implementation (and stuff building on top like tables) ignores destructors.

@planetis-m
Copy link
Author

Fork exists at https://github.com/b3liever/cairo2 @treeform try it if you like

@StefanSalewski
Copy link

Don't forget that we have some sort of high level cairo in gintro for some years now. I have not spent too much effort in it yet, as it does not support introspection and toggle_references, so it is different from the other gtk stuff. But it seems to work with ARC now. I don't accept pull requests, but I may accepts issues or suggestions like the "=" proc to create errors. May add it soon, also for GTK widgets and that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants