Jump to page: 1 2
Thread overview
std.stdio.File is throwing with the message of: "Access Violation"
Aug 18, 2021
Ruby The Roobster
Aug 18, 2021
Paul Backus
Aug 18, 2021
Ruby The Roobster
Aug 20, 2021
Kagamin
Aug 18, 2021
Ruby The Roobster
Aug 19, 2021
Jesse Phillips
Aug 19, 2021
Ruby The Roobster
Aug 20, 2021
Jesse Phillips
Aug 20, 2021
nov
Aug 20, 2021
Ruby The Roobster
Aug 20, 2021
jfondren
Aug 21, 2021
evilrat
Aug 21, 2021
Ruby The Roobster
Aug 21, 2021
Ruby The Roobster
Aug 22, 2021
evilrat
Aug 21, 2021
Adam D Ruppe
August 18, 2021

All I did was try to access a file with a self-made library. It didn't work. I tried again directly from the main file. This is the code:

File file = File("E:\\Users\\User\\Desktop\\dutils\\test.spr","r"); //This file exists on my system, so it should work...
file.close();

Output(Given to me by a message box that display's Throwable.msg in it's body):
Access Violation

Is this a bug, or me being stupid? If it's the latter, than tell me what went wrong. I am using DMD 2.097.2

August 18, 2021

On Wednesday, 18 August 2021 at 17:42:53 UTC, Ruby The Roobster wrote:

>

Output(Given to me by a message box that display's Throwable.msg in it's body):
Access Violation

Is this a bug, or me being stupid? If it's the latter, than tell me what went wrong. I am using DMD 2.097.2

It's a bug in your code. "Access Violation" means your program tried to access out-of-bounds memory.

August 18, 2021

On Wednesday, 18 August 2021 at 17:54:47 UTC, Paul Backus wrote:

>

On Wednesday, 18 August 2021 at 17:42:53 UTC, Ruby The Roobster wrote:

>

Output(Given to me by a message box that display's Throwable.msg in it's body):
Access Violation

Is this a bug, or me being stupid? If it's the latter, than tell me what went wrong. I am using DMD 2.097.2

It's a bug in your code. "Access Violation" means your program tried to access out-of-bounds memory.

When I removed those two lines of code, the program ran perfectly without displaying any error or throwing any exception...

August 18, 2021

On Wednesday, 18 August 2021 at 17:54:47 UTC, Paul Backus wrote:

>

On Wednesday, 18 August 2021 at 17:42:53 UTC, Ruby The Roobster wrote:

>

Output(Given to me by a message box that display's Throwable.msg in it's body):
Access Violation

Is this a bug, or me being stupid? If it's the latter, than tell me what went wrong. I am using DMD 2.097.2

It's a bug in your code. "Access Violation" means your program tried to access out-of-bounds memory.

In addition, I figured out that if I moved the code outside of WndProc() everything worked fine, so I think it's a Win32 issue.

August 19, 2021

On Wednesday, 18 August 2021 at 17:42:53 UTC, Ruby The Roobster wrote:

>

All I did was try to access a file with a self-made library. It didn't work. I tried again directly from the main file. This is the code:

File file = File("E:\\Users\\User\\Desktop\\dutils\\test.spr","r"); //This file exists on my system, so it should work...
file.close();

Output(Given to me by a message box that display's Throwable.msg in it's body):
Access Violation

Is this a bug, or me being stupid? If it's the latter, than tell me what went wrong. I am using DMD 2.097.2

This is an error message you'll get from Windows if the file is locked (open by another application).

August 19, 2021

On Thursday, 19 August 2021 at 03:25:31 UTC, Jesse Phillips wrote:
tell me what went wrong. I am using DMD 2.097.2

>

This is an error message you'll get from Windows if the file is locked (open by another application).
Odd. This works if I use a console application. It also works if I use C standard library functions. The file is always closed. I think it's just Win32 being buggy. For example, create a base Win32 application in D. Then put this in WM_CREATE:

case WM_CREATE: //Executed on creation of the window...
   try   {
      import core.stdc.stdio;
      FILE* file;
      file = fopen("file","r"); //NOTE: Replace 'file' with any file on the system...
      fclose(file);
   }
   catch(Throwable e)   {
      MessageBoxA(null,cast(const(char)*)e.msg,"ERROR",MB_OK | MB_ICONERROR);
   }

