C# System.AccessViolationException

1,386 views
Skip to first unread message

Chanan Zupnick

unread,
Nov 17, 2014, 6:11:57 PM11/17/14
to or-tools...@googlegroups.com
Hey, I am attempting to use the .NET DLL for VS 2013 and I keep getting a System.AccessViolationException whenever I call solver.NextSolution().
The message says "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." Below is the stack trace

I compiled my application for both x64 and Mixed Platform, and got  the error on both. Any help on how to further investigate / fix the issue would be greatly appreciated.

Thanks .

   at Google.OrTools.ConstraintSolver.operations_research_constraint_solverPINVOKE.Solver_NextSolution(HandleRef jarg1)
   at Google.OrTools.ConstraintSolver.Solver.NextSolution()
   at SchedulerFramework.CohortBlockScheduling.CohortSchedulerBase.linearOptimize(SchedulingPeriodData scenarioPeriodData, Int32 periodNumber) in c:\Users\Channan\Source\Repos\LearningAssigner\ConsoleProject\CohortBlockScheduling\SchedulingAlgorithmDetails.cs:line 1318
   at SchedulerFramework.CohortBlockScheduling.CohortSchedulerBase.GenerateCandidateStudentAssignments(Int32 periodNumber, SchedulingPeriodData periodData, Boolean createTaskAssignmentsForNonPupStudents) in c:\Users\Channan\Source\Repos\LearningAssigner\ConsoleProject\CohortBlockScheduling\CohortSchedulers.cs:line 183
   at SchedulerFramework.CohortBlockScheduling.CohortSchedulerBase.<>c__DisplayClass13.<Run>b__4(SchedulingScenarioDictionary scenario) in c:\Users\Channan\Source\Repos\LearningAssigner\ConsoleProject\CohortBlockScheduling\CohortSchedulers.cs:line 103
   at SchedulerFramework.CohortBlockScheduling.CohortSchedulerBase.<>c__DisplayClass13.<Run>b__8(SchedulingScenarioDictionary scenario) in c:\Users\Channan\Source\Repos\LearningAssigner\ConsoleProject\CohortBlockScheduling\CohortSchedulers.cs:line 127
   at System.Collections.Generic.List`1.ForEach(Action`1 action)
   at SchedulerFramework.CohortBlockScheduling.CohortSchedulerBase.<Run>d__18.MoveNext() in c:\Users\Channan\Source\Repos\LearningAssigner\ConsoleProject\CohortBlockScheduling\CohortSchedulers.cs:line 127
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
   at SchedulerFramework.CohortBlockScheduling.CohortSchedulerBase.Run()
   at SchedulerFramework.ScheduleGenerator.<>c__DisplayClasse.<>c__DisplayClass13.<<GenerateSchedules>b__7>d__15.MoveNext() in c:\Users\Channan\Source\Repos\LearningAssigner\ConsoleProject\Program.cs:line 127
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
   at SchedulerFramework.ScheduleGenerator.<>c__DisplayClasse.<>c__DisplayClass13.<GenerateSchedules>b__7()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
   at System.Threading.Tasks.Task.ExecuteEntry(Boolean bPreventDoubleExecution)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

Otto Dandenell

unread,
Nov 18, 2014, 3:00:34 AM11/18/14
to or-tools...@googlegroups.com

Chanan Zupnick skrev den 2014-11-18 00:11:
> Hey, I am attempting to use the .NET DLL for VS 2013 and I keep getting
> a System.AccessViolationException whenever I call solver.NextSolution().
> The message says "Attempted to read or write protected memory. This is
> often an indication that other memory is corrupt." Below is the stack trace

Hi,

You need to keep a reference in your c# client to any Constraints or
other CLR-created objects which you add to the solver. Otherwise, your
CLR-created objects may be disposed by the garbage collector.

This is a consequence of mixing native code with managed code.

Regards

/ Otto Dandenell

Chanan Zupnick

unread,
Nov 20, 2014, 5:37:28 PM11/20/14
to or-tools...@googlegroups.com
Thanks that was exactly it.

Michael Powell

unread,
Nov 22, 2014, 1:17:04 PM11/22/14
to or-tools...@googlegroups.com


On Tuesday, November 18, 2014 2:00:34 AM UTC-6, Otto Dandenell wrote:

Chanan Zupnick skrev den 2014-11-18 00:11:
> Hey, I am attempting to use the .NET DLL for VS 2013 and I keep getting
> a System.AccessViolationException whenever I call solver.NextSolution().
> The message says "Attempted to read or write protected memory. This is
> often an indication that other memory is corrupt." Below is the stack trace

Hi,

You need to keep a reference in your c# client to any Constraints or
other CLR-created objects which you add to the solver. Otherwise, your
CLR-created objects may be disposed by the garbage collector.

I am keeping variables. Will track the others also. Could just be in a 'simple' object collection?
 
This is a consequence of mixing native code with managed code.

Got it, thank you...
 
Regards

/ Otto Dandenell

Michael Powell

unread,
Nov 22, 2014, 1:35:34 PM11/22/14
to or-tools...@googlegroups.com


On Saturday, November 22, 2014 12:17:04 PM UTC-6, Michael Powell wrote:


On Tuesday, November 18, 2014 2:00:34 AM UTC-6, Otto Dandenell wrote:

Chanan Zupnick skrev den 2014-11-18 00:11:
> Hey, I am attempting to use the .NET DLL for VS 2013 and I keep getting
> a System.AccessViolationException whenever I call solver.NextSolution().
> The message says "Attempted to read or write protected memory. This is
> often an indication that other memory is corrupt." Below is the stack trace

Hi,

You need to keep a reference in your c# client to any Constraints or
other CLR-created objects which you add to the solver. Otherwise, your
CLR-created objects may be disposed by the garbage collector.

