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

#75 destroy hooks #81

Merged
merged 20 commits into from
Sep 16, 2023
Merged

Conversation

PhilippMDoerner
Copy link
Contributor

This is a branch containing 2 approaches to the entire destructor semantics:

  1. (First commit) Use a value object to wrap GdkPixBuf called PixBuf
  2. (Second commit) use a type alias to GdkPixBuf called PixBuf directly without any value objects.

To go back to the first attempt, just revert the second commit.

PhilippMDoerner and others added 5 commits September 13, 2023 19:29
This is a version of using destructors where PixBuf
is not a value object, but the pointer to GdkPixbuf directly.
You can simply revert this commit to return to the prior version
where PixBuf is a value object wrapping GdkPixBuf.
That version is known to work.
@can-lehmann
Copy link
Owner

Adding a =sink hook fixes it for me. I have no idea why though. I plan to investigate further when I have some more time.

We wrap the gdk pointer in a ref object instead of an object.

PixbufObj becomes an intermediate value-object and Pixbuf
an alias to the ref-object version of PixbufObj.
By adding the path flag to the nim compilation command
we can ensure that the owlkettle version used to compile
the examples is the **local** version, not one globally installed.
@PhilippMDoerner
Copy link
Contributor Author

PhilippMDoerner commented Sep 16, 2023

@can-lehmann We're running into the scenario that to support 1.6 we need destructors of type proc =destroy(pixbuf: var PixbufObj) = while for destructors of nim 2.0 that is deprecated.

To achieve 1.6. compatibility all it takes is change the signatures:
proc =destroy(pixbuf: var PixbufObj) =
proc =destroy(buffer: var TextBufferObj) =

My question would be whether the plan is for owlkettle 3.0 to support nim 1.6 still or not.
We can either:

  1. Ditch nim 1.6., have the destructors go with the recommended signature of proc =destroy(pixbuf: PixbufObj) =
  2. Ignore deprecation warnings in nim 2.0, have the destructors go with the deprecated signature of proc =destroy(pixbuf: var PixbufObj) =
  3. Satisfy both by using a when switch, which is slightly more maintenance and only makes sense if owlkettle 3.0 in total is intended to do so. (I will move forward with 3 for now, but I can easily revert the commit so that doesn't mean we have to stick with it)

The third approach looks like this:

when(NimMajor >= 2):
  proc `=destroy`(pixbuf: PixbufObj) =
    if isNil(pixbuf.gdk):
      return
    
    g_object_unref(pointer(pixbuf.gdk))
else:
  proc `=destroy`(pixbuf: var PixbufObj) =
    if isNil(pixbuf.gdk):
      return
    
    g_object_unref(pointer(pixbuf.gdk))
----
when(NimMajor >= 2):
  proc `=destroy`(buffer: TextBufferObj) =
    g_object_unref(pointer(buffer.gtk))
else:
  proc `=destroy`(buffer: var TextBufferObj) =
    g_object_unref(pointer(buffer.gtk))

Which path do we want to go down?

Regarding destructors for #76 I'll look into it.

This is a measure for better support of both nim 1.6. and nim 2.0
2.0 no longer supports lock-levels where they are deprecated.
So the locks:0 pragma is also deprecated and should be removed.
Nim 1.6. meanwhile will spit out warnings over undefined lock levels
if the examples are compiled without the pragma.

Thus customPragmas was created to define a `locker` pragma.
That pragma does nothing on nim2.0 and contains the locks:0 pragma
on nim 1.6., satisfying both versions and limiting warning noise.
proc `=destroy`(x: T) =

Nim 1.6 desires its destructor hooks to be defined as
proc `=destroy`(x: var T) =

That is deprecated in nim 2.0.
To satisfy both, the new destructor hooks were put behind
a when statement
@can-lehmann
Copy link
Owner

I think we can move forward with 3. for now. You could use a template to reduce code duplication (put that into common.nim as well).

@PhilippMDoerner
Copy link
Contributor Author

PhilippMDoerner commented Sep 16, 2023

I think we can move forward with 3. for now. You could use a template to reduce code duplication (put that into common.nim as well).

I'm actually somewhat torn here.
Because as far as I can see judge freeing the memory for a Pixbuf is a different task than freeing the memory of a TextBuffer is a different task than freeing the memory of a Cstring.
At least for now that's what it looks like to me.
Given that they don't seem inherently coupled to me and it just so happens that their destructors look similar, I hesitate to start coupling some of them together (because cstrings will most likely need their own strategy) when they should be different things.

Feel free to overrule me here and I'll still implement it, this is just my current instinct/gut-feeling from my very limited experience with owlkettle and GTK.

@can-lehmann
Copy link
Owner

can-lehmann commented Sep 16, 2023

I agree, I would do something like:

template destructor(name, typ, body: untyped) =
  when NimMajor >= 2:
    proc `=destroy`(name: typ) =
      body
  else:
    proc `=destroy`(name: var typ) =
      body

destructor(pixbuf, Pixbuf):
  ...

owlkettle/widgets.nim Outdated Show resolved Hide resolved
owlkettle/widgets.nim Outdated Show resolved Hide resolved
owlkettle/widgets.nim Outdated Show resolved Hide resolved
owlkettle/widgets.nim Outdated Show resolved Hide resolved
@PhilippMDoerner
Copy link
Contributor Author

Applied the template to TextBuf, PixBuf and Stylesheet and streamlined the two creation procs for textbuf and pixbuf as suggested.

@can-lehmann
Copy link
Owner

Are the =copy hooks still required? They are currently applied inconsistently.

owlkettle/mainloop.nim Outdated Show resolved Hide resolved
@PhilippMDoerner
Copy link
Contributor Author

Are the =copy hooks still required? They are currently applied inconsistently.

Logically speaking they should be required, because a copy means the ref count for gtk goes up by one and they must be informed of that so that they can handle memory management.
I swear I wrote a copy-hook for textbuf at one point, must've gotten deleted over the course of the PR.

@can-lehmann
Copy link
Owner

can-lehmann commented Sep 16, 2023

Since it is a ref object though, there should not be any copys (only if you do pixbuf1[] = pixbuf2[]).
So I think it is up to you if you want to support that, I just think it should be consistent.

@PhilippMDoerner
Copy link
Contributor Author

Which is a fair point but in that case I'd still want to have the copy-hook, just for "correctness" even if nobody should ever do that.
So yeah, give me a sec I'll provide a copy hook

@can-lehmann can-lehmann marked this pull request as ready for review September 16, 2023 13:37
@can-lehmann can-lehmann merged commit 996266b into can-lehmann:main Sep 16, 2023
3 checks passed
@PhilippMDoerner PhilippMDoerner deleted the #75-destroy-hooks branch September 16, 2023 13:46
@can-lehmann
Copy link
Owner

Looks good!

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

Successfully merging this pull request may close these issues.

2 participants