Skip to content

Fix bug in registry-generated code to set up derived dimensions#1458

Merged
mgduda merged 2 commits into
MPAS-Dev:developfrom
mgduda:framework/fix_generated_dimension_ptr
May 26, 2026
Merged

Fix bug in registry-generated code to set up derived dimensions#1458
mgduda merged 2 commits into
MPAS-Dev:developfrom
mgduda:framework/fix_generated_dimension_ptr

Conversation

@mgduda
Copy link
Copy Markdown
Contributor

@mgduda mgduda commented May 22, 2026

This PR fixes a bug in registry-generated code for setting up derived dimensions.

Previous work (in PR #1451, commit ae45a18) introduced changes to eliminate memory leaks that occurred while setting up derived dimensions. The approach taken in that work involved deallocating dimensions after they were allocated and added to the dimension pool, since the pool assigns the value of the dimension to its own internal memory, and it is up to calling code to free any memory allocated for the dimension argument to mpas_pool_add_dimension. However, as an unintended side effect, any later derived dimensions that depended on a dimension whose value came from a namelist or other registry definiton would generate a segmentation fault after referencing an unassociated pointer to the now deallocated dimension.

For example, in the init_atmosphere core, the nVertLevels dimension is set based on the namelist option config_nvertlevels, and the nVertLevelsP1 dimension is subsequently set to nVertLevels+1.

This PR fixes this issue by re-setting pointers to point to pool-owned memory after a dimension has been allocated, added to the dimension pool, and freed.

@mgduda
Copy link
Copy Markdown
Contributor Author

mgduda commented May 22, 2026

For the record, it's entirely my fault that this issue made its way into the develop branch in the first place -- I should have tested more thoroughly before merging PR #1451!

Comment thread src/tools/registry/gen_inc.c Outdated
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look good overall and able to build and run without issues. Just had a small comment re. indentation of this line in the generated code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this line isn't part of any of my changes in this PR, I can push an update to correct the indentation of this line as well as line 1082.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a separate commit to address indentation issues in the generated Fortran code.

mgduda added 2 commits May 26, 2026 22:26
The previous commit (ae45a18) aimed to fix memory leaks that occurred while
setting up derived dimensions by deallocating dimensions after they were
allocated and added to the dimension pool (since the pool assigns the value of
the dimension to its own internal memory, it is up to calling code to free any
memory allocated for the dimension argument to mpas_pool_add_dimension).
However, any later derived dimensions that depended on a dimension whose value
came from a namelist or other registry definiton would generate a segmentation
fault after referencing an unassociated pointer to the now deallocated
dimension.

For example, in the init_atmosphere core, the 'nVertLevels' dimension is set
based on the namelist option 'config_nvertlevels', and the 'nVertLevelsP1'
dimension is subsequently set to 'nVertLevels+1'.

This commit fixes this issue by re-setting pointers to point to pool-owned
memory after a dimension has been allocated, added to the dimension pool, and
freed.
…utines.inc

The parse_dimensions_from_registry C code in the registry is responsible for
generating the block_dimension_routines.inc file, and this commit makes changes
in parse_dimensions_from_registry to clean up whitespace / indentation issues in
the generated Fortran code. The changes in this commit otherwise have no impact
on results.
@mgduda mgduda force-pushed the framework/fix_generated_dimension_ptr branch from cbf8441 to 4069eb3 Compare May 26, 2026 22:54
@mgduda mgduda requested a review from abishekg7 May 26, 2026 22:57
Copy link
Copy Markdown
Collaborator

@abishekg7 abishekg7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@mgduda mgduda merged commit c33f6ff into MPAS-Dev:develop May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants