Skip to content
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

Fix csharp cscvrptw example. Protect callbacks from GC. #5

Merged
merged 1 commit into from
Jan 20, 2018

Conversation

narfunikita
Copy link
Contributor

When we invoke SolveWithParameters of RoutingModel CLR GC can start collecting.
If it's happens, we get exception when callbacks invoked because GC think that delegates won't be used and clear objects.

Exception: CallbackOnCollectedDelegate
A callback was made on a garbage collected delegate of type 'Google.OrTools!Google.OrTools.ConstraintSolver.NodeEvaluator2+SwigDelegateNodeEvaluator2_0::Invoke'. This may cause application crashes, corruption and

data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.

For fix use GC.KeepAlive method.
Here example of this solution with delegates and unmanaged code.
https://msdn.microsoft.com/en-us/library/system.gc.keepalive(v=vs.110).aspx

@orwant orwant mentioned this pull request Jan 11, 2016
@braktar braktar mentioned this pull request Mar 9, 2017
//protect callbacks from the GC
GC.KeepAlive(manhattan_callback);
GC.KeepAlive(demand_callback);
for (int cost_callback_index = 0; cost_callback_index < cost_callbacks.Length; cost_callback_index++)
Copy link
Collaborator

Choose a reason for hiding this comment

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

"{" should be at the end on line for consistency

//protect callbacks from the GC
GC.KeepAlive(manhattan_callback);
GC.KeepAlive(demand_callback);
for (int cost_callback_index = 0; cost_callback_index < cost_callbacks.Length; cost_callback_index++) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Mizux Fixed

"{" should be at the end on line for consistenc

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks !

@Mizux Mizux merged commit 4e7dfa3 into google:master Jan 20, 2018
@Mizux Mizux self-assigned this Oct 26, 2020
@Mizux Mizux added this to the v6.8 milestone Sep 14, 2021
GerHobbelt pushed a commit to GerHobbelt/or-tools that referenced this pull request May 23, 2024
AT-8842: Fix `go.Dockerfile` for export
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