Git Product home page Git Product logo

Comments (5)

ghaberek avatar ghaberek commented on August 17, 2024 1

So the string has to remain valid unless sent directly to the platform? How do we know when/if?

Sorry, I see now that my response was a bit vague. Looking through the code, it seems that a copy is always made.

It's just that sometimes libui passes your string pointer directly to the platform so the platform makes its own copy.

I'm not the author so I could be wrong. It'd be worth doing a deeper-dive to ensure string copying is consistent.

from libui.

davidgiven avatar davidgiven commented on August 17, 2024

Also, when I pass a string into a function, does libui copy the string or is the pointer expected to remain valid while the string is in use?

from libui.

ghaberek avatar ghaberek commented on August 17, 2024

When I create a control and add it to a hierarchy, apparently the parent control takes ownership of the child and ensures that it gets cleaned up properly.

I think this is a combination of libui and the native platform. Controls are based on struct uiControl which includes a function that returns the parent of the control. Each platform has its own unique struct ui[Platform]Control that includes a pointer to the uiControl *parent object: struct uiDarwinControl, struct uiUnixControl, struct uiWindowsControl.

The function uiWindowSetChild() only allows for one child, which should be a container that can contain other children. If you call uiWindowSetChild() more than once, it will destroy the existing child and replace it with the new one. When the window is destroyed it will destroy and free its child if it exists.

Some controls are containers which can contain multiple children. These cases are handled in an implementation-specific way, but they all contain some array type of children and will destroy and free each child when the container is destroyed.

If I create a window and show it with uiControlShow(), it seems that libui takes ownership of the window. Maybe because it's been reparented to the conceptual screen?

uiControlShow() calls the control's specific "show" function stored in struct uiControl which for a window would be uiWindowShow() and for other controls is typically ui[Platform]ControlDefaultShow() which calls the platform-specific "show" function. But uiControlShow() doesn't have anything to do with ownership.

On any platform, a top-level window is owned by the system and the window cleans up itself when it's time to close the application. Part of that involves destroying the window's child which then kicks off a recursive series of child destruction as I described above.

What happens then if I call uiControlHide()? Is the window deparented and therefore mine again, or does libui still keep it? What happens if I then show it once more?

uiControlHide() calls the control's specific "hide" function stored in struct uiControl. For windows on some platforms there's a specific uiWindowHide() but otherwise it just calls the platform-specific "hide" function. But just like uiControlShow(), uiControlHide() doesn't have anything to do with ownership.

Also, when I pass a string into a function, does libui copy the string or is the pointer expected to remain valid while the string is in use?

This seems to be the case, except for instances where libui just passes the string directly to the platform, in which case the platform should be copying the string. Functions that return strings allocate memory for the result which needs to be freed with uiFreeText().

Hope this helps!

from libui.

simplexidev avatar simplexidev commented on August 17, 2024

Also, when I pass a string into a function, does libui copy the string or is the pointer expected to remain valid while the string is in use?

This seems to be the case, except for instances where libui just passes the string directly to the platform, in which case the platform should be copying the string. Functions that return strings allocate memory for the result which needs to be freed with uiFreeText().

So the string has to remain valid unless sent directly to the platform? How do we know when/if?

from libui.

simplexidev avatar simplexidev commented on August 17, 2024

Ahh, no problem for the confusion. Thanks for the clarification!

from libui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.