Access Violation in callback from sort

Jabb jj.jabbaloo at gmail.com
Wed May 2 08:27:57 PDT 2012


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!


More information about the Digitalmars-d-learn mailing list