Skip to content

Freeing correctly TFormInfo when removed in destructor #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

spinettaro
Copy link

@spinettaro spinettaro commented Feb 26, 2024

Once removed during the destructor the form needs to be freed

@spinettaro spinettaro changed the title added doOwnsKeys to free correctly TFormInfo when removed Freeing correctly TFormInfo when removed in destructor Feb 26, 2024
Copy link
Owner

@andrea-magni andrea-magni left a comment

Choose a reason for hiding this comment

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

Hi Daniele, the Remove(LKey) call at line 169, is a call to TFormStand.Remove(ASubject: TSubject) (line 311, same file) that should end up freeing the form object.
What issue are you experiencing and on which platform? I have some suspects on the following IFDEF:

{$IFDEF AUTOREFCOUNT}
  LInfo.DisposeOf;
  LInfo := nil;
{$ELSE}
  LInfo.Free;
{$ENDIF}

I am considering stripping out the first case and to keep the LInfo.Free instruction only.
Let me know your thoughts and thanks for contributing.

@spinettaro
Copy link
Author

Ciao Andrea! Sorry for the late response :) What I'm experiencing is a Memory leak when I close the application. I'm testing the application with Win32 configuration. I can also create a simple project if you need it but here you can see the simple steps to reproduce it:

  • Create a new project with 2 forms: Form1 and Form2
  • Set ReportMemoryLeaksOnShutdown := True;
  • Add a button in Form1
  • under button click event uses aFormStand.NewAndShow<TForm2>
  • close the application

Expected Result:

  • The application closes successfully without a memory leak report

Actual Result:

  • The application closes with a memory leak report

Let me know

@spinettaro
Copy link
Author

oh sorry I missed a piece, I think that is the old code I committed for merge. I think the issue is that there is no check when the Form is created by TFormStand, then the lines you suggested should add a check if the form is owned by LInfo:

{$IFDEF AUTOREFCOUNT}
  LInfo.DisposeOf;
  LInfo := nil;
{$ELSE}
  if LInfo.FormIsOwned then LInfo.Form.Free;
  LInfo.Free;
{$ENDIF}

or create a specific destructor for it

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