Valhalla Legends Forums Archive | C/C++ Programming | ShellExecute

AuthorMessageTime
BreW
So um... after successfully opening the folder, the program works great, just as planned. then when i try to close it, my program crashes. you know, the laggy kind, with the "this application has encountered an error and needs to close" sort of thing. It works perfectly fine in VB6, i don't get why this is happening in C++.
[code]
case ID_OPEN_FOLDER:
    char path[256];
    // GetModuleFileName(NULL, buf, 256); //was going to use this...
    GetCurrentDirectory(256, path);
    ShellExecute(hwnd, NULL, path, NULL, "C:\\", 1);
    break;

....

case WM_CLOSE:
    FreeLibrary(hRichtxt);
    DestroyWindow(hwnd);
    break;
case WM_DESTROY:
    PostQuitMessage(0);
    break;
[/code]
you see?
July 21, 2007, 5:12 PM
Kp
At what line does the crash occur?
July 22, 2007, 4:24 AM
BreW
[quote author=Kp link=topic=16897.msg171145#msg171145 date=1185078296]
At what line does the crash occur?
[/quote]

To be honest, I have absolutely no idea. When i run it with the debugger, it just runs in the main GetMesssage() loop and I never get a chance to close the window manually, but when I put SendMessage(hwnd, WM_CLOSE, 0, 0); in the WM_CREATE handler it seems to give me an unhandled exception, access violation. (at the line where i create the window).
When I run the program as normal, it gives me the " a problem has encountered " dialog, and i press debug, it gives me another error. this time an "application error" which basically says "The instruction at memoryaddresshere referenced memory at randomaddresshere. The memory could not be "read"." So basically I have no way to find exactly what line it crashes on..
However when I don't call FreeLibrary(); to free the rich text box's library it doesn't close at all. By the way, now (after doing nothing different) it starts crashing after i close it all the time....
July 22, 2007, 4:59 AM
l2k-Shadow
put MessageBox(0, "test#", "0", 0); after each one of your lines around where you suspect the crash is and you can pinpoint the problem.

such random crashes are probably due to you corrupting memory somewhere, look through your project and make sure you are allocating/freeing memory appropriately.
July 22, 2007, 5:13 AM
Kp
[quote author=brew link=topic=16897.msg171147#msg171147 date=1185080390]
[quote author=Kp link=topic=16897.msg171145#msg171145 date=1185078296]
At what line does the crash occur?
[/quote]

To be honest, I have absolutely no idea. When i run it with the debugger, it just runs in the main GetMesssage() loop and I never get a chance to close the window manually, but when I put SendMessage(hwnd, WM_CLOSE, 0, 0); in the WM_CREATE handler it seems to give me an unhandled exception, access violation. (at the line where i create the window).
When I run the program as normal, it gives me the " a problem has encountered " dialog, and i press debug, it gives me another error. this time an "application error" which basically says "The instruction at memoryaddresshere referenced memory at randomaddresshere. The memory could not be "read"." So basically I have no way to find exactly what line it crashes on..
However when I don't call FreeLibrary(); to free the rich text box's library it doesn't close at all. By the way, now (after doing nothing different) it starts crashing after i close it all the time....
[/quote]

That memory address should be all you need.  Attach a debugger to the process and disassemble that address.  If you have symbols set up correctly, it should tell you the function name and, if it is in your code, the file and line number that map to that address.

What do you mean it "just runs in the main GetMessage() loop"?  Is the debugger breaking in?  If not, in what way is the program behaving differently when the debugger is present versus when it is absent?

Your remark about FreeLibrary implies that you may be unloading the richtext control while it is still in use.  Destroy all the richtext windows before you free the richtext library.
July 30, 2007, 1:03 AM
BreW
[quote author=Kp link=topic=16897.msg171259#msg171259 date=1185757414]
[quote author=brew link=topic=16897.msg171147#msg171147 date=1185080390]
[quote author=Kp link=topic=16897.msg171145#msg171145 date=1185078296]
At what line does the crash occur?
[/quote]

To be honest, I have absolutely no idea. When i run it with the debugger, it just runs in the main GetMesssage() loop and I never get a chance to close the window manually, but when I put SendMessage(hwnd, WM_CLOSE, 0, 0); in the WM_CREATE handler it seems to give me an unhandled exception, access violation. (at the line where i create the window).
When I run the program as normal, it gives me the " a problem has encountered " dialog, and i press debug, it gives me another error. this time an "application error" which basically says "The instruction at memoryaddresshere referenced memory at randomaddresshere. The memory could not be "read"." So basically I have no way to find exactly what line it crashes on..
However when I don't call FreeLibrary(); to free the rich text box's library it doesn't close at all. By the way, now (after doing nothing different) it starts crashing after i close it all the time....
[/quote]

That memory address should be all you need.  Attach a debugger to the process and disassemble that address.  If you have symbols set up correctly, it should tell you the function name and, if it is in your code, the file and line number that map to that address.

What do you mean it "just runs in the main GetMessage() loop"?  Is the debugger breaking in?  If not, in what way is the program behaving differently when the debugger is present versus when it is absent?
[/quote]
Yes that's what I ment, I didn't really know how to phrase it because I never really worked with debuggers, what I ment is that it never broke in unless it receives a window message and stops executing so i am unable to do the events which send the appropriate window messages.... but that's not really a problem because whatever piece of code i want to single step in, i would just have it run on WM_CREATE (although that's not a permanent solution).

[quote]
Your remark about FreeLibrary implies that you may be unloading the richtext control while it is still in use.  Destroy all the richtext windows before you free the richtext library.
[/quote]
Thanks, Kp. I destroyed the rich text box before unloading the library, and it all works perfectly now.
Also, would it have the same effect if I called DestoryWindow (for the main window) before unloading the dll?
July 30, 2007, 1:28 AM
Kp
If I recall correctly, DestroyWindow will destroy child windows as well.  If it destroys the child windows before execution reaches the FreeLibrary call, you will be fine.
July 31, 2007, 2:41 AM

Search