-
Notifications
You must be signed in to change notification settings - Fork 28
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
Arrays of pointers to abstract classes (sometimes) cause segfault in VariableServer when accessing std::string class members #1794
Comments
@M-Herr Thank you for the thorough information and for demonstrating the test. We’ll be looking into this further. |
fake news! |
j/k lol |
I do remember you brought up a similar issue before, lol |
the issue i ran into that i believe could be the root of the problem is that the resize call ends up doing a memcpy which doesn't work for std::string's because the original string instance will deallocate the underlying char * when it is destroyed. |
That sounds very reasonable. However, when the first event is added, the resize function isn't called, and memory is allocated for 100 events right away. The resize function won't be triggered until 100 events have been added. I recall this issue was there when testing with just a single event. I may have misunderstood your point though. Will test a bit more. Thanks! |
also, that event allocation example is for allocating an array of pointers to events. not the events themselves. so something else is happening for the event case i would think |
Was wondering about the same thing and did try to make event object with str initialized as empty string. The issue seemed still there. But I did it very quick and never got back to it. Could be that the test wasn't done properly |
If you allocate an array with an abstract class, the Memory Manager will allocate the memory like this:
address = calloc( (size_t)n_elems, (size_t)size
(Line 146 in MemoryManager_declare_var.cpp)
The memory is then returned to the caller uninitialized. That is all fine. The issue present itself when someone tries to access uninitialized elements of the array through the trick variable server.
For example, in
EventManager::add_to_active_events(Trick::Event * in_event)
if the number of active events is 1 trick will allocate an initial array withactive_events = (Trick::Event **)TMM_declare_var_s("Trick::Event* [100]");
. The Event Manager then assigns the new event to the correct index and returns. If the event is store at index 1, and you try to access the 'name' variable in Trick::Event at index 2 - the sim crashes (segfault).Line 327 in VariableReference.cpp
Possible solution:
I haven't tested this extensively, but here's an excerpt from the add_to_active_events function:
Setting uninitialized memory to null outside of the if statements seems to fix the crash.
I don't know if this is the "right" solution, but thought I'd include it as it at least stops the crash from happening.
Fun and interesting behaviors
Steps to reproduce:
The last one is a little strange as well. Sometimes it works. From some experimenting, if the garbage in memory happens to translate to an empty string everything works okay. But if it doesn't, then the segmentation fault occurs.
The text was updated successfully, but these errors were encountered: