www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.stdio.File is throwing with the message of: "Access Violation"

reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
   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:

   ```d
   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
Aug 18 2021
next sibling parent reply Paul Backus <snarwin gmail.com> writes:
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.
Aug 18 2021
next sibling parent reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
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...
Aug 18 2021
parent Kagamin <spam here.lot> writes:
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.
Aug 20 2021
prev sibling parent Ruby The Roobster <michaeleverestc79 gmail.com> writes:
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.
Aug 18 2021
prev sibling parent reply Jesse Phillips <Jesse.K.Phillips+D gmail.com> writes:
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:

   ```d
   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).
Aug 18 2021
parent reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
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: ```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?
Aug 19 2021
parent reply Jesse Phillips <Jesse.K.Phillips+D gmail.com> writes:
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.
Aug 19 2021
parent reply nov <sorryno em.ail> writes:
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 ```D 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); } } ```
Aug 19 2021
parent reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
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 ```D 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: ```d 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: ```d import core.sys.windows.wingdi; ``` And remove: ```d 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).
Aug 20 2021
next sibling parent jfondren <julian.fondren gmail.com> writes:
On Friday, 20 August 2021 at 21:19:09 UTC, Ruby The Roobster 
wrote:
                MessageBoxA(null, "Error", 
 cast(char)[])e.msg,MB_OK | ICON_ERROR);
use std.string.toStringz to ensure that e.msg is 0-terminated.
Aug 20 2021
prev sibling parent reply evilrat <evilrat666 gmail.com> writes:
On Friday, 20 August 2021 at 21:19:09 UTC, Ruby The Roobster 
wrote:
 int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
 LPSTR lpCmdLine, int iCmdShow)
 {
     // ...
     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);


 See the results(what matters is if you get the "Access 
 Violation" error or not, everything else is irrelevant).
Fix your code first. Your code is broken and in its current state fails to create window, hwnd = 0. GetLastError reports 1407 - ERROR_CANNOT_FIND_WND_CLASS. First parameter for CreateWindow should be window class string that you used in
 wndclass.lpszClassName = appName.toUTF16z;
Aug 20 2021
parent reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
On Saturday, 21 August 2021 at 06:08:17 UTC, evilrat wrote:
 First parameter for CreateWindow should be window class string 
 that you used in

 wndclass.lpszClassName = appName.toUTF16z;
Fix: wndclass.lpszClassName = "Test"; //May need casting...
Aug 21 2021
next sibling parent reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
On Saturday, 21 August 2021 at 23:50:08 UTC, Ruby The Roobster 
wrote:
 On Saturday, 21 August 2021 at 06:08:17 UTC, evilrat wrote:
 First parameter for CreateWindow should be window class string 
 that you used in

 wndclass.lpszClassName = appName.toUTF16z;
Fix: wndclass.lpszClassName = "Test"; //May need casting...
Anyways, this isn't an issue anymore. I'm just not gonna use Win32 API.
Aug 21 2021
parent evilrat <evilrat666 gmail.com> writes:
On Saturday, 21 August 2021 at 23:50:51 UTC, Ruby The Roobster 
wrote:
 On Saturday, 21 August 2021 at 23:50:08 UTC, Ruby The Roobster 
 wrote:
 On Saturday, 21 August 2021 at 06:08:17 UTC, evilrat wrote:
 First parameter for CreateWindow should be window class 
 string that you used in

 wndclass.lpszClassName = appName.toUTF16z;
Fix: wndclass.lpszClassName = "Test"; //May need casting...
Anyways, this isn't an issue anymore. I'm just not gonna use Win32 API.
Of course this is the "fix", but your example works fine and opens a file when window is created. What I don't really understand is why you are trying to remove import core.sys.windows.windows which contains declarations for HWND, HINSTANCE and family... It simply won't compile without it.
Aug 21 2021
prev sibling parent Adam D Ruppe <destructionator gmail.com> writes:
On Saturday, 21 August 2021 at 23:50:08 UTC, Ruby The Roobster 
wrote:
 wndclass.lpszClassName = "Test"; //May need casting...
don't cast it just use the w suffix wndclass.lpszClassName = "Test"w; casts are usually indicating a mistake
Aug 21 2021