www.digitalmars.com         C & C++   DMDScript  

D - opCall causes segfault

Linux (RH9) dmd 0.73

the following will not run (segfaults)
------------------------------
import c.stdio;
template ptr_to( C, R, P1 ) {
	class member_func {
		alias R delegate(P1) mfunc;
		mfunc tocall;
		this( mfunc initv ) {
			tocall = initv;
		}
		this() {
		}
		R opCall( C obj, P1 p1 ) {
			printf("in opcall\n");
			return 44;
		}
	}
}

class foo {
	alias instance ptr_to( foo, int, int ).member_func memberBar;
	
	static memberBar getMemberBar() {
		return new memberBar( &((new foo()).bar) );
	}
	
	int b;
	this( int b0 )  { b = b0; }
	this() { this(0); }
	int bar( int a ){
		printf( "this[0x%X](%d, %d)\n", cast(int)cast(void*)this, a, b );
		return a+b;
	}
}


int main( char[][] args ) {
	foo.memberBar func = foo.getMemberBar();
	foo f1 = new foo(1);
	printf("ok -1-\n");
	func( f1, 100 );
	printf("ok -2-\n");
	
	return 0;
}
-----------------------------
but this code does
-----------------------------
import c.stdio;
template ptr_to( C, R, P1 ) {
	class member_func {
		alias R delegate(P1) mfunc;
		mfunc tocall;
		this( mfunc initv ) {
			tocall = initv;
		}
		this() {
		}
		R opCall( C obj, P1 p1 ) {
			printf("in opcall\n");
			return 44;
		}
	}
}

class foo {
	alias instance ptr_to( foo, int, int ).member_func memberBar;
	
	static memberBar getMemberBar() {
		return new memberBar( );
	}
	static memberBar getBadMemberBar() {
		return new memberBar( &((new foo()).bar) );
	}
	
	int b;
	this( int b0 )  { b = b0; }
	this() { this(0); }
	int bar( int a ){
		printf( "this[0x%X](%d, %d)\n", cast(int)cast(void*)this, a, b );
		return a+b;
	}
}


int main( char[][] args ) {
	foo.memberBar func = foo.getMemberBar();
	foo f1 = new foo(1);
	printf("ok -1-\n");
	func( f1, 100 );
	printf("ok -2-\n");
	
	func = foo.getBadMemberBar();
	printf("ok -3-\n");
	func( f1, 100 );
	printf("ok -4-\n");
	
	return 0;
}
Sep 20 2003