digitalmars.D.learn - std.stdio.File is throwing with the message of: "Access Violation"
- Ruby The Roobster (14/14) Aug 18 2021 All I did was try to access a file with a self-made library.
- Paul Backus (4/9) Aug 18 2021 It's a bug in your code. "Access Violation" means your program
- Ruby The Roobster (3/13) Aug 18 2021 When I removed those two lines of code, the program ran perfectly
- Kagamin (6/9) Aug 20 2021 The errors aren't always nicely located and can be elsewhere. Try
- Ruby The Roobster (3/13) Aug 18 2021 In addition, I figured out that if I moved the code outside of
- Jesse Phillips (4/18) Aug 18 2021 This is an error message you'll get from Windows if the file is
- Ruby The Roobster (39/41) Aug 19 2021 Odd. This works if I use a console application. It also works if
- Jesse Phillips (4/41) Aug 19 2021 I don't have a good means to run with this, but it does not look
- nov (16/18) Aug 19 2021 no errors
- Ruby The Roobster (111/129) Aug 20 2021 This is not a console App. It's a Win32 one. Also, it turns out
- jfondren (3/5) Aug 20 2021 use std.string.toStringz to ensure that e.msg is 0-terminated.
- evilrat (8/32) Aug 20 2021 Fix your code first.
- Ruby The Roobster (3/6) Aug 21 2021 Fix:
- Ruby The Roobster (4/11) Aug 21 2021 Anyways, this isn't an issue anymore. I'm just not gonna use
- evilrat (7/19) Aug 21 2021 Of course this is the "fix", but your example works fine and
- Adam D Ruppe (5/6) Aug 21 2021 don't cast it just use the w suffix
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
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.2It's a bug in your code. "Access Violation" means your program tried to access out-of-bounds memory.
Aug 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:When I removed those two lines of code, the program ran perfectly without displaying any error or throwing any exception...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.2It's a bug in your code. "Access Violation" means your program tried to access out-of-bounds memory.
Aug 18 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.
Aug 20 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: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.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.2It's a bug in your code. "Access Violation" means your program tried to access out-of-bounds memory.
Aug 18 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: ```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.2This is an error message you'll get from Windows if the file is locked (open by another application).
Aug 18 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.2This 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
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.2I don't have a good means to run with this, but it does not look like what I suggested.:```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
On Friday, 20 August 2021 at 04:27:34 UTC, Jesse Phillips wrote: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); } } ```For me, this code generates the Message Box. Does this happen for you?
Aug 19 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: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).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); } } ```For me, this code generates the Message Box. Does this happen for you?
Aug 20 2021
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
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 inwndclass.lpszClassName = appName.toUTF16z;
Aug 20 2021
On Saturday, 21 August 2021 at 06:08:17 UTC, evilrat wrote:First parameter for CreateWindow should be window class string that you used inFix: wndclass.lpszClassName = "Test"; //May need casting...wndclass.lpszClassName = appName.toUTF16z;
Aug 21 2021
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:Anyways, this isn't an issue anymore. I'm just not gonna use Win32 API.First parameter for CreateWindow should be window class string that you used inFix: wndclass.lpszClassName = "Test"; //May need casting...wndclass.lpszClassName = appName.toUTF16z;
Aug 21 2021
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: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.On Saturday, 21 August 2021 at 06:08:17 UTC, evilrat wrote:Anyways, this isn't an issue anymore. I'm just not gonna use Win32 API.First parameter for CreateWindow should be window class string that you used inFix: wndclass.lpszClassName = "Test"; //May need casting...wndclass.lpszClassName = appName.toUTF16z;
Aug 21 2021
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