access violation 0xC0000005

I'm going to attach my entire source code for this file here. Maybe someone has an idea how to fix it. It works fine 99% of the time, and it will just randomly crash out with a 0xC0000005 access violation and write a huge log in Dr. Watson. The problem can be replicated by running the program, close it, and reopen right away. It will also crash if you try to open a port to a modem with a comm port that does not exist. I think it has to do with a sleep() command or something, but i'm not certain. This pesky little bug is causing me baldness. Any help would be GREATLY appreciated! The program is a communications program, that sits and waits for incoming calls from proprietary terminals that transmit over telco lines. It can control up to 32 modems.

use this link to get the source code zip file since it's too big for me to attach here... it's about 320kb.

http://www.freespaces.com/thepicpile/communications.zip
[958 byte] By [sorner] at [2007-11-19 7:30:25]
# 1 Re: access violation 0xC0000005
1. Why don't you debug using your the Debug Version of your binaries? When the crash occurs, Visual Studio will take you to the offending line, and you can fix it.
2. If it is a release mode crash, generate MAP files and use them.

This article will help you -
Finding crash information using the MAP file (http://www.codeproject.com/debug/mapfile.asp?df=100&forumid=14008&exp=0&select=960048)

Ciao,
Sid! :wave:
Siddhartha at 2007-11-11 0:27:22 >
# 2 Re: access violation 0xC0000005
Couldn't open you project.
Gordon Lee at 2007-11-11 0:28:22 >
# 3 Re: access violation 0xC0000005
I think it has to do with a sleep() command or something, but i'm not certain.
Sleep will not cause a crash.

I ran your program a couple of time, and it did not crash.

A crash especially an Access Violation occurs when you access a location not allocated by your program, and also when you use unitialized pointers.

Your program leaks about 66 KBytes of memory on simple start and close.
1. Plug this leak.
2. Initialize pointer Data. Sample below -
int * pInt = NULL;
3. Allocate memory before using the pointer.
pInt = new int [5];
4. Check pointer for non-NULL before using it - everywhere.
if (pInt != NULL)
{
// ...
}
5. Delete allocated data only ONCE.
if (pInt)
delete [] pInt;
pInt = NULL;

A] Do equivalent of the four points above for every pointer data you declare.
B] Use a software like Bounds-Checker to fix your leaks.
Siddhartha at 2007-11-11 0:29:23 >
# 4 Re: access violation 0xC0000005
is there any way you could point me in the direction to fix this? i'm a budding C++ programmer and the person who wrote the program is not accessible anymore... So i'm learning as I go.
sorner at 2007-11-11 0:30:18 >
# 5 Re: access violation 0xC0000005
is there any way you could point me in the direction to fix this? i'm a budding C++ programmer and the person who wrote the program is not accessible anymore... So i'm learning as I go.
I just re-edited my post to elaborate on what you can do... :thumb:
Siddhartha at 2007-11-11 0:31:17 >
# 6 Re: access violation 0xC0000005
where do i put that? in every function? also, how do i fix the memory leak... if there's a leak at startup and shutdown, i'm sure theres more elsewhere... also, how did you find that there is a memory leak, did you use a program to find that? I'm very weak with debugging and assembly language.. I have some books i need to read up on. Thanks for your help.
sorner at 2007-11-11 0:32:17 >
# 7 Re: access violation 0xC0000005
Crash

Surviving the Release version (http://www.codeproject.com/debug/survivereleasever.asp)
How To Determine the Location of a Crash (http://support.microsoft.com/kb/q196755)
Finding crash information using the MAP file (http://www.codeproject.com/debug/mapfile.asp)

Memory Leak

Detecting and Isolating Memory Leaks (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/vxcondetectingisolatingmemoryleaks.asp)
Detecting Memory Leaks in MFC (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/_core_detecting_memory_leaks.asp)
Andreas Masur at 2007-11-11 0:33:24 >
# 8 Re: access violation 0xC0000005
where do i put that? in every function? also, how do i fix the memory leak... if there's a leak at startup and shutdown, i'm sure theres more elsewhere... also, how did you find that there is a memory leak, did you use a program to find that? I'm very weak with debugging and assembly language.. I have some books i need to read up on. Thanks for your help.

Yes, I used Bounds Checker - also mentioned in my post.
Memory leaks get fixed by deleting memory allocated.

If in the sample above I had forgotten to perform step # 5, then I would have effectively leaked 5 integers - 5 x 4 = 20 bytes. Understand?

I allocated integers, and de-allocated them by delete, you must do the same for every object you allocate in your project.

Yes, it will be a good learning experience for you!!
Siddhartha at 2007-11-11 0:34:29 >
# 9 Re: access violation 0xC0000005
this is causing me to lose more hair... I'm going to attach a link to the file bounds checker spit out. I ran some diagnostics on the code and found a bunch of errors that I went in and fixed and they went away. I still have some memory leaks though. I'm also going to post a link to the updated version of the code since I made the changes... I've noticed that MSCTL.DLL's expected load address and it's actual load address are different, and I've read that isn't good. any ideas on that?

Here's the updated links:

http://www.freespaces.com/thepicpile/commhostdpbcl.zip

http://www.freespaces.com/thepicpile/communicationsnew.zip

There is also a .MAP file in the release folder if you want to look at that.

Thanks for your help! :)

Steve
sorner at 2007-11-11 0:35:27 >