![]() |
|
#1
|
|||
|
|||
strange bugThis code is in void CMyAppDlg::OnButton1() function and runs when I press the button:
CPP / C++ / C Code:
This code gets words from file words.txt Each line contains number of characters in a word and the actual word(ex.: "6 myword"). I also have some other code in the OnButton1 function which works fine. This code should work fine too. The problem is when I compile and run(in debug mode) the programm with this code I get "Unhandled exception at 0x7c200074 (mfc70d.dll) in MyApp.exe: 0xC0000005: Access violation reading location 0xffffffff." And it points me to a wincore.cpp function : CPP / C++ / C Code:
To be exact it points to the call of "::GetWindowRect(hWndChild, &rect);" functiobn above(after "CRect rect;"). Im breaking my head figuring out how my code could cause an error like this |
|
#2
|
|||
|
|||
|
I still need help with this. I have tried different things with the code but it still gives memory violation. Here is more info:
If I comment the code it gives no errors. So, I am 99% sure something is wrong in the code I provided. But how can there be something wrong with my code if I debugged it step by step many times with different inputs and it worked just as I expected it to work. Maybe I use fscanf function wrong? I use VC++ 7.0 Btw, I use alot of declarations(variables, functions, structs) at the top of my cpp file. Maybe it takes alot of memory? I don't really know what to do next. |
|
#3
|
||||
|
||||
|
Hi grizli. That is a strange error. One thing that is frustrating about debugging is that an error that was caused by one section of the program may not be a problem until another part of your program needs to use that memory.
I can't see anything readily in your code to say exactly what the error is, but I do have a few suggestions. First, you are not checking that the file was sucessfully opened. You should add a check on wordfile for NULL and then only perform everything else if it is not null (including closing the file). On your if/else you add another unnecessary comparison after the else statement. Comparisons are very costly (in CPU cycles). I would remove this. You have your char arrays defined as 20. Are you absolutely positive that there is never anything larger than 20? The only control on size seems to be numC, which I have no idea what this is or where it is defined. Lastly, you never null terminate your strings that I can see. Maybe this is unnecesary with the way that you are doing things, but to be safe, I would probably try to null-terminate this things. Sorry, I can't give an exact problem. You may want to work on some of these small things and at the same time trace your logic. It may help to reformat your code a bit to make it easier to read the flow, etc. __________________
The best damn Sports Blog period. |
|
#4
|
|||
|
|||
|
Thank you for your suggestions dsmith.
My char arrays are never anything larger than 20 and I don't need to null terminate my char array since I don't use it as a string. Here is what I just did. I added CPP / C++ / C Code:
at the end(when I finish using my char arrays). I run my program using "Start without debugging" in Debug tab(VC++ 7.0). It gave me this error: Debug Assertion Failed! Program: ...\Projects\MyApp\MyApp.exe File: dbgheap.c Line: 1132 Expression: _CrtIsValidHeapPointer(pUserData) Is this normal? |
|
#5
|
|||
|
|||
|
ahh, I have found my mistake on my own. I shouldn't have done this: fscanf(wordfile,"%s\n");
|
|
#6
|
|||
|
|||
|
It is generall better not to mix up C and C++ code.
If you use C++ compiler than use new rather then malloc. And you can be alway sure that when receive error in dbgheap.c that problem is around new (malloc)., because both new and malloc allocating memory from the heap. So pay special attention how you use new operator and what doing with pointers. For example you can allocate memory for 10 integer numbers and often in loops your pointer can pass beyound bounds of array. Always look to have equal number of calling new and delete to prevent memory leak in your program. It is surprising how often that bug is present even in every day applications you use includeing Windows. |
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| strange sizeof(structure) - multiple of 8 | pinkpanther | C Programming Language | 11 | 30-May-2004 08:20 |
| Strange problem when I try to print a value? | 7eleven | C Programming Language | 2 | 15-May-2004 11:45 |
| CD Drive really working,but not really working, strange!! | Joel86 | Computer Hardware Forum | 3 | 11-May-2004 09:45 |
| Cd burner behaves strange | Julepessimisten | Computer Hardware Forum | 0 | 03-Jan-2004 15:17 |
| strange compile error - code from reputable author!! | newbie_cpp | C++ Forum | 0 | 06-Nov-2003 17:15 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The