Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,300 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,460 people online right now. Registration is fast and FREE... Join Now!




Weird access violation

 
Reply to this topicStart new topic

Weird access violation, win32

Stalker
3 Sep, 2008 - 08:31 AM
Post #1

New D.I.C Head
*

Joined: 3 Sep, 2008
Posts: 4

CODE

// Load from file to an edit control
BOOL LoadFileToEdit(HWND hEdit, LPSTR filePath)
{
    HANDLE hFile = NULL;
    LPSTR text;
    DWORD dwSize = NULL;
    BOOL bResult = FALSE;

    hFile = CreateFile(
        filePath, GENERIC_READ, FILE_SHARE_READ, NULL,
        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile != INVALID_HANDLE_VALUE) {
        dwSize = GetFileSize(hFile, &dwSize);
        if (dwSize) {
            text = (LPSTR) GlobalAlloc(GPTR, dwSize + 1);
            if (text) {
                DWORD dwRead;
                if (ReadFile(hFile, text, dwSize, &dwRead, NULL)) {
                    SetWindowText(hEdit, text);
                    bResult = TRUE;
                }
                GlobalFree(text);
            }
        }
        CloseHandle(hFile);
    }
    return bResult;
}


It works fine, most of the time. But occasionally it will throw an access violation, and it's not file specific. It just does it... randomly. And I can't even set up a handler for it.

I can't figure it out!

p.s. I am fairly new to the win32 API, any tips would be appreciated. cool.gif
User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: Weird Access Violation
3 Sep, 2008 - 07:09 PM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
You can trap access violations with the __try/__except blocks in Microsoft C/C++. There are ways to do this in MinGW also, with some tricks.
User is offlineProfile CardPM
+Quote Post

Stalker
RE: Weird Access Violation
3 Sep, 2008 - 08:49 PM
Post #3

New D.I.C Head
*

Joined: 3 Sep, 2008
Posts: 4

QUOTE(perfectly.insane @ 3 Sep, 2008 - 08:09 PM) *

You can trap access violations with the __try/__except blocks in Microsoft C/C++. There are ways to do this in MinGW also, with some tricks.


I know, I tried that. I could encapsulate the entire program in a giant __try __except block, but it doesn't catch it. It's not the try block, because I can use throw and it will catch the same exception.

I'm using MVC++, btw.
User is offlineProfile CardPM
+Quote Post

Stalker
RE: Weird Access Violation
4 Sep, 2008 - 12:45 AM
Post #4

New D.I.C Head
*

Joined: 3 Sep, 2008
Posts: 4

It's not the code, I finally realized. I can recreate the exception in nearly any Open dialog in any program.

It's one of the following DLLs loading when I use GetOpeFileName()

'xpsp2res.dll'
'pdfshell.dll' -- Adobe reader
'msvcr80.dll'

I'm betting it's abobe.
User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: Weird Access Violation
4 Sep, 2008 - 03:56 PM
Post #5

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
QUOTE(Stalker @ 4 Sep, 2008 - 12:49 AM) *

QUOTE(perfectly.insane @ 3 Sep, 2008 - 08:09 PM) *

You can trap access violations with the __try/__except blocks in Microsoft C/C++. There are ways to do this in MinGW also, with some tricks.


I know, I tried that. I could encapsulate the entire program in a giant __try __except block, but it doesn't catch it. It's not the try block, because I can use throw and it will catch the same exception.

I'm using MVC++, btw.


That's strange. I never thought __try/__except worked with C++ exception handling. I have read that try/catch is supposed to implement SEH in Microsoft C++ though.

I've found times that SEH does not work properly for me, especially when the exception occurs during a message processing function (a window proc or a dialog proc). In that case, it seems like you need a __try/__except in that function also, possibly as there might be a handler installed in the API functions as well, handling the exception before your application gets a chance. But then again, that's been my experience with implementing a SEH library for MinGW, so it might not be entirely accurate.

QUOTE
It's not the code, I finally realized. I can recreate the exception in nearly any Open dialog in any program.

It's one of the following DLLs loading when I use GetOpeFileName()

'xpsp2res.dll'
'pdfshell.dll' -- Adobe reader
'msvcr80.dll'

I'm betting it's abobe.


Well, using a debugger would tell you for sure. A good debugger like WinDbg will break on an access violation, without having to step through everything to figure out where it is occurring. Visual C++ will also probably give the source, but then again, the problem could be more complicated and the module with the access violation may not be the actual module with the fault (passing a bad pointer to a function in another module, for example).


User is offlineProfile CardPM
+Quote Post

Stalker
RE: Weird Access Violation
4 Sep, 2008 - 08:30 PM
Post #6

New D.I.C Head
*

Joined: 3 Sep, 2008
Posts: 4

It was adobe integrating into the windows shell. Uninstalled and no more access violations.

Adobe = evil.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 06:25AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month