Changeset 18453


Ignore:
Timestamp:
08/21/14 09:20:51 (11 years ago)
Author:
Mathieu Morlighem
Message:

BUG: fixed memory leak in bamg and in InterpFromMeshToMesh2d, python needs to make a COPY of the data in WriteData just like matlab otherwise we cannot delete the data after the WriteData call and matlab leaks

Location:
issm/trunk-jpl/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/bamg/Mesh.cpp

    r18064 r18453  
    227227                /*Original code from Frederic Hecht <hecht@ann.jussieu.fr> (BAMG v1.01, Mesh2.cpp/Triangles)*/
    228228
    229                 //if (vertices)             delete [] vertices;
    230                 if (edges)                delete [] edges;
    231                 if (triangles)            delete [] triangles;
    232                 if (quadtree)             delete    quadtree;
    233                 //if (orderedvertices)                delete [] orderedvertices;
    234                 if (subdomains)           delete []  subdomains;
    235                 if (VerticesOnGeomEdge)   delete [] VerticesOnGeomEdge;
    236                 if (VerticesOnGeomVertex) delete [] VerticesOnGeomVertex;
    237                 if (VertexOnBThVertex)    delete [] VertexOnBThVertex;
    238                 if (VertexOnBThEdge)      delete [] VertexOnBThEdge;
    239 
    240                 if (&Gh) {
     229                if(vertices)             delete [] vertices;
     230                if(edges)                delete [] edges;
     231                if(triangles)            delete [] triangles;
     232                if(quadtree)             delete    quadtree;
     233                if(orderedvertices)      delete [] orderedvertices;
     234                if(subdomains)           delete []  subdomains;
     235                if(VerticesOnGeomEdge)   delete [] VerticesOnGeomEdge;
     236                if(VerticesOnGeomVertex) delete [] VerticesOnGeomVertex;
     237                if(VertexOnBThVertex)    delete [] VertexOnBThVertex;
     238                if(VertexOnBThEdge)      delete [] VertexOnBThEdge;
     239
     240                if(&Gh){
    241241                        if (Gh.NbRef>0) Gh.NbRef--;
    242242                        else if (Gh.NbRef==0) delete &Gh;
    243243                }
    244                 if (&BTh && (&BTh != this)) {
     244                if(&BTh && (&BTh != this)){
    245245                        if (BTh.NbRef>0) BTh.NbRef--;
    246246                        else if (BTh.NbRef==0) delete &BTh;
     
    27262726
    27272727                /*Allocate if maxnbv_in>0*/
    2728                 if (maxnbv_in) {
     2728                if(maxnbv_in){
    27292729                        vertices=new BamgVertex[maxnbv];
    27302730                        _assert_(vertices);
     
    27342734                        _assert_(triangles);
    27352735                }
    2736                 else {
     2736                else{
    27372737                        vertices=NULL;
    27382738                        orderedvertices=NULL;
  • issm/trunk-jpl/src/c/modules/InterpFromMeshToMesh2dx/InterpFromMeshToMesh2dx.cpp

    r16521 r18453  
    4949
    5050        /*read background mesh*/
    51         Mesh Th(index_data,x_data,y_data,nods_data,nels_data);
     51        Mesh* Th=new Mesh(index_data,x_data,y_data,nods_data,nels_data);
    5252
    5353        /*Get reference number (for subdomains)*/
    54         long* reft = xNew<long>(Th.nbt);
    55         Th.TriangleReferenceList(reft);
    56         Th.CreateSingleVertexToTriangleConnectivity();
     54        long* reft = xNew<long>(Th->nbt);
     55        Th->TriangleReferenceList(reft);
     56        Th->CreateSingleVertexToTriangleConnectivity();
    5757
    5858        /*Get domain boundaries*/
     
    7979                /*Get current point coordinates*/
    8080                r.x=x_interp[i]; r.y=y_interp[i];
    81                 I2 I=Th.R2ToI2(r);
     81                I2 I=Th->R2ToI2(r);
    8282
    8383                /*Find triangle holding r/I*/
    84                 Triangle &tb=*Th.TriangleFindFromCoord(I,dete);
     84                Triangle &tb=*Th->TriangleFindFromCoord(I,dete);
    8585
    8686                /*point inside convex*/
     
    9292                        areacoord[2]= (double) dete[2]/tb.det;
    9393                        /*3 vertices of the triangle*/
    94                         i0=Th.GetId(tb[0]);
    95                         i1=Th.GetId(tb[1]);
    96                         i2=Th.GetId(tb[2]);
     94                        i0=Th->GetId(tb[0]);
     95                        i1=Th->GetId(tb[1]);
     96                        i2=Th->GetId(tb[2]);
    9797                        /*triangle number*/
    98                         it=Th.GetId(tb);
     98                        it=Th->GetId(tb);
    9999
    100100                        /*Inside convex but outside mesh*/
     
    120120                                areacoord[OppositeVertex[k]] = 1 - aa -bb;
    121121                                //3 vertices of the triangle
    122                                 i0=Th.GetId(tc[0]);
    123                                 i1=Th.GetId(tc[1]);
    124                                 i2=Th.GetId(tc[2]);
     122                                i0=Th->GetId(tc[0]);
     123                                i1=Th->GetId(tc[1]);
     124                                i2=Th->GetId(tc[2]);
    125125                                //triangle number
    126                                 it=Th.GetId(tc);
     126                                it=Th->GetId(tc);
    127127                        }
    128128                }
     
    145145
    146146        /*clean-up and return*/
     147        delete Th;
    147148        xDelete<long>(reft);
    148149        *pdata_interp=data_interp;
  • issm/trunk-jpl/src/wrappers/InterpFromMeshToMesh2d/InterpFromMeshToMesh2d.cpp

    r15105 r18453  
    7676
    7777        /*end module: */
     78        xDelete<int>(index);
     79        xDelete<double>(x_data);
     80        xDelete<double>(y_data);
     81        xDelete<double>(data);
     82        xDelete<double>(x_interp);
     83        xDelete<double>(y_interp);
     84        xDelete<double>(data_interp);
     85        delete options;
    7886        MODULEEND();
    7987}
  • issm/trunk-jpl/src/wrappers/python/io/WritePythonData.cpp

    r15165 r18453  
    3535        PyObject* array=NULL;
    3636
    37         dims[0]=(npy_intp)M;
    38         dims[1]=(npy_intp)N;
    39         array=PyArray_SimpleNewFromData(2,dims,NPY_DOUBLE,matrix);
     37        /*copy matrix: */
     38        double* matrix_python=xNew<double>(M*N);
     39        memcpy(matrix_python,matrix,M*N*sizeof(double));
     40
     41        dims[0]=(npy_intp)M;
     42        dims[1]=(npy_intp)N;
     43        array=PyArray_SimpleNewFromData(2,dims,NPY_DOUBLE,matrix_python);
    4044
    4145        PyTuple_SetItem(tuple, index, array);
     
    4751        PyObject* array=NULL;
    4852
    49         /*intermediary:*/
    50         long* lmatrix=NULL;
    51         int i;
    52 
    5353        /*transform into long matrix: */
    54         lmatrix=xNew<long>(M*N);
    55         for(i=0;i<M*N;i++)lmatrix[i]=(long)matrix[i];
     54        long* lmatrix=xNew<long>(M*N);
     55        for(int i=0;i<M*N;i++)lmatrix[i]=(long)matrix[i];
    5656
    5757        dims[0]=(npy_intp)M;
     
    6767        PyObject* array=NULL;
    6868
    69         dims[0]=(npy_intp)M;
    70         dims[1]=(npy_intp)N;
    71         array=PyArray_SimpleNewFromData(2,dims,NPY_BOOL,matrix);
     69        /*copy matrix: */
     70        bool* matrix_python=xNew<bool>(M*N);
     71        memcpy(matrix_python,matrix,M*N*sizeof(bool));
     72
     73        dims[0]=(npy_intp)M;
     74        dims[1]=(npy_intp)N;
     75        array=PyArray_SimpleNewFromData(2,dims,NPY_BOOL,matrix_python);
    7276
    7377        PyTuple_SetItem(tuple, index, array);
     
    7680void WriteData(PyObject* py_tuple, int index, double* vector, int M){
    7781
    78         double   *buffer = NULL;
    7982        npy_intp  dim   = 10;
    8083        PyObject *array = NULL;
    8184
    82         /*Copy vector*/
    83         buffer=xNew<double>(M);
    84         for(int i=0;i<M;i++)buffer[i]=vector[i];
     85
     86        /*copy vector: */
     87        double* vector_python=xNew<double>(M);
     88        memcpy(vector_python,vector,M*sizeof(double));
    8589
    8690        dim=(npy_intp)M;
    87         array=PyArray_SimpleNewFromData(1,&dim,NPY_DOUBLE,buffer);
     91        array=PyArray_SimpleNewFromData(1,&dim,NPY_DOUBLE,vector_python);
    8892
    8993        PyTuple_SetItem(py_tuple, index, array);
Note: See TracChangeset for help on using the changeset viewer.