Skip to content

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

Open
mgduda wants to merge 1 commit into
MPAS-Dev:developfrom
mgduda:framework/fix_generated_dimension_ptr
Open

Fix bug in registry-generated code to set up derived dimensions#1458
mgduda wants to merge 1 commit 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.

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.
@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!

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.

1 participant