Skip to content

Commit

Permalink
m.nviz.image: Fix Resource Leak issue (#5079)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhamDesai authored Feb 26, 2025
1 parent 9b8a52a commit 34b6764
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 46 deletions.
71 changes: 39 additions & 32 deletions misc/m.nviz.image/surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
*/
int load_rasters(const struct GParams *params, nv_data *data)
{
const char *mapset;
const char *mapset = NULL;
char *mname = NULL;
int i;
int nelevs, nelev_map, nelev_const, ncolor_map, ncolor_const, nmask_map;
int ntransp_map, ntransp_const, nshine_map, nshine_const;
Expand All @@ -50,11 +51,10 @@ int load_rasters(const struct GParams *params, nv_data *data)
G_fatal_error(_("Raster map <%s> not found"),
params->elev_map->answers[i]);
}

id = Nviz_new_map_obj(
MAP_OBJ_SURF,
G_fully_qualified_name(params->elev_map->answers[i], mapset),
0.0, data);
mname =
G_fully_qualified_name(params->elev_map->answers[i], mapset);
id = Nviz_new_map_obj(MAP_OBJ_SURF, mname, 0.0, data);
G_free(mname);
}
else {
if (i - nelev_map < nelev_const &&
Expand Down Expand Up @@ -107,10 +107,11 @@ int load_rasters(const struct GParams *params, nv_data *data)
G_fatal_error(_("Raster map <%s> not found"),
params->color_map->answers[i]);
}
Nviz_set_attr(
id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
G_fully_qualified_name(params->color_map->answers[i], mapset),
-1.0, data);
mname =
G_fully_qualified_name(params->color_map->answers[i], mapset);
Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT, mname, -1.0,
data);
G_free(mname);
}
/* check for color value */
else if (i - ncolor_map < ncolor_const &&
Expand All @@ -122,14 +123,14 @@ int load_rasters(const struct GParams *params, nv_data *data)
}
else { /* use by default elevation map for coloring */
if (nelev_map > 0) {
Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
G_fully_qualified_name(
params->elev_map->answers[i], mapset),
-1.0, data);
mname = G_fully_qualified_name(params->elev_map->answers[i],
mapset);
Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT, mname, -1.0,
data);
G_verbose_message(
_("Color attribute not defined, using default <%s>"),
G_fully_qualified_name(params->elev_map->answers[i],
mapset));
mname);
G_free(mname);
}
else {
G_fatal_error(_("Missing color attribute for surface %d"),
Expand All @@ -138,18 +139,20 @@ int load_rasters(const struct GParams *params, nv_data *data)
}
/* mask */
if (i < nmask_map && strcmp(params->mask_map->answers[i], "")) {
Nviz_set_attr(
id, MAP_OBJ_SURF, ATT_MASK, MAP_ATT,
G_fully_qualified_name(params->mask_map->answers[i], mapset),
-1.0, data);
mname =
G_fully_qualified_name(params->mask_map->answers[i], mapset);
Nviz_set_attr(id, MAP_OBJ_SURF, ATT_MASK, MAP_ATT, mname, -1.0,
data);
G_free(mname);
}

/* transparency */
if (i < ntransp_map && strcmp(params->transp_map->answers[i], "")) {
Nviz_set_attr(
id, MAP_OBJ_SURF, ATT_TRANSP, MAP_ATT,
G_fully_qualified_name(params->transp_map->answers[i], mapset),
-1.0, data);
mname =
G_fully_qualified_name(params->transp_map->answers[i], mapset);
Nviz_set_attr(id, MAP_OBJ_SURF, ATT_TRANSP, MAP_ATT, mname, -1.0,
data);
G_free(mname);
}
else if (i - ntransp_map < ntransp_const &&
strcmp(params->transp_const->answers[i - ntransp_map], "")) {
Expand All @@ -160,10 +163,11 @@ int load_rasters(const struct GParams *params, nv_data *data)

/* shininess */
if (i < nshine_map && strcmp(params->shine_map->answers[i], "")) {
Nviz_set_attr(
id, MAP_OBJ_SURF, ATT_SHINE, MAP_ATT,
G_fully_qualified_name(params->shine_map->answers[i], mapset),
-1.0, data);
mname =
G_fully_qualified_name(params->shine_map->answers[i], mapset);
Nviz_set_attr(id, MAP_OBJ_SURF, ATT_SHINE, MAP_ATT, mname, -1.0,
data);
G_free(mname);
}
else if (i - nshine_map < nshine_const &&
strcmp(params->shine_const->answers[i - nshine_map], "")) {
Expand All @@ -174,10 +178,11 @@ int load_rasters(const struct GParams *params, nv_data *data)

/* emission */
if (i < nemit_map && strcmp(params->emit_map->answers[i], "")) {
Nviz_set_attr(
id, MAP_OBJ_SURF, ATT_EMIT, MAP_ATT,
G_fully_qualified_name(params->emit_map->answers[i], mapset),
-1.0, data);
mname =
G_fully_qualified_name(params->emit_map->answers[i], mapset);
Nviz_set_attr(id, MAP_OBJ_SURF, ATT_EMIT, MAP_ATT, mname, -1.0,
data);
G_free(mname);
}
else if (i - nemit_map < nemit_const &&
strcmp(params->emit_const->answers[i - nemit_map], "")) {
Expand All @@ -191,6 +196,7 @@ int load_rasters(const struct GParams *params, nv_data *data)
set_default_wirecolors(data, i);
*/
}
G_free(surf_list);

return nsurfs;
}
Expand Down Expand Up @@ -273,6 +279,7 @@ void surface_set_draw_mode(const struct GParams *params)
/* wire color */
GS_set_wire_color(id, Nviz_color_from_str(wire_color));
}
G_free(surf_list);

return;
}
40 changes: 31 additions & 9 deletions misc/m.nviz.image/vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ int load_vectors(const struct Option *elev_map, const struct Option *elev_const,

surf_list = GS_get_surf_list(&nsurf);
GS_set_att_const(surf_list[0], ATT_TRANSP, 255);
G_free(surf_list);
}

nvects = 0;
Expand All @@ -81,9 +82,9 @@ int load_vectors(const struct Option *elev_map, const struct Option *elev_const,
if (mapset == NULL) {
G_fatal_error(_("Vector map <%s> not found"), vect->answers[i]);
}
id = Nviz_new_map_obj(map_obj_type,
G_fully_qualified_name(vect->answers[i], mapset),
0.0, data);
char *mname = G_fully_qualified_name(vect->answers[i], mapset);
id = Nviz_new_map_obj(map_obj_type, mname, 0.0, data);
G_free(mname);

/* set position */
x = atof(position->answers[i * 3 + 0]);
Expand Down Expand Up @@ -111,7 +112,7 @@ int load_vectors(const struct Option *elev_map, const struct Option *elev_const,
*/
int vlines_set_attrb(const struct GParams *params)
{
int i, layer, color, width, flat, height;
int i, layer = -1, color, width, flat, height;
int *vect_list, nvects;
int have_colors;

Expand All @@ -138,23 +139,28 @@ int vlines_set_attrb(const struct GParams *params)
flat = 0;

/* style (mode -- use memory by default) */
if (GV_set_style(vect_list[i], TRUE, color, width, flat) < 0)
if (GV_set_style(vect_list[i], TRUE, color, width, flat) < 0) {
G_free(vect_list);
return 0;
}

/* check for vector color table */
have_colors = Vect_read_colors(params->vlines->answers[i], "", &colors);

if (have_colors || color_column || width_column)
if (GV_set_style_thematic(vect_list[i], layer, color_column,
width_column,
have_colors ? &colors : NULL) < 0)
have_colors ? &colors : NULL) < 0) {
G_free(vect_list);
return 0;
}

/* height */
height = atoi(params->vline_height->answers[i]);
if (height > 0)
GV_set_trans(vect_list[i], 0.0, 0.0, height);
}
G_free(vect_list);

