Skip to content

Fix memory leak in radTInteraction::Setup#5

Open
per-gron wants to merge 1 commit into
ochubar:masterfrom
per-gron:mem3
Open

Fix memory leak in radTInteraction::Setup#5
per-gron wants to merge 1 commit into
ochubar:masterfrom
per-gron:mem3

Conversation

@per-gron

@per-gron per-gron commented Dec 29, 2019

Copy link
Copy Markdown

The memory allocated for IdentTransPtr was not reclaimed when the method failed, causing the constructor to throw and skip the destructor.

Changing the code to manage the memory with unique_ptr causes the object to be automatically reclaimed regardless of whether the constructor throws or not. The lines that initialize the pointer to NULL are removed because unique_ptr initializes to null automatically by default.

I found this with Address Sanitizer. The change removes the warning and seems to work. The change is tested on Linux/Clang only.

The memory allocated for IdentTransPtr was not reclaimed when the
method failed, causing the constructor to throw and skip the
destructor.

Changing the code to manage the memory with unique_ptr causes the
object to be automatically reclaimed regardless of whether the
constructor throws or not.
Comment thread cpp/src/core/radintrc.cpp

NewMagnArray = NULL;
NewFieldArray = NULL;
IdentTransPtr = NULL;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

There is probably a ton of places in the code that may lead to some memory leak(s) when a method fails. In our previous / current interfaces (Mathematica. Python) this is not so important, because entire simulation is usually re-started if some method fails. But all such places could indeed de analyzed more thoroughly, and eliminated.
In this particular case, instead of a pointer to a radIdentTrans object, we can keep the object itself (since it does not require a lot of memory) as a member variable, in that or in the base class.
I am working now on parallelizing (via MPI) this and other functions, so will postpone this eventual change until the corresponding update.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, code with manual memory management tends to not do the right thing around errors, it's just very very hard to get it right. This is where things like unique_ptr, vector and other higher level constructs really shine. In fact, there is a wide consensus that since C++11 there is essentially never a need to use new or delete at all in application level code*, and it's considered best practice to avoid them.

Cool that you are working on parallelizing this code! Let me warmly recommend ThreadSanitizer. It detects and pinpoints data races and other threading issues, which can be nearly impossible to fix or even find. If you write code that has any mutable state that is shared across threads you need this tool. It is one of the closest things to magic I have seen in the world of software.

*: The one exception to this is when creating unique_ptr objects, where you still need new. C++14 introduces std::make_unique to fix this little wart.

ksugahar added a commit to ksugahar/Radia that referenced this pull request Jun 2, 2026
…bar#5)

calc_em_table --curie-temp Tc [--curie-exp beta]: per-T BH magnetic-part
scaling f(T)=(1-T/Tc)^beta clamped [0,1] -> mu_r=1 at/above Tc.  Verified
Zs_re ~15.6x collapse across Curie; unit-tested.  Complements sigma(T)
(--sigma-T-curve).  Part of the T-dependence functionalization (ochubar#5).
ksugahar added a commit to ksugahar/Radia that referenced this pull request Jun 2, 2026
The biot ht-source no longer raises.  It now walks the coil centerline
from --coil-step (the same extractor the PEEC path uses), evaluates the
incident Biot-Savart field at each workpiece-surface DOF, and projects
it onto the local tangent plane to get the reference |H_t_ref(r)| --
needing NO upstream calc_fem_kelvin run.

The new physics factors into pure, mesh-free helpers:
  _polyline_to_filament_segments  centerline -> filament endpoint pairs
                                  (+ closed-loop wrap-around)
  _surface_vertex_normals         area-weighted Newell vertex normals
  _biot_Ht_on_surface             |H - (H.n)n| incident tangential mag

This is the INCIDENT tangential field (no workpiece backreaction), so
it under-predicts the surface field by ~2x for a good flat conductor
(the eddy-current image that doubles H_t is absent).  --biot-image-factor
(default 1.0 = raw incident) lets an informed user opt into that
doubling EXPLICITLY -- never applied silently (No-Fallbacks).  Prefer
--ht-source kelvin when the backreaction matters.

calc_heat_with_em_table is a CLI-only tool (no panel drives it), so the
new flags are advanced CLI knobs; no panel widget needed.

Verified:
  - tests/panels/test_heat_em_table_biot.py (5 tests): tangential
    projection + image-factor + current scaling validated against the
    closed-form circular-loop on-axis field H_z=I a^2/(2(a^2+z^2)^1.5);
    polyline->segment wrap-around.
  - end-to-end smoke (encircling loop + R25 cylinder .vol + steel
    em_table): biot branch heats the workpiece (T_max 240 C, P~13.5 kW,
    |H_t| 62..18330 A/m over 334 surface DOFs), finite positive power.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
ksugahar added a commit to ksugahar/Radia that referenced this pull request Jun 3, 2026
Stream-function framework matured to production: multi-surface biplanar
coils, min-inductance objective, 2-opt single-stroke ordering, per-step
manufacturing visualisation, and cylindrical Gx/Gz distortion (bankin-ho).
IH thermal-EM coupling v2 (sigma(T)/mu(T)/biot), figure tooling API
stabilised (lab_figure + audit), and stellarator-fusion MCP knowledge added.
cubit-mesh-export unchanged (v0.11.0, no C++ changes).

- radia 4.90.0: SF min-inductance / biplanar / single-stroke 2-opt /
  steps-plot / IH resonance target / sigma(T)+mu(T) biot-source (ochubar#5/ochubar#8);
  radia version shown in panel output; Cubit toolbar restored
- radia-mcp 0.100.0: radia-streamfunction server + SF/figure/fusion
  knowledge; TOOLS.md regenerated; 204 mcp_server tests pass

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants