www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Access Violation in callback from sort

reply "Jabb" <jj.jabbaloo gmail.com> writes:
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 02 2012
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
May 03 2012