Skip to content

Commit 65a0328

Browse files
committed
Fixed ConstantExpressionHelper.cs (#167)
1 parent e7cdd1a commit 65a0328

File tree

6 files changed

+168
-7
lines changed

6 files changed

+168
-7
lines changed

System.Linq.Dynamic.Core.sln

+19
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp_net452_EF6", "sr
5151
EndProject
5252
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp_netcore2.0_EF2.0.2_InMemory", "src-console\ConsoleAppEF2.0.2_InMemory\ConsoleApp_netcore2.0_EF2.0.2_InMemory.csproj", "{437473EE-7FBB-4C28-96EC-41E1AEE161F3}"
5353
EndProject
54+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MemoryLeakTest167", "src-console\MemoryLeakTest167\MemoryLeakTest167.csproj", "{AD4F83E5-4240-485D-BB5C-F43974F716E4}"
55+
EndProject
5456
Global
5557
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5658
Debug|Any CPU = Debug|Any CPU
@@ -269,6 +271,22 @@ Global
269271
{437473EE-7FBB-4C28-96EC-41E1AEE161F3}.Release|x64.Build.0 = Release|Any CPU
270272
{437473EE-7FBB-4C28-96EC-41E1AEE161F3}.Release|x86.ActiveCfg = Release|Any CPU
271273
{437473EE-7FBB-4C28-96EC-41E1AEE161F3}.Release|x86.Build.0 = Release|Any CPU
274+
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
275+
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
276+
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Debug|ARM.ActiveCfg = Debug|Any CPU
277+
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Debug|ARM.Build.0 = Debug|Any CPU
278+
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Debug|x64.ActiveCfg = Debug|Any CPU
279+
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Debug|x64.Build.0 = Debug|Any CPU
280+
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Debug|x86.ActiveCfg = Debug|Any CPU
281+
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Debug|x86.Build.0 = Debug|Any CPU
282+
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
283+
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Release|Any CPU.Build.0 = Release|Any CPU
284+
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Release|ARM.ActiveCfg = Release|Any CPU
285+
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Release|ARM.Build.0 = Release|Any CPU
286+
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Release|x64.ActiveCfg = Release|Any CPU
287+
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Release|x64.Build.0 = Release|Any CPU
288+
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Release|x86.ActiveCfg = Release|Any CPU
289+
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Release|x86.Build.0 = Release|Any CPU
272290
EndGlobalSection
273291
GlobalSection(SolutionProperties) = preSolution
274292
HideSolutionNode = FALSE
@@ -287,6 +305,7 @@ Global
287305
{E587974C-19A9-426A-A952-216D5F883EB6} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
288306
{5DC68E83-ABE0-4887-B17E-1ED4EEE89C2C} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
289307
{437473EE-7FBB-4C28-96EC-41E1AEE161F3} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
308+
{AD4F83E5-4240-485D-BB5C-F43974F716E4} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
290309
EndGlobalSection
291310
GlobalSection(ExtensibilityGlobals) = postSolution
292311
SolutionGuid = {94C56722-194E-4B8B-BC23-B3F754E89A20}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
5+
</startup>
6+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{AD4F83E5-4240-485D-BB5C-F43974F716E4}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>MemoryLeakTest167</RootNamespace>
10+
<AssemblyName>MemoryLeakTest167</AssemblyName>
11+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="System" />
36+
<Reference Include="System.Core" />
37+
<Reference Include="System.Xml.Linq" />
38+
<Reference Include="System.Data.DataSetExtensions" />
39+
<Reference Include="Microsoft.CSharp" />
40+
<Reference Include="System.Data" />
41+
<Reference Include="System.Net.Http" />
42+
<Reference Include="System.Xml" />
43+
</ItemGroup>
44+
<ItemGroup>
45+
<Compile Include="Program.cs" />
46+
<Compile Include="Properties\AssemblyInfo.cs" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<None Include="App.config" />
50+
</ItemGroup>
51+
<ItemGroup>
52+
<ProjectReference Include="..\..\src\System.Linq.Dynamic.Core\System.Linq.Dynamic.Core.csproj">
53+
<Project>{b70cd050-3b9c-406c-85fd-1a26394ec7c8}</Project>
54+
<Name>System.Linq.Dynamic.Core</Name>
55+
</ProjectReference>
56+
</ItemGroup>
57+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
58+
</Project>
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Linq.Dynamic.Core;
3+
using System.Linq.Expressions;
4+
using System.Threading;
5+
6+
namespace MemoryLeakTest167
7+
{
8+
class Program
9+
{
10+
static void Main(string[] asArgs)
11+
{
12+
RunThreads(8, RawMemoryLeak);
13+
}
14+
15+
private static void RunThreads(int nThreads, Action actThreadStart)
16+
{
17+
for (int i = 0; i < nThreads; i++)
18+
{
19+
Thread thread = new Thread(new ThreadStart(actThreadStart));
20+
thread.Start();
21+
}
22+
23+
while (true)
24+
{
25+
Thread.Sleep(5000);
26+
}
27+
}
28+
29+
private static void RawMemoryLeak()
30+
{
31+
while (true)
32+
{
33+
string sExpr = "1234567890";
34+
LambdaExpression expr = DynamicExpressionParser.ParseLambda(new ParameterExpression[0], typeof(double), sExpr);
35+
}
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Reflection;
2+
using System.Runtime.InteropServices;
3+
4+
// General Information about an assembly is controlled through the following
5+
// set of attributes. Change these attribute values to modify the information
6+
// associated with an assembly.
7+
[assembly: AssemblyTitle("MemoryLeakTest167")]
8+
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyProduct("MemoryLeakTest167")]
12+
[assembly: AssemblyCopyright("Copyright © Stef Heyenrath 2018")]
13+
[assembly: AssemblyTrademark("")]
14+
[assembly: AssemblyCulture("")]
15+
16+
// Setting ComVisible to false makes the types in this assembly not visible
17+
// to COM components. If you need to access a type in this assembly from
18+
// COM, set the ComVisible attribute to true on that type.
19+
[assembly: ComVisible(false)]
20+
21+
// The following GUID is for the ID of the typelib if this project is exposed to COM
22+
[assembly: Guid("ad4f83e5-4240-485d-bb5c-f43974f716e4")]
23+
24+
// Version information for an assembly consists of the following four values:
25+
//
26+
// Major Version
27+
// Minor Version
28+
// Build Number
29+
// Revision
30+
//
31+
// You can specify all the values or you can default the Build and Revision Numbers
32+
// by using the '*' as shown below:
33+
// [assembly: AssemblyVersion("1.0.*")]
34+
[assembly: AssemblyVersion("1.0.0.0")]
35+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
using System.Collections.Concurrent;
2-
using System.Collections.Generic;
32
using System.Linq.Expressions;
43

54
namespace System.Linq.Dynamic.Core.Parser
65
{
76
internal static class ConstantExpressionHelper
87
{
9-
private static readonly IDictionary<Expression, string> Literals = new ConcurrentDictionary<Expression, string>();
8+
private static readonly ConcurrentDictionary<object, Expression> Expressions = new ConcurrentDictionary<object, Expression>();
9+
private static readonly ConcurrentDictionary<Expression, string> Literals = new ConcurrentDictionary<Expression, string>();
1010

11-
public static bool TryGetText(Expression expresion, out string text)
11+
public static bool TryGetText(Expression expression, out string text)
1212
{
13-
return Literals.TryGetValue(expresion, out text);
13+
return Literals.TryGetValue(expression, out text);
1414
}
1515

1616
public static Expression CreateLiteral(object value, string text)
1717
{
18-
ConstantExpression expresion = Expression.Constant(value);
18+
if (!Expressions.ContainsKey(value))
19+
{
20+
ConstantExpression constantExpression = Expression.Constant(value);
1921

20-
Literals.Add(expresion, text);
21-
return expresion;
22+
Expressions.TryAdd(value, constantExpression);
23+
Literals.TryAdd(constantExpression, text);
24+
}
25+
26+
return Expressions[value];
2227
}
2328
}
2429
}

0 commit comments

Comments
 (0)