Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
May 02, 2012 Access Violation in callback from sort | ||||
---|---|---|---|---|
| ||||
Just got the TDPL book and it's a great read! I learn best when typing out the code myself, so I decided to make a single VisualD project and put the different exercises in separate modules. I am having problems with sort in std.algorithms - well, actually it appears to be a closure problem when sort calls my delegate. Here's a sample of the problem - in main.d I have //------------------ module main; import std.algorithm; void main() { uint[string] counts = [ "a":4, "b":5, "c":3, "d":1 ]; string[] keys = counts.keys; sort!((a, b) { return counts[a] > counts[b]; })(keys); } //------------------ Alone this works just fine. But if I add another file called myalgs.d, and put the following in it: //------------------ module myalgs; import std.algorithm; //------------------ Then I get the following exception: object.Error: Access Violation ---------------- C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(7112): mainmain__T8sortImplS14main9__lambda3VE3std9algorithm12SwapStrategy0TAAyaZsortImpl ---------------- Steping through the code it appears that the Access Violation occurs when accessing counts[a]. I am using DMD32 v2.059; my compilation command line looks like: dmd -g -debug -X -Xf"$(IntDir)\$(TargetName).json" -deps="$(OutDir)\$(ProjectName).dep" -of"$(OutDir)\$(ProjectName).exe_cv" -map "$(INTDIR)\$(SAFEPROJECTNAME).map" -L/NOMAP Am I doing something wrong here? thanks for the help! |
May 03, 2012 Re: Access Violation in callback from sort | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jabb | On Wed, 02 May 2012 11:27:57 -0400, Jabb <jj.jabbaloo@gmail.com> wrote: > Just got the TDPL book and it's a great read! I learn best when typing out the code myself, so I decided to make a single VisualD project and put the different exercises in separate modules. I am having problems with sort in std.algorithms - well, actually it appears to be a closure problem when sort calls my delegate. There shouldn't be any closure here, sort is not storing the delegate pointer. > Here's a sample of the problem - in main.d I have > > //------------------ > module main; > import std.algorithm; > void main() { > uint[string] counts = [ "a":4, "b":5, "c":3, "d":1 ]; > string[] keys = counts.keys; > > sort!((a, b) { return counts[a] > counts[b]; })(keys); > } > //------------------ > > Alone this works just fine. But if I add another file called myalgs.d, and put the following in it: > > //------------------ > module myalgs; > import std.algorithm; > //------------------ > > Then I get the following exception: > > object.Error: Access Violation > ---------------- > C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(7112): mainmain__T8sortImplS14main9__lambda3VE3std9algorithm12SwapStrategy0TAAyaZsortImpl > ---------------- > > Steping through the code it appears that the Access Violation occurs when accessing counts[a]. > > I am using DMD32 v2.059; my compilation command line looks like: > dmd -g -debug -X -Xf"$(IntDir)\$(TargetName).json" -deps="$(OutDir)\$(ProjectName).dep" -of"$(OutDir)\$(ProjectName).exe_cv" -map "$(INTDIR)\$(SAFEPROJECTNAME).map" -L/NOMAP I can't see any errors, I think this is worthy of a bug report, it's nice and small and self-contained. http://d.puremagic.com/issues -Steve |
March 21, 2013 Re: Access Violation in callback from sort | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jabb | On Wednesday, 2 May 2012 at 15:27:58 UTC, Jabb wrote:
> import std.algorithm;
> void main() {
> uint[string] counts = [ "a":4, "b":5, "c":3, "d":1 ];
> string[] keys = counts.keys;
>
> sort!((a, b) { return counts[a] > counts[b]; })(keys);
> }
Did you try
string[] keys = counts.keys.dup;
?
Sometimes you get weird aliasing issues, as the sort! template
modifies the original array.
|
March 21, 2013 Re: Access Violation in callback from sort | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan | On 03/21/2013 01:02 PM, Stefan wrote: > On Wednesday, 2 May 2012 at 15:27:58 UTC, Jabb wrote: > >> import std.algorithm; >> void main() { >> uint[string] counts = [ "a":4, "b":5, "c":3, "d":1 ]; >> string[] keys = counts.keys; >> >> sort!((a, b) { return counts[a] > counts[b]; })(keys); >> } > > Did you try > string[] keys = counts.keys.dup; > ? That should be ok. Unlike .byKey, .keys is eager; all of the keys are in the returned slice by the time it returns. Ali |
March 21, 2013 Re: Access Violation in callback from sort | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jabb | On 05/02/2012 08:27 AM, Jabb wrote: > Alone this works just fine. But if I add another file called myalgs.d, > and put the following in it: > > //------------------ > module myalgs; > import std.algorithm; > //------------------ > > Then I get the following exception: > > object.Error: Access Violation I can reproduce the bug with 2.062 as well as an earlier development build of 2.063 (a fresher dmd does not build at the moment.) I have found a workaround: Put the second file later in the dmd command line: dmd main.d myalgs.d Ali |
Copyright © 1999-2021 by the D Language Foundation