return 1;
}
Expand All @@ -169,7 +175,7 @@ int vlines_set_attrb(const struct GParams *params)
*/
int vpoints_set_attrb(const struct GParams *params)
{
int i, layer, have_colors, with_z;
int i, have_colors;
int *site_list, nsites;
int marker, color, width;
float size;
Expand All @@ -181,6 +187,7 @@ int vpoints_set_attrb(const struct GParams *params)
site_list = GP_get_site_list(&nsites);

for (i = 0; i < nsites; i++) {
int layer = -1, with_z = 0;
check_map(params, i, FALSE, &layer, &with_z);

color = Nviz_color_from_str(params->vpoint_color->answers[i]);
Expand Down Expand Up @@ -208,8 +215,10 @@ int vpoints_set_attrb(const struct GParams *params)
GP_set_zmode(site_list[i], TRUE);
}

if (GP_set_style(site_list[i], color, width, size, marker) < 0)
if (GP_set_style(site_list[i], color, width, size, marker) < 0) {
G_free(site_list);
return 0;
}

/* check for vector color table */
have_colors =
Expand All @@ -219,10 +228,13 @@ int vpoints_set_attrb(const struct GParams *params)
marker_column) {
if (GP_set_style_thematic(site_list[i], layer, color_column,
width_column, size_column, marker_column,
have_colors ? &colors : NULL) < 0)
have_colors ? &colors : NULL) < 0) {
G_free(site_list);
return 0;
}
}
}
G_free(site_list);

return 1;
}
Expand Down Expand Up @@ -251,6 +263,7 @@ int check_map(const struct GParams *params, int index, int vlines, int *field,

Fi = NULL;
driver = NULL;
column = NULL;

if (vlines) {
map = params->vlines->answers[index];
Expand Down Expand Up @@ -310,6 +323,8 @@ int check_map(const struct GParams *params, int index, int vlines, int *field,

if (db_column_Ctype(driver, Fi->table, color) != DB_C_TYPE_STRING)
G_fatal_error(_("Data type of color column must be character"));
db_free_column(column);
column = NULL;
}
if (size) {
db_get_column(driver, Fi->table, size, &column);
Expand All @@ -320,6 +335,8 @@ int check_map(const struct GParams *params, int index, int vlines, int *field,
type = db_column_Ctype(driver, Fi->table, size);
if (type != DB_C_TYPE_INT && type != DB_C_TYPE_DOUBLE)
G_fatal_error(_("Data type of size column must be numeric"));
db_free_column(column);
column = NULL;
}
if (width) {
db_get_column(driver, Fi->table, width, &column);
Expand All @@ -330,6 +347,8 @@ int check_map(const struct GParams *params, int index, int vlines, int *field,
type = db_column_Ctype(driver, Fi->table, width);
if (type != DB_C_TYPE_INT && type != DB_C_TYPE_DOUBLE)
G_fatal_error(_("Data type of width column must be numeric"));
db_free_column(column);
column = NULL;
}
if (marker) {
db_get_column(driver, Fi->table, marker, &column);
Expand All @@ -341,9 +360,12 @@ int check_map(const struct GParams *params, int index, int vlines, int *field,
if (db_column_Ctype(driver, Fi->table, marker) != DB_C_TYPE_STRING)
G_fatal_error(
_("Data type of marker column must be character"));
db_free_column(column);
column = NULL;
}

db_close_database_shutdown_driver(driver);
Vect_destroy_field_info(Fi);
}

Vect_close(&Map);
Expand Down
11 changes: 6 additions & 5 deletions misc/m.nviz.image/volume.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ int load_rasters3d(const struct GParams *params, nv_data *data)
G_fatal_error(_("3d raster map <%s> not found"),
params->volume->answers[i]);
}

id = Nviz_new_map_obj(
MAP_OBJ_VOL,
G_fully_qualified_name(params->volume->answers[i], mapset), 0.0,
data);
char *mname =
G_fully_qualified_name(params->volume->answers[i], mapset);
id = Nviz_new_map_obj(MAP_OBJ_VOL, mname, 0.0, data);
G_free(mname);

/* set position */
if (opt_get_num_answers(params->volume_pos) != 3 * nvol) {
Expand Down Expand Up @@ -243,6 +242,7 @@ int add_isosurfs(const struct GParams *params, nv_data *data UNUSED)

GVL_isosurf_set_drawmode(id, draw_mode);
}
G_free(vol_list);

return 1;
}
Expand Down Expand Up @@ -331,6 +331,7 @@ int add_slices(const struct GParams *params, nv_data *data UNUSED)

GVL_slice_set_drawmode(id, draw_mode);
}
G_free(vol_list);

return 1;
}

0 comments on commit 34b6764

Please sign in to comment.