Bizarre problem!

This is a discussion on Bizarre problem! within the C Programming forums, part of the General Programming Boards category; I have a global variable array: Code: TABLE g_table[MAX_TABLES]; // TABLE is defined typedef struct{...} TABLE; I also have a ...

  1. #1
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120

    Unhappy Bizarre problem!

    I have a global variable array:

    Code:
        TABLE g_table[MAX_TABLES]; // TABLE is defined typedef struct{...} TABLE;
    I also have a function that uses TABLE as its argument and its return value:

    Code:
        TABLE MyFunction(TABLE table)
        {... 
        }
    The problem is when I call MyFunction(), my program performs an 'illegal operation'. I am calling it like this:

    Code:
        g_table[x] = MyFunction(g_table[x]);
    HOWEVER, and this really puzzles me, if I do this:

    Code:
        MessageBeep(0xFFFFFFFF);
        g_table[x] = MyFunction(g_table[x]);
    my function works fine!! It also works if I send a WM_PAINT message before calling MyFunction.

    Although I can fix this bug, I don't know whats causing it and I've been getting rather stressed so any ideas would be much appreciated!

    Thanks in advance

    dt

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Can you post the structure declaration?

    -Prelude
    My best code is written with the delete key.

  3. #3
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Code:
    typedef struct
    {
    	PLAYER player[MAX_PLAYERS];
    	int pot,
    		dButton,
    		nPlayers,
    		nActivePlayers;
    	BLINDS blinds;
    	BOOL visible; 
    	BOOL active;
    	BOOL suspended; 
    } TABLE;
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    > HOWEVER, and this really puzzles me, if I do this
    The change does not affect anything, except to rearrage the compiled code in a subtle way.

    There is a bug alright, but this isn't the place where the problem is, this is just the place where the problem is noticed - your real bug is elsewhere.

    First thing to do is to check all pointers carefully
    - are they initialised - if not, start by initialising them to NULL when you declare them
    - are they allocated with malloc? are you allocating the right amount of space
    Eg
    type *ptr = malloc ( n * sizeof(ptr[0]) );
    - are all array subscripts in bounds? check your for loops are
    for ( i = 0 ; i < N ; i++ )
    not
    for ( i = 0 ; i <= N ; i++ )

  5. #5
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Thanks, I'm usually pretty stringent about using pointers properly but I'll do some fine combing!
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    It might be a simple buffer overflow. Best check for that type of thing as well. Have fun combing!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Excuse my ignorance but what is a buffer overflow and how would I check it??!

    (I've checked all my pointers etc. and I'm pretty sure everything is as it should be, I'm ready to cry! ;-)

    Thanks again

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  8. #8
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by DominicTrix
    Excuse my ignorance but what is a buffer overflow and how would I check it??!
    A buffer overflow is when you try to write data to memory which you didn't allocate.
    Code:
    char buf[3];
    strcpy(buf, "Hi"); /* Correct */
    strcpy(buf, "Hello");  /* buffer overflow */
    buf[5] = 'A'; /* buffer overflow */
    Just post (or attach) your code so we can take a look at it.

  9. #9
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Ah, thats what I thought the problem was to start with (thanks), I guess it probably is.

    I'll try and post what I think the relevant code is: (the whole code is around 6000 lines)

    declarations:

    Code:
    DWORD WINAPI Play(LPVOID);
    int PlayHome(void);
    TABLE PlaySeenHand(TABLE);
    
    TABLE g_table[MAX_TABLES];
    int g_nTables;
    Main window procedure that starts thread 'Play'

    Code:
    case WM_COMMAND:
         switch(LOWORD(wParam))
         {
                case IDB_DEAL:
                {
                         // start Dealer Control thread
    	    hThread = CreateThread(NULL, 0,  Play , NULL, 0, &dwThreadId);
    	    if(hThread == NULL)
    	    {
    	           MessageBox(hwnd, "Thread Failed", ":-(", MB_OK);
    	           SendMessage(hwnd, WM_CLOSE, 0, 0L);
    	    }
    	    CloseHandle(hThread);
    
                }
         }

    Play thread:

    Code:
    DWORD WINAPI Play(LPVOID lParam)
    {
    	int i;
    
    	switch(g_gameType)
    	{
    		case HOME:
    			return PlayHome();
    		default:
    			return -1;
    	}
    }
    
    int PlayHome()
    {
    	int x;
    	char txt[200];
    
    	for(x=0; x<g_nTables; x++)
    // THIS IS WHERE THE ERROR OCCURRS (the code doesn't reah line 1 of PlaySeenHand();
    
    	g_table[x] = PlaySeenHand(g_table[x]);
    
    
    	// see how many players are left
    		if(g_table[0].nActivePlayers == 1)
    		{
    			for(x=0; x<g_table[0].nPlayers; x++)
    			{
    				if(g_table[0].player[x].playing == TRUE)
    				{
    					char msg[30];
    					wsprintf(msg, "%s wins the game!!", g_table[0].player[x].name);
    					MyMessageBox(msg, TRUE, FALSE);
    					return 1;
    				}
    			}
    		}
    
    		btnDeal.enabled = TRUE;
    		btnDeal.mouseState = OFF;
    		DrawButton(btnDeal, g_hdcBuffer);
    		return 0;
    }

    Thanks again
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    Mmm....

    CloseHandle(hThread);
    Seems like the wrong thing to be doing just having created the thread.

    Also
    int g_nTables;
    What is this initialised to, when it gets to that for loop?

  11. #11
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    I don't see even a prototype for PlaySeenHand()... We need to know what it's supposed to return.

    Secondly, have you run it through a debugger?

    As a (rare) possibility-- Have you done a disassembly to be sure the compiler is generating the proper mnemonics?

    The error occurs usually above the problem-- that's just where the code finally got halted.
    It is not the spoon that bends, it is you who bends around the spoon.

  12. #12
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Thanks guys, I'm trying to figure out debugging right now. The prototype for PlaySeenHand() is

    Code:
    TABLE PlaySeenHand(TABLE);
    I've discovered the error actually occurs whenever I try and pass g_table[0] as an argument to a function (the functions are all prototyped correctly and work with other TABLE structures), and yet when I inspect the g_table[0] variable at the point just before the error with the debugger, nothing seems abnormal.

    The error occurs usually above the problem-- that's just where the code finally got halted.
    Does this mean that it might have nothing to do with the g_table variable??!

    Anyways, thanks for your help, I'm going to see if I can work my debugger to give me some more details about the exception.

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  13. #13
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Well I've fixed my bug without finding out what the fault was. Instead of using:

    Code:
    TABLE PlaySeenHand(TABLE);
    ...
    g_table[x] = PlaySeenHand(g_table[x]);
    I'm using:
    Code:
    int PlaySeenHand(TABLE *) 
    ...
    ret = PlaySeenHand(&g_table[x]);
    which is far more efficient anyway!

    The fact that this works leads me to believe that there may have been a problem allocating memory for the passed data structure(the TABLE structure is pretty large). Does this sound plausible? It would help to know about this for the future.

    Anyways, thanks for all your help (this forum never ceases to impress me)

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    > Well I've fixed my bug without finding out what the fault was
    Or you've successfully managed to mask it, until it bites you again later on.

    Like
    MessageBeep(0xFFFFFFFF);
    masked it in your original post.

    > passed data structure(the TABLE structure is pretty large). Does this sound plausible?
    Exactly how large is this structure?
    Do
    printf( "%d\n", sizeof(TABLE) );

  15. #15
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Yeah, it is kind of worrying. In fact the error did reoccur after this point and again it was when I was passing the TABLE structure as an argument to a function (I've now 'fixed' all the functions that had TABLE as an argument). The size of the struct is 7724 (bytes?).

    When I used the Turbo debugger from Borland and stepped through the code, my program threw an "Access Violation - Writing to memory 5310000" exception at the line of the function call.

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21