SPHinXsys aims to follow the concept of resource acquisition is initialization (RAII) strictly. Therefore, the ownership of all objects have been clarified. Basically, there are three types of ownerships.
First, the SPHsystem, bodies, body relations, gravities and particle-dynamics methods are owned by the application, i.e. the main function. This requires that these objects are defined in the main function. Second, the particles and materials are owned by SPHbody; Third, though the geometries, are owned by SPHBody; the shapes owned by complex shape or body part during the simulation, these objects are temporally owned by base and derived constructors. Therefore, we need to use shared pointer for them.
One important choice in SPHinXsys is that the ownership is only clarified at the construction phase. After that, observers of raw pointers are assigned and used during the simulation.
上面是官网的说明。SPHinXsys严格遵循RAII原则。RAII(Resource Acquisition Is Initialization)的直观理解是:把资源的生命周期绑定到对象生命周期——在构造阶段“拿到/绑定资源”,在析构阶段“释放资源”。智能指针是RAII在内存管理上的经典体现。RAII的价值不只是“避免内存泄漏”,更重要的是:哪怕中途 return/异常抛出,析构仍然会发生,从而把资源回收路径变得可靠、可维护。