GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Jul-2004, 12:45
grizli grizli is offline
New Member
 
Join Date: Jun 2004
Posts: 7
grizli is on a distinguished road

strange bug


This code is in void CMyAppDlg::OnButton1() function and runs when I press the button:

CPP / C++ / C Code:
FILE *wordfile = fopen("words.txt","r");
int fnumC=0;
int chars=0;
int bestchars=0;
char bestword[20];// = (char *)malloc( numC );
char tempword[20];// = (char *)malloc( numC );
while(!feof(wordfile)){
   chars=0;
   fscanf(wordfile,"%d ",&fnumC);
   if(fnumC!=numC)fscanf(wordfile,"%s\n");else if(fnumC==numC){
      for(int j=0;j<numC;j++){
         fscanf(wordfile,"%c",&tempword[j]);
         if(theword[j]==tempword[j])chars++;
     }
     if(chars>bestchars){
        bestchars=chars;
        for(int r=0;r<numC;r++)bestword[r]=tempword[r];
     }
     fscanf(wordfile,"\n");
   }

}
fclose(wordfile);

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:
void CWnd::ScrollWindow(int xAmount, int yAmount,
	LPCRECT lpRect, LPCRECT lpClipRect)
{
	ASSERT(::IsWindow(m_hWnd));

	if (IsWindowVisible() || lpRect != NULL || lpClipRect != NULL)
	{
		// When visible, let Windows do the scrolling
		::ScrollWindow(m_hWnd, xAmount, yAmount, lpRect, lpClipRect);
	}
	else
	{
		// Windows does not perform any scrolling if the window is
		// not visible.  This leaves child windows unscrolled.
		// To account for this oversight, the child windows are moved
		// directly instead.
		HWND hWndChild = ::GetWindow(m_hWnd, GW_CHILD);
		if (hWndChild != NULL)
		{
			for (; hWndChild != NULL;
				hWndChild = ::GetNextWindow(hWndChild, GW_HWNDNEXT))
			{
				CRect rect;
				::GetWindowRect(hWndChild, &rect);
				ScreenToClient(&rect);
				::SetWindowPos(hWndChild, NULL,
					rect.left+xAmount, rect.top+yAmount, 0, 0,
					SWP_NOSIZE|SWP_NOACTIVATE|SWP_NOZORDER);
			}
		}
	}

#ifndef _AFX_NO_OCC_SUPPORT

	if ((m_pCtrlCont == NULL) || (lpRect != NULL))
		return;

	// the following code is for OLE control containers only

	m_pCtrlCont->ScrollChildren(xAmount, yAmount);

#endif // !_AFX_NO_OCC_SUPPORT
}


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  
Old 12-Jul-2004, 11:10
grizli grizli is offline
New Member
 
Join Date: Jun 2004
Posts: 7
grizli is on a distinguished road
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  
Old 13-Jul-2004, 08:42
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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.
  #4  
Old 13-Jul-2004, 15:13
grizli grizli is offline
New Member
 
Join Date: Jun 2004
Posts: 7
grizli is on a distinguished road
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:
delete [] bestword;
delete [] tempword;
delete [] theword;

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  
Old 14-Jul-2004, 15:06
grizli grizli is offline
New Member
 
Join Date: Jun 2004
Posts: 7
grizli is on a distinguished road
ahh, I have found my mistake on my own. I shouldn't have done this: fscanf(wordfile,"%s\n");
  #6  
Old 15-Jul-2004, 14:41
Pandiani Pandiani is offline
New Member
 
Join Date: May 2004
Location: The Balkans
Posts: 16
Pandiani is on a distinguished road
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 GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 01:45.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.