help needed

Posted by Gohan_TheDragonball on Sat 30 Jun 2007 05:21 AM — 20 posts, 65,090 views.

USA #0
gdb ./smaug ../area/core
GNU gdb 5.3
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-slackware-linux"...
Core was generated by `smaug 1202 hotboot 8'.
Program terminated with signal 11, Segmentation fault.

warning: current_sos: Can't read pathname for load map: Input/output error

Cannot access memory at address 0x4c4c5255
#0 0x2283b892 in ?? ()
(gdb) bt
#0 0x2283b892 in ?? ()
#1 0x080e2279 in nextLetter (row=1, col=1, num=21) at scrabble.c:421
#2 0x080e1d93 in nextLetter (row=2, col=1, num=20) at scrabble.c:336
#3 0x080e1d93 in nextLetter (row=3, col=1, num=19) at scrabble.c:336
#4 0x080e1d93 in nextLetter (row=4, col=1, num=18) at scrabble.c:336
#5 0x080e2369 in nextLetter (row=4, col=2, num=17) at scrabble.c:438
#6 0x080e217a in nextLetter (row=3, col=2, num=16) at scrabble.c:404
#7 0x080e217a in nextLetter (row=2, col=2, num=15) at scrabble.c:404
#8 0x080e2279 in nextLetter (row=1, col=3, num=14) at scrabble.c:421
#9 0x080e1d93 in nextLetter (row=2, col=3, num=13) at scrabble.c:336
#10 0x080e1d93 in nextLetter (row=3, col=3, num=12) at scrabble.c:336
#11 0x080e1d93 in nextLetter (row=4, col=3, num=11) at scrabble.c:336
#12 0x080e2369 in nextLetter (row=4, col=4, num=10) at scrabble.c:438
#13 0x080e217a in nextLetter (row=3, col=4, num=9) at scrabble.c:404
#14 0x080e217a in nextLetter (row=2, col=4, num=8) at scrabble.c:404
#15 0x080e217a in nextLetter (row=1, col=4, num=7) at scrabble.c:404
#16 0x080e217a in nextLetter (row=0, col=4, num=6) at scrabble.c:404
#17 0x080e1f85 in nextLetter (row=0, col=3, num=5) at scrabble.c:370
#18 0x080e1f85 in nextLetter (row=0, col=2, num=4) at scrabble.c:370
#19 0x080e1d93 in nextLetter (row=1, col=2, num=3) at scrabble.c:336
#20 0x080e2087 in nextLetter (row=0, col=1, num=1) at scrabble.c:387
#21 0x080e194f in startWord (row=0, col=0, num=1) at scrabble.c:237
#22 0x081941a1 in do_scrabble (ch=0x8e2e880, argument=0x0) at scrabble.c:154
#23 0x080b1502 in interpret (ch=0x8e2e880, argument=0x5e9843c9 "showwords") at interp.c:611
#24 0x080710d6 in game_loop () at comm.c:881
#25 0x080705e7 in main (argc=4, argv=0x5e9868d4) at comm.c:440
#26 0x227d6d06 in ?? ()
USA #1
sorry had to break it up:

Scrabble Blast v1.0

1 2 3 4 5
-----------
1 | R U L L D
2 | I P R N O
3 | I O A D U
4 | O M J F Z
5 | O H C F X

Syntax: scrabble <word>
scrabble choose <word> <word #>
scrabble set <row> <col> <letter>
scrabble showwords

Alrighty I made a small little scrabble game for my mud, everything compiled fine, no errors or warnings. But when i run the showwords part of my function, the mud crashes. The part that is bugging me is it is crashing in different places each time. i already checked and made sure i made clean and recompiled, but still the same results. i tried searching google for the wierd warning i got since it was the first time id seen it.

http://www.mudbytes.net/index.php?a=pastebin&s=view&pid=1693

i pasted scrabble.c into mudbytes for your viewing pleasure
USA #2
What you're seeing is very typical of stack overflow. I'd run it through Valgrind -- that's the quickest way to find the problem. If you can't use Valgrind, use asserts to sanity-check all arguments to functions.

Also note that strings need to be long enough to have the zero bytes.

On a more general note:
I'm not sure I understand the design of your scrabble program. If the variables are all global, does that mean only one game can be played at a time?
USA #3
well i went through and created something that should have been in there in the first place, which was a check along every step of the way to make sure we are creating a word, since there is no point going all the way to 7 letters if no word in the dictionary starts with the first 2-3-4-5etc letters. doing that was able to stop the crashing. however now i am getting a memory leak. and i can't figure out why.
USA #4
oh and the variables will eventually be transferred into the char struct so each player can have their own game.
USA #5
Well, again, the best way to figure out the memory leak is to run this through Valgrind. Sure beats poring through the code by hand, especially when you know how to trigger the memory leak.
USA #6
yeah i know perfectly how to trigger it, unfortunately wolfpaw is run by i am guessing lazy bums, since i wrote them many messages asking them to install valgrind but never got any response. i figured out the problem, somehow the words are growing larger than 7 letters long, even though i put a check in to prevent that:

if ( strlen(sofar) > 6 )
return;
USA #7
Can you install it into your own directory by any chance? Looking at this page:
http://valgrind.org/docs/manual/manual-core.html#manual-core.install
it doesn't look that hard to do.
Australia Forum Administrator #8
I am a bit confused about the code you showed. For one thing, I gather each position is a single letter? So why do STRALLOC for each position in the array? Why not have an array of char (not char *) and simply store the character in each position? Ones not set could simply be zero.
USA #9
from what i remember i was unable to install valgrind, so i had asked wolfpaw if they could do it, but they never responded to my emails, how nice of them huh.
USA #10
ok was able to get onto a friends server with valgrind and fire it up, i then triggered the function and well i got a lot of stuff that i am trying to sort through, any help would be appreciated:

http://www.mudbytes.net/index.php?a=pastebin&s=view&pid=1743

the part initiated by scrabble starts at line 335
Australia Forum Administrator #11
This is the sort of thing that the Lua interface would help with. String handling is much easier, and it virtually never crashes. You don't need to change the character structure, and it keeps an instance per player.
USA #12
alright i tried fixing this but i cant seem to get it to work correctly. i googled "Invalid write of size 1" and it said something about going outside an arrays range. i put in checks to make sure they were not outside the range, but it still said the same thing.


    if ( num < 0 || num >= 2000 || row < 0 || col < 0 || row > 4 || col > 4 )
        return;

    strcat( curword[num], scrabble[row][col] );


now after putting in these checks, i did notice a vast improvement in the speed as well as a glitch or two disappeared, but apparently it still needs work.
USA #13
yes it definitely still needs help, while the glitch where the strings extended past 7 letters was fixed, however the memory is still being corrupted and affecting other areas, prompts are being changed to a random word that was used, etc.
USA #14
any idea why this is not working as it should?

sprintf(sql,"select * from dictionary where word like '%s%%'", word );
bug ( sql );

Sat Jul 7 08:42:45 PM 2007 :: (db.c, line 4976) :: [*****] BUG: select * from dictionary where word like 'OL

for some reason its cutting off instead of putting a percent sign in there as well as the last single quote.
Amended on Sat 07 Jul 2007 11:58 PM by Gohan_TheDragonball
USA #15
What are the contents of 'word'? How big is the 'sql' buffer?
USA #16
the contents of word were OL, and the sql buffer is 1000, so there is no issue there. when i remove the %%, the output is normal, it only messes up when i put in the two percentage signs.
USA #17
Is it possible that the MUD is interpreting those symbols somehow?

See the following:


[david@thebalrog:~]$ cat test.c

#include <stdlib.h>
#include <stdio.h>

int main(char ** argv, int argc)
{
    char buf[1000];
    const char * word = "OL";

    sprintf(buf,"select * from dictionary where word like '%s%%'\n", word );
    printf(buf);

    return 0;
}

[david@thebalrog:~]$ gcc test.c -o test
[david@thebalrog:~]$ ./test                            
select * from dictionary where word like 'OL%'
[david@thebalrog:~]$


I use the \n because otherwise zsh gets a little confused.

Anyhow, it comes out correctly...

I would try, on the MUD, using printf instead of bug and see if it does the right thing. (Of course you will have to look at the logs.)
USA #18
yeah i guess for whatever reason it wasn't showing up correctly either because of valgrind or the mud, but wrote up a little function to check words and it worked just fine.
USA #19
Ok well after exhausting debugging and changes, I finally have a working version of this scrabble game. All the variables are now in the character structure. Instead of using an array of current words i decided to hold an array of integers pointing to the letter on the board instead. I have a couple problems however that I could use an outside opinion on.

1.) Not going in correct order, in terms of direction it might go fine up through the 4th letter but the 5th letter is 3 spaces away instead of adjacent to the last square.

2.) Non-words not being cut off when determined not a word. IE words like IUIRPU being checked in the dictionary when it shouldn't have made it past IU. I was even checking the code on my friends server, which does not even have access to my database, so techincally no words should have gone past two letters, but they were anyways.

http://www.mudbytes.net/index.php?a=pastebin&s=view&pid=1943

Thats the two files scrabble.c and scrabble.h pasted into one file, as well as the mud.h definitions and declarations. Any helps would be appreciated thanks.

****EDIT*****
After more debugging and changing I have fixed those two errors, and the code is actually working now. I still have some memory leaks to track down so back to valgrind.