Fix memory leak in radTInteraction::Setup#5
Conversation
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.
|
|
||
| NewMagnArray = NULL; | ||
| NewFieldArray = NULL; | ||
| IdentTransPtr = NULL; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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>
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>
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_ptrcauses the object to be automatically reclaimed regardless of whether the constructor throws or not. The lines that initialize the pointer toNULLare removed becauseunique_ptrinitializes 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.