I am keeping variables. Will track the others also. Could just be in a 'simple' object collection?

I'm not sure what else I need to track? I included IntExpr and constraints in the Clr-created-objects.

My impression was reading the documentation that Solver, sometimes DecisionBuilder, etc, assumes ownership of these things? Is that not the case?

Michael Powell

unread,
Nov 22, 2014, 1:46:31 PM11/22/14
to or-tools...@googlegroups.com


On Saturday, November 22, 2014 12:35:34 PM UTC-6, Michael Powell wrote:


On Saturday, November 22, 2014 12:17:04 PM UTC-6, Michael Powell wrote:


On Tuesday, November 18, 2014 2:00:34 AM UTC-6, Otto Dandenell wrote:

Chanan Zupnick skrev den 2014-11-18 00:11:
> Hey, I am attempting to use the .NET DLL for VS 2013 and I keep getting
> a System.AccessViolationException whenever I call solver.NextSolution().
> The message says "Attempted to read or write protected memory. This is
> often an indication that other memory is corrupt." Below is the stack trace

Hi,

You need to keep a reference in your c# client to any Constraints or
other CLR-created objects which you add to the solver. Otherwise, your
CLR-created objects may be disposed by the garbage collector.

I am keeping variables. Will track the others also. Could just be in a 'simple' object collection?

I'm not sure what else I need to track? I included IntExpr and constraints in the Clr-created-objects.

I'm not sure how liberal I need to be in that treatment: i.e. including the callbacks? including any and all expressions and constraints used to prepare the solver problem?

It's simple enough to add, then clear when my solver-wrapper disposes.

That gets me past this particular issue...

Michael Powell

unread,
Nov 23, 2014, 12:33:55 PM11/23/14
to or-tools...@googlegroups.com


On Tuesday, November 18, 2014 2:00:34 AM UTC-6, Otto Dandenell wrote:

Chanan Zupnick skrev den 2014-11-18 00:11:
> Hey, I am attempting to use the .NET DLL for VS 2013 and I keep getting
> a System.AccessViolationException whenever I call solver.NextSolution().
> The message says "Attempted to read or write protected memory. This is
> often an indication that other memory is corrupt." Below is the stack trace

Hi,

You need to keep a reference in your c# client to any Constraints or
other CLR-created objects which you add to the solver. Otherwise, your
CLR-created objects may be disposed by the garbage collector.

Let's say a governing using statement creates a new Solver, within which block all the configuration and solutions are taking place.

It is possible to say: (?)

GC.SuppressFinalize(s);

As long as at the end of the block, in a try/finally clause, let's say, one remembers to un-suppress finalizers?
 

Laurent Perron

unread,
Nov 23, 2014, 12:40:38 PM11/23/14
to or-tools...@googlegroups.com
actually, 

any object created by the solver factory belong to the solver.
You need to store objects you create yourself and pass to the solver, that is:
  - custom decision builders
  - custom evaluators
  - custom search monitors

and so on.

I hope this helps.

--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Powell

unread,
Nov 23, 2014, 12:50:32 PM11/23/14
to or-tools...@googlegroups.com
On Sun, Nov 23, 2014 at 11:40 AM, Laurent Perron
<laurent...@gmail.com> wrote:
> actually,
>
> any object created by the solver factory belong to the solver.
> You need to store objects you create yourself and pass to the solver, that
> is:
> - custom decision builders
> - custom evaluators
> - custom search monitors
>
> and so on.
>
> I hope this helps.

Makes sense. Thanks...
> You received this message because you are subscribed to a topic in the
> Google Groups "or-tools-discuss" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/or-tools-discuss/ChDSqVJg0NU/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to

Pablo Hernán Giménez

unread,
Sep 30, 2015, 5:25:34 PM9/30/15
to or-tools-discuss
Hi, 

I'm having the same problem: System.AccessViolationException whenever I call RoutingModel.Solve. I'm also using  .NET DLL for VS 2013.
When you say that I should keep a reference to objects passed to the solver you mean using something like:

            this.cost = new SimpleDistanceEvaluator(this.locations);
            routingModel.SetCost(cost);

Instead of:

            routingModel.SetCost(new SimpleDistanceEvaluator(this.locations));

Right? Or is it something different?

Thanks

Pablo Hernán Giménez

unread,
Oct 2, 2015, 5:08:46 PM10/2/15
to or-tools-discuss
I found that System.AccessViolationException occurs only when I enable LNS:

If I set:

parameters.no_lns = true; in the RoutingSearchParameters it works OK.

Laurent,

Using the example you shared with me:
If you enable LNS (comment out parameters.no_lns = true;) and put it in a loop (while(true)), you can reproduce the exception. Could this be a bug?

Karol Kowalski

unread,
Jul 27, 2016, 4:14:18 AM7/27/16
to or-tools-discuss
Same issue here.

If I set:
       _searchParameters.LocalSearchOperators.UsePathLns = true;
       _searchParameters.LocalSearchOperators.UseFullPathLns = true;
       _searchParameters.LocalSearchOperators.UseInactiveLns = true;

I get System.AccessViolationException

But if i set:
       _searchParameters.LocalSearchOperators.UsePathLns = false;
       _searchParameters.LocalSearchOperators.UseFullPathLns = false;
       _searchParameters.LocalSearchOperators.UseInactiveLns = false;

everything work fine.

Driss Lahlou

unread,
Jul 28, 2016, 11:36:30 AM7/28/16
to or-tools-discuss
Hello Karol,

I tried to run cscvrptw example with the parameters you suggested on 64 windows machine, with both visual studio 2013 and 2015, and it worked fine for me. Can you please share your code with you, so I try it on my machine?

Regards,
Reply all
Reply to author
Forward
0 new messages