Skip to content

Commit cf9783f

Browse files
V624-007: Fix memory leaks in lsp-ada_handlers.adb
1 parent 3703f6b commit cf9783f

File tree

6 files changed

+45
-7
lines changed

6 files changed

+45
-7
lines changed

source/ada/lsp-ada_documents.adb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,6 +1808,17 @@ package body LSP.Ada_Documents is
18081808
Recompute_Indexes (Self);
18091809
end Initialize;
18101810

1811+
-------------
1812+
-- Cleanup --
1813+
-------------
1814+
1815+
procedure Cleanup (Self : in out Document) is
1816+
begin
1817+
for Source of Self.Diagnostic_Sources loop
1818+
LSP.Diagnostic_Sources.Unchecked_Free (Source);
1819+
end loop;
1820+
end Cleanup;
1821+
18111822
------------------------
18121823
-- Reset_Symbol_Cache --
18131824
------------------------

source/ada/lsp-ada_documents.ads

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ package LSP.Ada_Documents is
5959
-- Create a new document from a TextDocumentItem. Use Diagnostic as
6060
-- project status diagnostic source.
6161

62+
procedure Cleanup (Self : in out Document);
63+
-- Free all the data associated to this document.
64+
6265
-----------------------
6366
-- Contents handling --
6467
-----------------------

source/ada/lsp-ada_handlers.adb

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,6 @@ package body LSP.Ada_Handlers is
168168
procedure Index_Files (Self : access Message_Handler);
169169
-- Index all loaded files in each context. Emit progresormation.
170170

171-
procedure Unchecked_Free is new Ada.Unchecked_Deallocation
172-
(LSP.Ada_Documents.Document, Internal_Document_Access);
173-
174171
procedure Release_Contexts_And_Project_Info (Self : access Message_Handler);
175172
-- Release the memory associated to project information in Self
176173

@@ -514,20 +511,24 @@ package body LSP.Ada_Handlers is
514511
-- Cleanup --
515512
-------------
516513

517-
procedure Cleanup (Self : access Message_Handler) is
514+
procedure Cleanup (Self : access Message_Handler)
515+
is
518516
begin
519517
if Self.File_Monitor.Assigned then
520518
Self.File_Monitor.Stop_Monitoring_Directories;
521519
end if;
522520

523521
-- Cleanup documents
524522
for Document of Self.Open_Documents loop
525-
Unchecked_Free (Document);
523+
Free (Document);
526524
end loop;
527525
Self.Open_Documents.Clear;
528526

529527
-- Cleanup contexts, project and environment
530528
Self.Release_Contexts_And_Project_Info;
529+
530+
-- Free the file monitor
531+
LSP.File_Monitors.Unchecked_Free (Self.File_Monitor);
531532
end Cleanup;
532533

533534
-----------------------
@@ -2553,7 +2554,7 @@ package body LSP.Ada_Handlers is
25532554
Context.Flush_Document (File);
25542555
end loop;
25552556

2556-
Unchecked_Free (Document);
2557+
Free (Document);
25572558

25582559
else
25592560
-- We have received a didCloseTextDocument but the document was
@@ -3686,7 +3687,7 @@ package body LSP.Ada_Handlers is
36863687
(Document.all, Context.all, Pattern,
36873688
Canceled.Has_Been_Canceled'Access, Result.result);
36883689

3689-
Unchecked_Free (Internal_Document_Access (Document));
3690+
Free (Internal_Document_Access (Document));
36903691
end;
36913692
else
36923693
Self.Get_Symbols
@@ -5900,6 +5901,18 @@ package body LSP.Ada_Handlers is
59005901
end if;
59015902
end Publish_Diagnostics;
59025903

5904+
----------
5905+
-- Free --
5906+
----------
5907+
5908+
procedure Free (Self : in out Internal_Document_Access) is
5909+
procedure Unchecked_Free is new Ada.Unchecked_Deallocation
5910+
(LSP.Ada_Documents.Document, Internal_Document_Access);
5911+
begin
5912+
Self.Cleanup;
5913+
Unchecked_Free (Self);
5914+
end Free;
5915+
59035916
------------------
59045917
-- Show_Message --
59055918
------------------

source/ada/lsp-ada_handlers.ads

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ private
113113

114114
type Internal_Document_Access is access all LSP.Ada_Documents.Document;
115115

116+
procedure Free (Self : in out Internal_Document_Access);
117+
-- Free all the data for the given document.
118+
116119
-- Container for documents indexed by URI
117120
package Document_Maps is new Ada.Containers.Hashed_Maps
118121
(Key_Type => GNATCOLL.VFS.Virtual_File,

source/ada/lsp-diagnostic_sources.ads

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
-- of the license. --
1616
------------------------------------------------------------------------------
1717

18+
with Ada.Unchecked_Deallocation;
1819
with LSP.Messages;
1920
limited with LSP.Ada_Contexts;
2021

@@ -38,4 +39,7 @@ package LSP.Diagnostic_Sources is
3839
-- Return True if diagnostic changed since last call to Get_Diagnostic or
3940
-- if Get_Diagnostic was never called and any diagnostic presents.
4041

42+
procedure Unchecked_Free is new Ada.Unchecked_Deallocation
43+
(Diagnostic_Source'Class, Diagnostic_Source_Access);
44+
4145
end LSP.Diagnostic_Sources;

source/server/lsp-file_monitors.ads

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
-- This package provides a file monitoring service interface.
1919

2020
with GNATCOLL.VFS;
21+
with Ada.Unchecked_Deallocation;
2122

2223
package LSP.File_Monitors is
2324

@@ -43,4 +44,7 @@ package LSP.File_Monitors is
4344
-- Stop filesystem monitoring. This is a no-op if no monitoring is
4445
-- ongoing.
4546

47+
procedure Unchecked_Free is new Ada.Unchecked_Deallocation
48+
(File_Monitor'Class, File_Monitor_Access);
49+
4650
end LSP.File_Monitors;

0 commit comments

Comments
 (0)