D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7244 - Access Violation with optimized build when using memory-mapped file
Summary: Access Violation with optimized build when using memory-mapped file
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: Other Windows
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-01-07 19:53 UTC by Andrej Mitrovic
Modified: 2016-08-27 22:31 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 Andrej Mitrovic 2012-01-07 19:53:30 UTC
I am *not* sure if the following code is correct, it's copied from another source and I've never used memory-mapped files before:

module test;

import std.algorithm;
import std.mmfile;

void readfile(string filename)
{
    string[] lines;
    auto fFile   = new MmFile(filename);
    auto fBuffer = cast(char*)(fFile[].ptr);
    auto fLength = cast(size_t)fFile.length;
    auto textData = fBuffer[0 .. fLength];
    
    foreach (char[] line; splitter(textData, "\n"))
        lines ~= line.idup;
}

void main() {
    readfile("datetime.d");
}

This only happens when reading large files like std.datetime:
$ dmd test.d && test.exe
ok

$ dmd -O test.d && test.exe
object.Error: Access Violation
----------------
----------------

ddbg says:
Unhandled Exception: EXCEPTION_ACCESS_VIOLATION(0xc0000005) at std.algorithm.find!(pred,ubyte[],ubyte[]).find D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d:2885 (0x00402b23) thread(2408)

That's why I've labeled is as a Phobos bug. But maybe it's a bug in the user code?
Comment 1 SomeDude 2012-04-21 02:55:08 UTC
Note that this executes correctly (even with -O) when we replace "\n" (LF) by "\r\n" (CR+LF) under Windows:

    foreach (char[] line; splitter(textData, "\r\n"))
        lines ~= line.idup;

Yet datetime.d is in Unix format according to my editor !
Comment 2 SomeDude 2012-04-21 06:28:21 UTC
See also issue 7689
Comment 3 Andrej Mitrovic 2016-08-27 22:31:50 UTC
Works with latest DMD.