Comments (7)
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.
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.
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.
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.
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.
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.
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)
- Scale to window maintaining aspect ratio, but Viewport won't center HOT 2
- Please help me to clarify the concepts of some specific terms HOT 2
- WebGPU ImGui::Image not accepts different texture formats HOT 3
- InputText CTRL+V paste fails if text is too long HOT 4
- for some reason when i try to change the color of my text and try to do pop style color it crashed? HOT 3
- Why does ImGui tables insert extra ID into the id path of the child widgets? HOT 1
- Emscripten build runtime error: Unable to preventDefault inside passive event listener HOT 5
- getting tightest possible horizontal spacing HOT 7
- #7595 turned into an issue related to fonts loading problem after upgrade (ImGui/SDL3/Vulkan)
- Color too dark custom checkbox color fill HOT 3
- No horizontal scrollbar in a window when calling SetNextWindowContentSize with size larger than the screen HOT 6
- Plugin architecture in imgui(C++)? HOT 4
- Misbehaving GUI when i through libigl to use imgui HOT 2
- DXGI warning with DX11 backend about `DXGI_SWAP_EFFECT_DISCARD` HOT 4
- Transparent issue HOT 3
- ImGui Dockspace not working when viewports are enabled HOT 11
- Tabbar containing dockspace HOT 8
- How to dock a window to the size programmatically wthout taking the whole space HOT 3
- Memory leak when rendering with DX11 in C++ HOT 13
- How to display the same texture with different scale modes? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from imgui.