c++ 来自Chrono的例外

2ic8powd  于 2023-08-09  发布在  其他
关注(0)|答案(1)|浏览(124)

我是新的项目计时,并试图运行教程,但陷入了第一个。代码如下:

  1. #include "chrono/physics/ChLinkMotorRotationSpeed.h"
  2. #include "chrono/physics/ChSystemNSC.h"
  3. using namespace chrono;
  4. int main(int argc, char* argv[]) {
  5. GetLog() << "Copyright (c) 2017 projectchrono.org\nChrono version: " << CHRONO_VERSION << "\n\n";
  6. {
  7. //
  8. // EXAMPLE 1:
  9. //
  10. GetLog() << " Example: create a physical system.. \n";
  11. // The physical system: it contains all physical objects.
  12. ChSystemNSC sys;
  13. // Create a bunch of rigid bodies..
  14. // Note that we use shared pointers, so you don't
  15. // have to care about the deletion (never use delete.. for
  16. // objects managed with shared pointers! it will be automatic!)
  17. auto my_body_A = chrono_types::make_shared<ChBody>();
  18. auto my_body_B = chrono_types::make_shared<ChBody>();
  19. auto my_body_C = chrono_types::make_shared<ChBody>();
  20. // Create some markers..
  21. // Markers are 'auxiliary coordinate systems' to be added
  22. // to rigid bodies.
  23. // Again, note that they are managed by shared pointers.
  24. auto my_marker_a1 = chrono_types::make_shared<ChMarker>();
  25. auto my_marker_a2 = chrono_types::make_shared<ChMarker>();
  26. auto my_marker_b1 = chrono_types::make_shared<ChMarker>();
  27. auto my_marker_b2 = chrono_types::make_shared<ChMarker>();
  28. // You can create some forces too...
  29. auto my_force_a1 = chrono_types::make_shared<ChForce>();
  30. auto my_force_a2 = chrono_types::make_shared<ChForce>();
  31. // Here you will add forces and markers to rigid
  32. // bodies.
  33. // Note: the same marker shouldn't be added to multiple bodies.
  34. my_body_A->AddMarker(my_marker_a1);
  35. my_body_A->AddMarker(my_marker_a2);
  36. my_body_A->AddForce(my_force_a1);
  37. my_body_A->AddForce(my_force_a2);
  38. my_body_B->AddMarker(my_marker_b1);
  39. my_body_B->AddMarker(my_marker_b2);
  40. // Ok, remember that rigid bodies must be added to
  41. // the physical system.
  42. sys.AddBody(my_body_A);
  43. sys.AddBody(my_body_B);
  44. sys.AddBody(my_body_C);
  45. // Show the hierarchy in the shell window...
  46. GetLog() << "Here's the system hierarchy which you built: \n\n ";
  47. sys.ShowHierarchy(GetLog());
  48. // Do you want to remove items? Use the
  49. // Remove...() functions.
  50. my_body_A->RemoveAllForces();
  51. // Remove a single body..
  52. sys.RemoveBody(my_body_A);
  53. // Add markers to another body...
  54. my_body_B->AddMarker(my_marker_a1);
  55. my_body_B->AddMarker(my_marker_a2);
  56. my_body_B->AddForce(my_force_a1);
  57. my_body_B->AddForce(my_force_a2);
  58. // By the way, you can set an Ascii name for objects as desired:
  59. my_marker_a1->SetName("JohnFoo");
  60. // ..so you can later use my_body_B.SearchMarker("JohnFoo"); etc.
  61. GetLog() << "\n\n\nHere's the system hierarchy after modifications: \n\n ";
  62. sys.ShowHierarchy(GetLog());
  63. }
  64. return 0;
  65. }

字符串
我添加了所有需要的模块,库和dll到项目中,所以现在我没有得到与它们相关的错误消息,但得到了一个异常,我现在不知道如何处理。此异常仅在代码完成时且在“return 0”之前发生。我的想法是,异常的发生是因为共享指针被删除了两次,但我不确定是不是这样。
下面是我在Visual Studio的'memory'文件中得到的异常消息:

  • 在ConsoleApplication7.exe中的0x00007FFF6E0ECA3E(ChronoEngine.dll)处引发异常:0xC0000005:访问冲突写入位置0x00000185243FCA0F*

内存文件中抛出异常的部分如下:

  1. void _Destroy() noexcept override { // destroy managed resource
  2. delete _Ptr;
  3. }

gc0ot86w

gc0ot86w1#

我认为问题是在您删除后标记仍然链接到my_body_A。当您尝试将其添加到my_body_B时,此链接会导致异常。
您应该创建新标记以添加到my_body_B

  1. my_body_A->AddMarker(my_marker_a1);
  2. my_body_A->AddMarker(my_marker_a2);
  3. sys.AddBody(my_body_A);
  4. sys.AddBody(my_body_B);
  5. sys.AddBody(my_body_C);
  6. sys.RemoveBody(my_body_A); //Here, you delete it as usual
  7. //And here you create new marker for my_body_B
  8. auto new_marker_a1 = chrono_types::make_shared<ChMarker>();
  9. auto new_marker_a2 = chrono_types::make_shared<ChMarker>();
  10. my_body_B->AddMarker(new_marker_a1);
  11. my_body_B->AddMarker(new_marker_a2);
  12. my_body_B->AddForce(my_force_a1);
  13. my_body_B->AddForce(my_force_a2);

字符串

展开查看全部

相关问题