Skip to content

WIP: Fix memory leak in ProtonPairsToDistanceDrivenProjection#71

Open
acoussat wants to merge 1 commit intoRTKConsortium:mainfrom
acoussat:fix-memory-leak
Open

WIP: Fix memory leak in ProtonPairsToDistanceDrivenProjection#71
acoussat wants to merge 1 commit intoRTKConsortium:mainfrom
acoussat:fix-memory-leak

Conversation

@acoussat
Copy link
Collaborator

@acoussat acoussat commented Mar 9, 2026

When running a Python script like

for p in range(number_of_projections):
    pct.pctbinning(input=os.path.join(pairs_folder, f'pairs{p:04d}.mhd'),
                   output=os.path.join(projections_folder, f'projections{p}.mhd'),
                   source=-1000.,
                   size=[200, 1, 200],
                   spacing=[2., 1., 1.],
                   verbose=True)

the memory consumption increases to a suspiciously high amount (dozens of gigabytes). After careful investigation using valgrind, it seems that the variable m_ConvFunc used in ProtonPairsToDistanceDrivenProjection is never freed, resulting in a leak of about 50MB per call to pct.pctbinning. These 50MB seem to correspond to the Bethe-Bloch LUT.

This wasn't a problem when the same script was ran through bash, because (to the best of my understanding) each pctbinning invocation resulted in a new sub-process that was deleted after completion. With Python, all the computations are executed in a single Python process, hence the issue.

I am not sure if my fix here is really "ITK-like", but as far as I tested this seems to do the trick. I assume that there is a way to fix it that better respects ITK's conventions.

protected:
ProtonPairsToDistanceDrivenProjection();
virtual ~ProtonPairsToDistanceDrivenProjection() {}
virtual ~ProtonPairsToDistanceDrivenProjection() { delete m_ConvFunc; }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks (and sorry for the bug). I would move this to AfterThreadedGenerateData since it's been allocated in BeforeThreadedGenerateData.
We could also consider making this LUT a singleton to avoid recreating it if the parameters are not changed but that can be a future work.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the feedback. I moved the deletion to AfterThreadedGenerateData as you suggest in 0e37fec.

@acoussat
Copy link
Collaborator Author

acoussat commented Mar 9, 2026

The CI fails with the following error:

Imported target "RTK" includes non-existent path

     "/work/utilities/lp_solve"
 
   in its INTERFACE_INCLUDE_DIRECTORIES.  Possible reasons include:
 
   * The path was deleted, renamed, or moved to another location.
 
   * An install or uninstall procedure did not complete successfully.
 
   * The installation package was faulty and references files it does not
   provide.

The error seems to be linked with RTKConsortium/RTK#901. Do we need to change anything on PCT side? (@SimonRit @axel-grc)

++itCOut;
}

delete m_ConvFunc;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think it should be in this if (m_ComputeNoise) block. It can be deleted unconditionally I believe.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oops, my bad, I was sure I was at the end of the function when I added the line! Fixed in 00909ac.

@SimonRit
Copy link
Collaborator

SimonRit commented Mar 9, 2026

The CI fails with the following error:

Imported target "RTK" includes non-existent path

     "/work/utilities/lp_solve"
 
   in its INTERFACE_INCLUDE_DIRECTORIES.  Possible reasons include:
 
   * The path was deleted, renamed, or moved to another location.
 
   * An install or uninstall procedure did not complete successfully.
 
   * The installation package was faulty and references files it does not
   provide.

The error seems to be linked with RTKConsortium/RTK#901. Do we need to change anything on PCT side? (@SimonRit @axel-grc)

I don't know what is the issue but it seems to be on the RTK side, I don't think you should have to change PCT. Maybe something's wrong in these lines. I don't know how to fix it though... Maybe @axel-grc can have a look?

@acoussat
Copy link
Collaborator Author

For future reference, we should check this PR again once RTKConsortium/RTK#918 is merged.

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