www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4593] New: (DMD 2.047) std.string.split + unittest build = Access Violation

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4593

           Summary: (DMD 2.047) std.string.split + unittest build = Access
                    Violation
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: epi atari8.info



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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 07 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4593


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com



11:32:05 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 07 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4593


Adrian Matoga <epi atari8.info> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Platform|Other                       |x86_64



Mhm, on XP SP3 it works fine for me also. But on Win7 (x86_64)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 07 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4593




12:50:29 PDT ---
I'm not sure if DMD is fully supported on x64 yet(?)

Someone else will have to fill in.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 07 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4593




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).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 07 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4593




14:00:10 PDT ---
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!");
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 07 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4593


Adrian Matoga <epi atari8.info> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED



Seems like it's fixed now with 2.048.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 11 2010