Wrapping a C library

Graham J Lee <[email protected]>
Newsgroups gmane.comp.lib.gnustep.user
Message-ID <[email protected]>
Hi,

I'm creating a wrapper class for libdict (yes, I realise this has 
possibly been done before), and running into a segfault problem.  I 
realise this has possibly more to do with bad C than with Foundation 
per se, but I'd appreciate some help :-)

libdict has a function ld_define, declared thus in libdict.h:
struct ld_defanswer **ld_define(struct ld_conn *conn, char *word);

and I have an object GLDictionaryAccessor which contains the following 
method:
-(struct ld_defanswer **)define:(NSString *)w
{
	return ld_define(connection,[w cString]);
}

I wrote an ObjC CLI interface for the class [this is, apart from for 
testing purposes a bit pointless, as I fgets c strings, 
stringWithCString them, then use cString in the wrapper object...] 
which does this:

		if(!strcasecmp(input,"define"))
		{
			struct ld_defanswer **d;
			printf("Define word: ");
			fgets(arg1,BUFSIZ,stdin);
			arg1[strlen(arg1)-1]='\0';
			a1=[NSString stringWithCString:arg1];
			d=[da define:a1];
			if(d==NULL)
			{
				printf("No results for '%s'\n",arg1);
			}
			else
			{
				while(d[i]!=NULL)
				{
					printf("Got HERE too, d[i]=%d\n",d[i]);
					printf("Found definition:\n%s\n",d[i]->ld_ansdef);
					i++;
				}
			}
			continue;
		}

which looks, to me at least, the same as the code in a C CLI interface 
for the library, which does this:
         struct ld_defanswer **ans;
         int i = 0;

         if(!(ans = ld_define(conn, arg))) {
                 printf("No results for '%s'.\n", arg);
         } else {
                 while(ans[i]) {
                         printf("Found in dictionary '%s':\n", 
ans[i]->ld_ansdict);
                         printf("%s\n", ans[i]->ld_ansdef);
                         i++;
                 }
         }

except that mine segfaults and theirs doesn't :-(.  gdb doesn't offer 
much information:
dictstep> define
Define word: foo
Got HERE too, d[i]=1851596885

Program received signal EXC_BAD_ACCESS, Could not access memory.
0x000032bc in main (argc=1, argv=0xbffffb58) at dictstep.m:169
169                                             printf("Found 
definition %s:\n",d[i]->ld_ansdef);
(gdb) bt
#0  0x000032bc in main (argc=1, argv=0xbffffb58) at dictstep.m:169
(gdb) quit

I am lost.

Cheers,

Graham.
-- 
Graham Lee                       GPG Key ID: 01D5B9D8
UNIX Systems Manager,
Oxford Physics Practical Course
http://nextstep.sdf-eu.org               01865 273450
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.