D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4593 - (DMD 2.047) Access Violation in unittest build
Summary: (DMD 2.047) Access Violation in unittest build
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-08-07 08:14 UTC by Adrian Matoga
Modified: 2010-08-11 04:54 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Adrian Matoga 2010-08-07 08:14:50 UTC
Hi,

I encountered a strange problem that appears when I build my project with -unittest switch on.
The executable fails giving a message "object.Error: Access Violation",
despite that without "-unittest" it works correctly.

I tried to successively cut different parts of code to find a source of problem, and I came up with an example which may be close to a minimal one.
It consists of two files, "a.d":

-------------------------
import std.string;
import std.stdio;

static string foo(string path)
{
	auto strs = path.split(".");
	return strs.length ? strs[$ - 1] : "";
}
-------------------------

and "b.d":

-------------------------
import std.stdio;
import a;

void main()
{
	writeln("Hello, world!");
}
-------------------------


Compile it using:
dmd -oftest.exe -unittest a.d b.d

and then launch it, and you'll see the "Access violation" message.
However, when you drop -unittest switch, or replace `path.split(".")' with, for example, `[ "" ]', or merge both sources together into a single source file, the program displays "Hello, world!" as expected.

I have no idea what could be the actual source of problem, but I hope the example can help fixing it.

Regards,
Adrian Matoga
Comment 1 Andrej Mitrovic 2010-08-07 11:32:05 UTC
I can't reproduce this on XP SP3, the output is fine for me:

b.d
--------------------------------
import std.stdio;
import a;

void main()
{
    writeln("Hello, world!");
    writeln(foo("some.path"));
}
--------------------------------

a.d
--------------------------------
import std.string;
import std.stdio;

static string foo(string path)
{
    auto strs = path.split(".");
    return strs.length ? strs[$ - 1] : "";
}
--------------------------------

C:\Test>dmd -oftest.exe -unittest a.d b.d

C:\Test>test
Hello, world!
path

Same thing when using a unittest block in b.d.
Comment 2 Adrian Matoga 2010-08-07 12:47:35 UTC
Mhm, on XP SP3 it works fine for me also. But on Win7 (x86_64)
Comment 3 Andrej Mitrovic 2010-08-07 12:50:29 UTC
I'm not sure if DMD is fully supported on x64 yet(?)

Someone else will have to fill in.
Comment 4 Adrian Matoga 2010-08-07 13:44:35 UTC
Hmm, this seems to be a lot more subtle thing.

Could you try the following:

image.d:
--------------------
import std.string;
import std.contracts;
import std.conv;

interface Image
{
	static Image open(string path, string type, bool readOnly = true)
	{
		auto img = newObj(path, type);
		img.openImpl(path, readOnly);
		return img;
	}

	static Image open(string path, bool readOnly = true)
	{
		return open(path, autoType(path), readOnly);
	}

protected:
	void openImpl(string path, bool readOnly);
	
private:
	static Image newObj(string path, string type)
	{
		auto image = cast(Image) Object.factory(tolower(type) ~ "." ~ capitalize(type) ~ "Image");
		version (unittest)
		{
			if (image is null)
				image = cast(Image) Object.factory("image." ~ capitalize(type) ~ "Image");
		}
		enforce(image, "Unknown image format: " ~ type);
		return image;
	}
	
	static string autoType(string path)
	{
		auto sp = path.split(".");
		return sp.length ? sp[$ - 1] : "";
	}
}
--------------------

main.d
--------------------
import std.stdio;

void main()
{
	writeln("Hello world!");
}
--------------------

dmd -oftest.exe -unittest main.d image.d

For me, this also fails on XP SP3 (x86).
Comment 5 Andrej Mitrovic 2010-08-07 14:00:10 UTC
You forgot to put an import to image in main.d . Without it, I'll get that object error thing. Otherwise, this will work fine:

import image;
import std.stdio;

void main()
{
   writeln("Hello world!");
}
Comment 6 Adrian Matoga 2010-08-11 04:54:30 UTC
Seems like it's fixed now with 2.048.