Packaging solution: (ana)conda

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
wmayer
Founder
Posts: 20317
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Packaging solution: (ana)conda

Post by wmayer »

The problem starts because netgen::OCCGenerateMesh has changed its interface where the second parameter is now a shared_ptr<Mesh> instead of Mesh*. This affects the code throughout the Netgen plugin of smesh.

Important to know when working with shared_ptr is that once it holds an object you must adjust the interface of functions to replace a Mesh* with shared_ptr<Mesh> too when it internally expects a shared_ptr because otherwise you confuse the reference counter and get a dangling pointer in the calling instance. But this will likely cause a crash and not an empty mesh.

Example:

Code: Select all


class Mesh
{
public:
    Mesh()
    {
        i=0;
    }
    ~Mesh()
    {

    }
    void func()
    {
        i++;
    }
private:
    int i;
};

void useMesh(Mesh* p)
{
    std::shared_ptr<Mesh> u(p);
    u->func();
} // <<== here the Test object will be destroyed

int main()
{
    std::shared_ptr<Mesh> t(new Mesh);
    Mesh*p = t.get();
    useMesh(p);
    Mesh*q = t.get(); // <<== t holds a dangling pointer now
    q->func();
}
So, in the smesh netgen code there are a few classes that internally only has a Mesh* and this must also be replaced with a shared_ptr<Mesh>.

Last weekend I was also working to improve the Netgen cmake checks and therefore had to try netgen v6. I made the same experience that the mesh is empty and stumbled across the same issue as you. For testing purposes I added an "exit(0);" at this code bloc but FreeCAD didn't terminate. So, this means the code part is never executed and there must be other reasons for the empty mesh.
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: Packaging solution: (ana)conda

Post by looo »

Thanks for the information.

Regarding the shared pointers: there is also a way to suppress the deletion of the pointer...:
http://stackoverflow.com/questions/2013 ... all-delete
http://de.cppreference.com/w/cpp/memory ... shared_ptr

Code: Select all

template< class Y, class Deleter >
shared_ptr( Y* ptr, Deleter d );
Wouldn't this work in this case?
wmayer wrote:Last weekend I was also working to improve the Netgen cmake checks and therefore had to try netgen v6. I made the same experience that the mesh is empty and stumbled across the same issue as you. For testing purposes I added an "exit(0);" at this code bloc but FreeCAD didn't terminate. So, this means the code part is never executed and there must be other reasons for the empty mesh.
sounds not good. Have you done this on all the cases? There are more then one implementation of NETGENPlugin...::Compute.
wmayer
Founder
Posts: 20317
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Packaging solution: (ana)conda

Post by wmayer »

Wouldn't this work in this case?
Yes, this should do the trick.
sounds not good. Have you done this on all the cases? There are more then one implementation of NETGENPlugin...::Compute.
No. I started to change the interface of NETGENPlugin_NETGEN_3D::compute but then had to change more and more. Since my main goal was to improve the cmake netgen check I reverted this part. Here you can see the changes I made so far: https://github.com/wwmayer/FreeCAD/comm ... bb7ac29481
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: Packaging solution: (ana)conda

Post by looo »

A backtrace from creating a mesh with mesh-design workbench and netgen6:

Code: Select all

#0  0x00007fff579f3b7b in netgen::OCCGenerateMesh (geom=..., mesh=..., mparam=..., perfstepsstart=1, perfstepsend=1)
   from /home/lo/anaconda/envs/fc_2/lib/././libocc.so
#1  0x00007fff5de1dd17 in NETGENPlugin_Mesher::Compute (this=0x7fffffff9ee0) from /home/lo/anaconda/envs/fc_2/lib/./libNETGENPlugin.so
#2  0x00007fff5de52030 in NETGENPlugin_NETGEN_2D::Compute (this=0x2cbe1e0, aMesh=..., aShape=...)
   from /home/lo/anaconda/envs/fc_2/lib/./libNETGENPlugin.so
#3  0x00007fff5d0c7d2e in SMESH_subMesh::ComputeStateEngine (this=0x2cbe6c0, event=1) from /home/lo/anaconda/envs/fc_2/lib/./libSMESH.so
#4  0x00007fff5cf68f96 in SMESH_Gen::Compute (this=0x2ccaa50, aMesh=..., aShape=..., aShapeOnly=false, anUpward=false, aDim=MeshDim_3D, 
    aShapesId=0x0) from /home/lo/anaconda/envs/fc_2/lib/./libSMESH.so
#5  0x00007fff5e10769a in MeshPart::Mesher::createMesh (this=0x7fffffffb1b0) from /home/lo/anaconda/envs/fc_2/lib/MeshPart.so
#6  0x00007fff5e0efd92 in MeshPart::Module::meshFromShape (this=0x2b6cc70, args=..., kwds=...) from /home/lo/anaconda/envs/fc_2/lib/MeshPart.so
#7  0x00007fff5e0f5a3f in Py::ExtensionModule<MeshPart::Module>::invoke_method_keyword (this=0x2b6cc70, method_def=0x2b6cee0, args=..., 
    keywords=...) from /home/lo/anaconda/envs/fc_2/lib/MeshPart.so
Seems the error is somewhere in the libocc.so from netgen.
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: Packaging solution: (ana)conda

Post by looo »

I have created a pull-request for the shared pointer stuff so it doesn't get lost: https://github.com/FreeCAD/FreeCAD/pull/277
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: Packaging solution: (ana)conda

Post by looo »

any ideas how to debug the netgen stuff? I tried to place std::cout in the netgen plugin but nothing got printed. I also have build netgen in debug mode, but gdb doesn't show anything related to netgen.
wmayer
Founder
Posts: 20317
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Packaging solution: (ana)conda

Post by wmayer »

I tried to place std::cout in the netgen plugin but nothing got printed.
Use std::cerr instead.
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: Packaging solution: (ana)conda

Post by looo »

thanks, I have tried to debug with std::cerr. It seems that the problem is at the first call to OCCGenerateMesh. And in this function the crash happen here: https://github.com/looooo/netgen/blob/m ... .cpp#L1294

Looking at the smesh implementation the shared pointer holds a NULL. Is it a problem to set a member of a shared NULL pointer?
wmayer
Founder
Posts: 20317
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Packaging solution: (ana)conda

Post by wmayer »

Looking at the smesh implementation the shared pointer holds a NULL. Is it a problem to set a member of a shared NULL pointer?
Of course. OCCGenerateMesh expects a pointer to an existing Mesh instance. If it's a NULL pointer then trying to access a member variable causes a segmentation fault. So, the question is why in NETGENPlugin_Mesher::Compute() is the member "_ngMesh" NULL?
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: Packaging solution: (ana)conda

Post by looo »

in older versions of netgen a new mesh was created inside the OCCGenerateMesh function. https://github.com/vejmarie/netgen_5.3. ... .cpp#L1283.
Post Reply