MengeCrowdSim / Menge

The source code for the Menge crowd simulation framework
Apache License 2.0
139 stars 63 forks source link

Can't run navMesh more than once #158

Open MichelangeloDiamanti opened 3 years ago

MichelangeloDiamanti commented 3 years ago

Hello,

As mentioned in the title I am having problems running any project employing navMesh as a velocity component more than once in a row. Everything works the first time I run the project, but the second time it throws a read access violation exception. This is the case only for projects that use NavMesh as a velocity component, in fact I can run the 4square example (which uses a K/D tree) repeatedly without any issue. I recorded a short video to better explain the issue:

https://user-images.githubusercontent.com/6999714/135186388-4273ddfb-f8f8-4d7f-acab-b9dc0232e11a.mp4

I initially discovered this issue on the MengeUnity project, which loads menge as an external DLL. In this case, the application loads and initializes the simulator, and then should be able to call the simulator more than once on any example (at least once every time the "play button" is pressed). So, when I tried running the navMesh example with Unity, it crashed the editor every other time. Then I traced back the issue to the main Menge application, and in the video you can see that it throws the read access violation. I tried to investigate the issue further, and I believe it is tied to the fact that the navMesh is never "cleaned up". In fact, putting some breakpoint in the Destructor of the navMesh class, I determined that those are never hit during execution, and the "Clear()" function is never called.

image

If that is the case, I think that after the first run, the simulator might be left in an inconsistent state, and that raises the exception. That would also explain why this happens only when the navMesh is involved. In fact, the K/D tree example works as intended, the destuctor gets called, and the simulation can be run multiple times. I would appreciate any help on this matter, thanks :)

MengeCrowdSim commented 3 years ago

First, a thousand apologies for taking so long to get back to you on this.

Second, I love the video you've created; you've done a remarkable job documenting the problem you're having. It should be very reproducible.

My initial (admittedly unhelpful) response is that the simulator certainly wasn't designed to do what you've shown. That said, I would hope that it would work anyways (more or less). While I reproduce and investigate locally, could you explain what you're trying to achieve? This specific example seems like you've encountered a problem and found the simplest mechanism you could come up with to reproduce it.

What is the real thing you're trying to accomplish that this is interfering with?

MengeCrowdSim commented 3 years ago

Never mind -- I re-read the original issue and see exactly what problem gave rise to this. I'm invesitgating.

MengeCrowdSim commented 3 years ago

OK Follow up. I've tried to reproduce the error you're getting. I've failed. I'm using Visual Studio Community 2017. By adding the loop that you've included in the video, I'm able to run the simulation several times in a row. It works for 4square.xml as well as navMesh.xml.

Looking at the video, I infer that you're using a different version of VS than I am. What version are you using (and what's the details of the compiler you're using)?

MichelangeloDiamanti commented 2 years ago

Hello Again, and sorry for the delayed response - I only recently got back into working with this.

Following your suggestion, I've tried to recompile Menge with Visual Studio Community 2017 and indeed things are working correctly now. But the issue is now moved to the MengeCS, and MengeCSExe. Basically I can run the navMesh.xml example multiple times if I modify the main loop in the c++ program (mengeMain.cpp), but I can't do the same in the C# version modifying the Program.cs. I get the same error that I was getting before, namely a read access violation exception.

I've tried configuring the .csproj files to target the same platform (x64) both in the c++ version and in the C# one, as noted in the MengeCS repository README - but that didn't help.

I'm not really sure how to debug this. Do you have any pointers?

Also, do let me know if it's best to close this issue and open a new one under the CS repository.

Thanks for the help :)

MichelangeloDiamanti commented 2 years ago

Ok so, after some more debugging I think I have made some progress. What I did was trying to find some differences between the native c++ menge application mengeMain.cpp and the exported functions in the menge_c_api.cpp. I've done this because I suspected that the dll was being used differently in the two cases, since I was now getting the error only on the C# program.

What I've noticed is that, in the InitSimulator function of menge_c_api.cpp, the simulator gets explicitly deleted at every run of the program (if it's not null), before getting a new simulator. On the other hand, there is no delete statement anywhere (that I could find) in the mengeMain.cpp.

image The image is a snippet from menge_c_api.cpp

I have confirmed this by placing a breakpoint in the destructor of the SimulatorInterface class. When I start debugging from MengeCS (Program.cs) the breakpoint is triggered, while when I debug from mengeMain.cpp it is not triggered.

image The image is a snippet from SimulatorInterface.cpp

Noting this discrepancy, I have removed the explicit delete statement in the InitSimulator function inside menge_c_api.cpp and now it seems to be working. By that I mean that I can run the navMesh example on MengeCS multiple times without crashes/exceptions. I have also tried it with MengeUnity, and it works there as well: the editor does not crash anymore when I play the scenes multiple times - so I believe that was the error.

Nevertheless, I'm not sure about the implications of deleting the call to the destructor during the initialization, and it's still not clear to me why this only happened when the navMesh was involved. I would appreciate any feedback you could give.

Thanks :)