Skip to content
This repository was archived by the owner on Mar 1, 2022. It is now read-only.

Commit bc2bba9

Browse files
authored
Merge pull request #100 from dayllenger/memory-leaks
Fix some memory leak issues
2 parents acd5676 + 39a1754 commit bc2bba9

File tree

5 files changed

+23
-12
lines changed

5 files changed

+23
-12
lines changed

source/workspaced/api.d

+14-7
Original file line numberDiff line numberDiff line change
@@ -996,11 +996,14 @@ package string getVersionAndFixPath(ref string execPath)
996996

997997
class Future(T)
998998
{
999+
import core.thread : Fiber, Thread;
1000+
9991001
static if (!is(T == void))
10001002
T value;
10011003
Throwable exception;
10021004
bool has;
10031005
void delegate() _onDone;
1006+
private Thread _worker;
10041007

10051008
/// Sets the onDone callback if no value has been set yet or calls immediately if the value has already been set or was set during setting the callback.
10061009
/// Crashes with an assert error if attempting to override an existing callback (i.e. calling this function on the same object twice).
@@ -1036,10 +1039,8 @@ class Future(T)
10361039

10371040
static Future!T async(T delegate() cb)
10381041
{
1039-
import core.thread : Thread;
1040-
10411042
auto ret = new Future!T;
1042-
new Thread({
1043+
ret._worker = new Thread({
10431044
try
10441045
{
10451046
static if (is(T == void))
@@ -1096,10 +1097,13 @@ class Future(T)
10961097
/// Waits for the result of this future using Thread.sleep
10971098
T getBlocking(alias sleepDur = 1.msecs)()
10981099
{
1099-
import core.thread : Thread;
1100-
11011100
while (!has)
11021101
Thread.sleep(sleepDur);
1102+
if (_worker)
1103+
{
1104+
_worker.join();
1105+
_worker = null;
1106+
}
11031107
if (exception)
11041108
throw exception;
11051109
static if (!is(T == void))
@@ -1109,10 +1113,13 @@ class Future(T)
11091113
/// Waits for the result of this future using Fiber.yield
11101114
T getYield()
11111115
{
1112-
import core.thread : Fiber;
1113-
11141116
while (!has)
11151117
Fiber.yield();
1118+
if (_worker)
1119+
{
1120+
_worker.join();
1121+
_worker = null;
1122+
}
11161123
if (exception)
11171124
throw exception;
11181125
static if (!is(T == void))

source/workspaced/com/dcdext.d

+3-1
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ class DCDExtComponent : ComponentWrapper
350350
/// See_Also: CodeBlockInfo
351351
CodeBlockInfo getCodeBlockRange(scope const(char)[] code, int position)
352352
{
353+
RollbackAllocator rba;
353354
auto tokens = getTokensForParser(cast(ubyte[]) code, config, &workspaced.stringCache);
354355
auto parsed = parseModule(tokens, "getCodeBlockRange_input.d", &rba);
355356
auto reader = new CodeBlockInfoFinder(position);
@@ -367,6 +368,7 @@ class DCDExtComponent : ComponentWrapper
367368

368369
scope const(char)[] codeBlock = code[container.innerRange[0] .. container.innerRange[1]];
369370

371+
RollbackAllocator rba;
370372
scope tokensInsert = getTokensForParser(cast(ubyte[]) insert, config,
371373
&workspaced.stringCache);
372374
scope parsedInsert = parseModule(tokensInsert, "insertCode_insert.d", &rba);
@@ -790,6 +792,7 @@ class DCDExtComponent : ComponentWrapper
790792
/// Extracts information about a given class or interface at the given position.
791793
InterfaceDetails getInterfaceDetails(string file, scope const(char)[] code, int position)
792794
{
795+
RollbackAllocator rba;
793796
auto tokens = getTokensForParser(cast(ubyte[]) code, config, &workspaced.stringCache);
794797
auto parsed = parseModule(tokens, file, &rba);
795798
auto reader = new InterfaceMethodFinder(code, position);
@@ -843,7 +846,6 @@ class DCDExtComponent : ComponentWrapper
843846
}
844847

845848
private:
846-
RollbackAllocator rba;
847849
LexerConfig config;
848850
}
849851

source/workspaced/com/importer.d

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class ImporterComponent : ComponentWrapper
3131
/// Returns all imports available at some code position.
3232
ImportInfo[] get(scope const(char)[] code, int pos)
3333
{
34+
RollbackAllocator rba;
3435
auto tokens = getTokensForParser(cast(ubyte[]) code, config, &workspaced.stringCache);
3536
auto mod = parseModule(tokens, "code", &rba);
3637
auto reader = new ImporterReaderVisitor(pos);
@@ -43,6 +44,7 @@ class ImporterComponent : ComponentWrapper
4344
ImportModification add(string importName, scope const(char)[] code, int pos,
4445
bool insertOutermost = true)
4546
{
47+
RollbackAllocator rba;
4648
auto tokens = getTokensForParser(cast(ubyte[]) code, config, &workspaced.stringCache);
4749
auto mod = parseModule(tokens, "code", &rba);
4850
auto reader = new ImporterReaderVisitor(pos);
@@ -173,6 +175,7 @@ class ImporterComponent : ComponentWrapper
173175

174176
part = code[start .. end];
175177

178+
RollbackAllocator rba;
176179
auto tokens = getTokensForParser(cast(ubyte[]) part, config, &workspaced.stringCache);
177180
auto mod = parseModule(tokens, "code", &rba);
178181
auto reader = new ImporterReaderVisitor(-1);
@@ -198,7 +201,6 @@ class ImporterComponent : ComponentWrapper
198201
}
199202

200203
private:
201-
RollbackAllocator rba;
202204
LexerConfig config;
203205
}
204206

source/workspaced/com/moduleman.d

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ModulemanComponent : ComponentWrapper
3434
if (!refInstance)
3535
throw new Exception("moduleman.rename requires to be instanced");
3636

37+
RollbackAllocator rba;
3738
FileChanges[] changes;
3839
bool foundModule = false;
3940
auto from = mod.split('.');
@@ -170,7 +171,6 @@ class ModulemanComponent : ComponentWrapper
170171
}
171172

172173
private:
173-
RollbackAllocator rba;
174174
LexerConfig config;
175175
}
176176

source/workspaced/com/snippets/package.d

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class SnippetsComponent : ComponentWrapper
4848
providers ~= dependencySnippets;
4949
}
5050

51-
/**
51+
/**
5252
* Params:
5353
* file = Filename to resolve dependencies relatively from.
5454
* code = Code to complete snippet in.
@@ -95,6 +95,7 @@ class SnippetsComponent : ComponentWrapper
9595
}
9696
}
9797

98+
RollbackAllocator rba;
9899
scope parsed = parseModule(tokens, cast(string) file, &rba);
99100

100101
trace("determineSnippetInfo at ", position);
@@ -435,7 +436,6 @@ private:
435436
return tuple(info, futures.data);
436437
}
437438

438-
RollbackAllocator rba;
439439
LexerConfig config;
440440
}
441441

0 commit comments

Comments
 (0)