svenreiche / Genesis-1.3-Version4

Time-dependent, 3D Code to simulate the amplification process of a Free-electron Laser.
GNU General Public License v3.0
55 stars 27 forks source link

Fix memory leak of wakegeo, wakerou and wakeext #125

Closed ken-lauer closed 10 months ago

ken-lauer commented 11 months ago

Building Genesis4 clued me into a bug in the master branch, a memory leak of 3 arrays in Wake.cpp post-init():

2023-12-04T23:03:48.8335470Z /Users/runner/Miniforge3/conda-bld/genesis4_1701730808588/work/src/Main/Wake.cpp:120:20: warning: left operand of comma operator has no effect [-Wunused-value]
2023-12-04T23:03:48.8434770Z   delete[] wakeres,wakegeo,wakerou,wakeext;
2023-12-04T23:03:48.8535100Z                    ^~~~~~~
2023-12-04T23:03:48.8592490Z /Users/runner/Miniforge3/conda-bld/genesis4_1701730808588/work/src/Main/Wake.cpp:120:28: warning: left operand of comma operator has no effect [-Wunused-value]
2023-12-04T23:03:48.8695580Z   delete[] wakeres,wakegeo,wakerou,wakeext;
2023-12-04T23:03:48.8743960Z                            ^~~~~~~
2023-12-04T23:03:48.8843570Z /Users/runner/Miniforge3/conda-bld/genesis4_1701730808588/work/src/Main/Wake.cpp:120:36: warning: expression result unused [-Wunused-value]
2023-12-04T23:03:48.8919660Z   delete[] wakeres,wakegeo,wakerou,wakeext;
2023-12-04T23:03:48.9011940Z                                    ^~~~~~~

The delete [] a, b, c, d syntax in actuality only deletes a, as the comma is not separating arguments to delete[] but rather is functioning as the comma operator. There are a number of examples of this on the Internet (example) so you're not alone in thinking this was valid. Clang is the only one that reported the warning in my testing.

(cc @ChristopherMayes )

svenreiche commented 10 months ago

Thanks for finding this bug.