This works, no Message Box should be displayed(Note: use 2.097.2, this is the version I am using, and I don't know about if this same bug happens on earlier versions).

Now do this:

case WM_CREATE:
   try   {
      import std.stdio;
      File file = File("file","r");  //NOTE: Again, replace 'file' with any file on the system...
      file.close();
   }
   catch(Throwable e)   {
      MessageBoxA(null, cast(const(char)*)e.msg, "ERROR", MB_OK | MB_ICONERROR);
}

For me, this code generates the Message Box. Does this happen for you?

August 20, 2021
On Thursday, 19 August 2021 at 13:47:56 UTC, Ruby The Roobster wrote:
> On Thursday, 19 August 2021 at 03:25:31 UTC, Jesse Phillips wrote:
>  tell me what went wrong.  I am using DMD 2.097.2
>>:
>
> ```d
> case WM_CREATE: //Executed on creation of the window...
>    try   {
>       import core.stdc.stdio;
>       FILE* file;
>       file = fopen("file","r"); //NOTE: Replace 'file' with any file on the system...
>       fclose(file);
>    }
>    catch(Throwable e)   {
>       MessageBoxA(null,cast(const(char)*)e.msg,"ERROR",MB_OK | MB_ICONERROR);
>    }
> ```
> This works, no Message Box should be displayed(Note: use 2.097.2, this is the version I am using, and I don't know about if this same bug happens on earlier versions).
>
> Now do this:
>
> ```d
> case WM_CREATE:
>    try   {
>       import std.stdio;
>       File file = File("file","r");  //NOTE: Again, replace 'file' with any file on the system...
>       file.close();
>    }
>    catch(Throwable e)   {
>       MessageBoxA(null, cast(const(char)*)e.msg, "ERROR", MB_OK | MB_ICONERROR);
> }
> ```
>
> For me, this code generates the Message Box. Does this happen for you?

I don't have a good means to run with this, but it does not look like what I suggested.

August 20, 2021

On Friday, 20 August 2021 at 04:27:34 UTC, Jesse Phillips wrote:

> >

For me, this code generates the Message Box. Does this happen for you?

no errors
https://run.dlang.io/is/4tlm3p

void main() {
    try {
       import std.stdio: File;
       File file = File(__FILE__,"r");
       file.close();
   }
   catch(Throwable e) {
       import std.stdio: writefln;
       writefln("err=%s", e.msg);
   }
}
August 20, 2021

On Wednesday, 18 August 2021 at 17:56:53 UTC, Ruby The Roobster wrote:

>

When I removed those two lines of code, the program ran perfectly without displaying any error or throwing any exception...

The errors aren't always nicely located and can be elsewhere. Try to write a minimal runnable example. Also that cast won't work, you don't really know what you're doing there, use toStringz instead.

August 20, 2021

On Friday, 20 August 2021 at 05:22:20 UTC, nov wrote:

>

On Friday, 20 August 2021 at 04:27:34 UTC, Jesse Phillips wrote:

> >

For me, this code generates the Message Box. Does this happen for you?

no errors
https://run.dlang.io/is/4tlm3p

void main() {
    try {
       import std.stdio: File;
       File file = File(__FILE__,"r");
       file.close();
   }
   catch(Throwable e) {
       import std.stdio: writefln;
       writefln("err=%s", e.msg);
   }
}

This is not a console App. It's a Win32 one. Also, it turns out to be a bug. If I compile my application(with a couple of 'imports' removed under dmd 2.060), it works just fine, with no Access Violation Error. Try the following on dmd 2.060:

import core.runtime;

pragma(lib, "gdi32.lib");

import core.sys.windows.windows;
import std.stdio;

extern(Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
    int result;

    try
    {
        Runtime.initialize();
        result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
        Runtime.terminate();
    }
    catch(Throwable o)
    {
        MessageBoxA(null, o.msg, "Error", MB_OK | MB_ICONERROR);
        result = 1;
    }

    return result;
}

int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
    HWND hwnd;
    MSG  msg;
    WNDCLASSA wndclass;

    wndclass.style         = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc   = &WndProc;
    wndclass.cbClsExtra    = 0;
    wndclass.cbWndExtra    = 0;
    wndclass.hInstance     = hInstance;
    wndclass.hIcon         = LoadIconA(NULL, IDI_APPLICATION);
    wndclass.hCursor       = LoadCursorA(NULL, IDC_ARROW);
    wndclass.hbrBackground = cast(HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName  = NULL;
    wndclass.lpszClassName = appName.toUTF16z;

    if(!RegisterClassA(&wndclass))
    {

        return 0;
    }

    hwnd = CreateWindowA( "Test",
                         "Test",
                         WS_OVERLAPPEDWINDOW,
                         CW_USEDEFAULT,
                         CW_USEDEFAULT,
                         CW_USEDEFAULT,
                         CW_USEDEFAULT,
                         NULL,
                         NULL,
                         hInstance,
                         NULL);

    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    while (GetMessageA(&msg, NULL, 0, 0))
    {
        TranslateMessageA(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

extern(Windows)
LRESULT WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
{
    switch (message)
    {
        case WM_CREATE:
            try
            {
               File file = File("test","r");
               file.close();
            }
            catch(Throwable e)
            {
               MessageBoxA(null, "Error", cast(char)[])e.msg,MB_OK | ICON_ERROR);
               PostQuitMessage(1);
            }
            return 0;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;

        default:
    }

    return DefWindowProcA(hwnd, message, wParam, lParam);
}

Sorry if I misnamed(forgot an 'A' at the end of the function name) something in above code.

Now, edit above code(provided that it has been fixed if necessary) by adding:

import core.sys.windows.wingdi;

And remove:

import core.sys.windows.windows;

And compile it with dmd 2.080.0 or later.

See the results(what matters is if you get the "Access Violation" error or not, everything else is irrelevant).

« First   ‹ Prev
1 2