star-cad

Geometric operators for computer-aided design
git clone git://git.meso-star.fr/star-cad.git
Log | Files | Refs | README | LICENSE

commit c13413e928a413bb282b60cfa3a85f55e5a42693
parent 8b91856d961532dc43bfab31e0832ec0074a2465
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date:   Mon,  5 Sep 2022 16:54:03 +0200

Fix step import internal memory management

Diffstat:
Msrc/scad_geometry.c | 35++++++++++++++++++++++-------------
1 file changed, 22 insertions(+), 13 deletions(-)

diff --git a/src/scad_geometry.c b/src/scad_geometry.c @@ -1117,9 +1117,10 @@ scad_step_import { int ierr; int* tagout = NULL; - size_t tagoutn, i; + size_t tagoutn, i, ga_sz; struct str strname; int name_initialized = 0; + struct scad_geometry** geom_array = NULL; res_T res = RES_OK; if(!filename) { @@ -1135,24 +1136,31 @@ scad_step_import gmshModelOccImportShapes(filename, &tagout, &tagoutn, 1, "step", &ierr); ERR(gmsh_err_to_res_T(ierr)); - *out_geometry = malloc(tagoutn/2 * sizeof(struct scad_geometry*)); + ASSERT(tagoutn % 2 == 0); + ga_sz = tagoutn / 2; + geom_array = malloc(ga_sz * sizeof(struct scad_geometry*)); + if(!geom_array) { + res = RES_MEM_ERR; + goto error; + } str_init(get_device()->allocator, &strname); name_initialized = 1; - for (i=0; i<tagoutn/2; ++i) { + for (i=0; i<ga_sz; ++i) { ERR(str_set(&strname, name)); ERR(str_append_printf(&strname,"_%lu", (unsigned long)i)); - ERR(scad_geometry_create(str_cget(&strname), &(*out_geometry)[i])); - (*out_geometry)[i]->gmsh_dimTags_n = 2; - (*out_geometry)[i]->gmsh_dimTags = malloc(2 * sizeof(int)); - (*out_geometry)[i]->gmsh_dimTags[0] = tagout[2*i]; - (*out_geometry)[i]->gmsh_dimTags[1] = tagout[2*i+1]; - ERR(device_register_tags((*out_geometry)[i])); + ERR(scad_geometry_create(str_cget(&strname), geom_array+i)); + geom_array[i]->gmsh_dimTags_n = 2; + geom_array[i]->gmsh_dimTags = malloc(2 * sizeof(int)); + geom_array[i]->gmsh_dimTags[0] = tagout[2*i]; + geom_array[i]->gmsh_dimTags[1] = tagout[2*i+1]; + ERR(device_register_tags(geom_array[i])); } - *out_geometry_n = tagoutn/2; exit: + *out_geometry_n = ga_sz ; + *out_geometry = geom_array; if(name_initialized) str_release(&strname); return res; error: @@ -1160,9 +1168,10 @@ error: gmshModelOccRemove(tagout, tagoutn, 1, &ierr); free(tagout); } - if(out_geometry) { - free(out_geometry); - out_geometry = NULL; + ga_sz = 0; + if(geom_array) { + free(geom_array); + geom_array = NULL; } goto exit; }