Code Monkey home page Code Monkey logo

Comments (7)

BraunSilas avatar BraunSilas commented on May 27, 2024

Lol, sry for the Spam. I figured out that I was making mistakes regarding pointer and scope.
I had to give startDragSRnewUniversal a string pointer into the function, because otherwise the pointer given into setDragDropPayload() would point to unused memory once the function is exited and the string is released;
I think something like that is happening.

from imgui.

BraunSilas avatar BraunSilas commented on May 27, 2024

Hey Sry for switching this Issue on again, but I am really confused. Tried some more and compared the code above with the code I use somewhere else where the payload is an int.
when I applied same same method I used for ints it worked except that the string that is received is only 3 char long
The funny thing is that the code looks the exact same to the way it was before but the behavior is different.
the only idea I have is that the Memory of the pointer is being corrupted during runtime or the payload is not receiving the right pointer to the string?

from imgui.

cfillion avatar cfillion commented on May 27, 2024

SetDragDropPayload copies the given amount of bytes. It doesn't construct a full copy of the object. Just its raw data. When startDragSRnewUniversal returns and the local SRType is destroyed, its internal string buffer is freed. It's long gone by the time endDragSRnewUniversal happens.

You probably want to store a copy the string's data (not the string object) instead.

ImGui::SetDragDropPayload("DummySR", SRType.c_str(), SRType.size() + 1); // include the null terminator
auto SRType = static_cast<const char *>(payload->Data);

(Or if you know that whichever string is given to startDragSRnewUniversal will live long enough, and really want to have it in the payload rather than its current contents, pass it by pointer instead of by value. If so then you can do ImGui::SetDragDropPayload("DummySR", &SRType, sizeof(SRType)); and static_cast<string**>(payload->Data).)

from imgui.

GamingMinds-DanielC avatar GamingMinds-DanielC commented on May 27, 2024

Since std::string is a container that manages its own memory, you can't just copy it like a POD (plain old data) structure. At least it is quite dangerous. If you manipulate the original string in any way, it could reallocate and invalidate the memory pointed to in your copy. A safer way would be to transport the string contents, you can get them with string::data() and string::size() (number of characters, not bytes if you use f.e. wide chars).

from imgui.

BraunSilas avatar BraunSilas commented on May 27, 2024

Thanks guys, I made it work with @cfillion 's approach.
to reflect on this:
I understood that the 2nd argument for setDragDropPayload() copies the value given in.
so Copying the string (or pointer to it) declared in the function didn't work because its got freed at the end,
copying a string or pointer (declared outside)that is declared outside is unreliable because it can change in size or position(because it isn't a POD
but copying a string** works ?
or are strings just not compatible with the payload functionality?

from imgui.

GamingMinds-DanielC avatar GamingMinds-DanielC commented on May 27, 2024

but copying a string** works ?

The example from cfillion doesn't copy a string**, but a string* (the contents pointed to by the **). This works if done correctly, but you have to be sure of the lifetime of the underlying string. Since you transport a pointer to the string and not its content, changed content before a drop will be reflected in the drop.

or are strings just not compatible with the payload functionality?

Transporting the string as if it were a POD is theoretically possible too, but with higher restrictions. You are not allowed to alter the string before the drop, otherwise you risk invalid memory accesses, even if the string you copied from is still alive.

The best way would be to just copy neither the string object nor its address, but the contents of the string object (the continuous chars) as said.

from imgui.

ocornut avatar ocornut commented on May 27, 2024

Thanks guys, I made it work with @cfillion 's approach. to reflect on this: I understood that the 2nd argument for setDragDropPayload() copies the value given in. so Copying the string (or pointer to it) declared in the function didn't work because its got freed at the end, copying a string or pointer (declared outside)that is declared outside is unreliable because it can change in size or position(because it isn't a POD but copying a string** works ? or are strings just not compatible with the payload functionality?

Your wording makes things incredibly ambiguous and difficult to grasp.

One way to think about it is:

  • You want your payload to carry the individual characters composing the string.
  • If it is a zero-terminated string (with std::string are) then implicitly an extra 0 character at the end is enough to tell what the length of the string is.
  • Copying that data is the simplest thing to do.

What you initially tried was:

  • Your payload was containing the contents of the std::string structure, it is in theory an opaque structure, and contains at least a pointer owned by the std::string instance. There is no point in copying copy this pointer especially as the underlying character data may move to be deleted.

So just copy the character data here, and you can perfectly recreate a std::string from the payload afterwards.

from imgui.

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.