swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Swarm-Support] Lisp saving objects subclassed from Arguments_c


From: Paul Johnson
Subject: Re: [Swarm-Support] Lisp saving objects subclassed from Arguments_c
Date: Wed, 18 Apr 2007 00:13:44 -0500
User-agent: Thunderbird 1.5.0.10 (X11/20070302)

Marcus G. Daniels wrote:
Marcus G. Daniels wrote:
I suspect it is because there is a function variable in Arguments_c, that serialization code doesn't know how to handle.
Nope, that's not it. Here's a test case that perhaps you can adapt to break and illustrate the problem. This only tests writing. It should write a complete myFile.scm file if you invoke it like this:

./a.out -X 12 -Y test
Interesting. Thank you again for pointing the way. I had not seen the usage of the addOption: method, I've always been using the older style C struct.

If I run your example with no command line arguments, I get the same kind of segmentation fault I got before, but if I run it with these two arguments as you do, it is OK. If I put in default values for those variables, then it works -- no crash--without the command line options. (Working example below). As long as all the variables have default values, all is well.

After experimentation, I found the cause of the crashes I was experiencing. I was initializing one string variable to nothing, as in.

args->gnorkGnork= NULL;

Any variable that does not have a valid non-NULL value will trigger a segmentation fault.

#include <simtools.h> // initSwarmArguments
#include <defobj.h>
#include <defobj/Arguments.h>
#include <misc.h> // atoi, strdup

@interface MyArguments: Arguments_c
{
 int foobar;
 char *gnorkGnork;
}
@end

@implementation MyArguments
+ createBegin: aZone
{
 MyArguments * args = [super createBegin: aZone];

 [args addOption: "foobar" key: 'X' arg: "value" flags: 0
   doc: "the important foobar value"
   group: 100];
 [args addOption: "gnorkgnork" key: 'Y' arg: "value" flags: 0
   doc: "the less crucial gnorkgnork value"
   group: 101];

 args->foobar = 10;
 args->gnorkGnork= "WHATEVER";
 // CRASH IF: args->gnorkGnork=NULL;
 return args;
}

- (int)parseKey: (int)key arg: (const char *)arg
{
 if (key == 'X') {
   foobar = atoi (arg);
   return 0;
 } else if (key == 'Y') {
   gnorkGnork = strdup (arg);
   return 0;
 }
 else
   return [super parseKey: key arg: arg];
}


- (void)saveParameters: (char*)aString
{
 char dataArchiveName[strlen (aString + 4 + 1)];
 id dataArchiver;

 sprintf (dataArchiveName,"%s.scm", aString);
 printf ("saving to `%s'\n", dataArchiveName);
 dataArchiver = [LispArchiver create: [self getZone]
                  setPath: dataArchiveName];

 [dataArchiver putShallow: "parameters" object: self];
 [dataArchiver sync];
 [dataArchiver drop];
}


@end

int
main (int argc, const char **argv)
{
 initSwarmArguments (argc, argv, [MyArguments class]);
 [(id) arguments saveParameters: "myFile"];
 return 0;
}

/*
Local Variables:
compile-command: "gcc -D_GNU_SOURCE address@hidden -g -I/usr/include/swarm -L/usr/lib/swarm -Wl,-rpath,/usr/lib/swarm test.m -lswarm -lobjc"
End:
*/





--
Paul E. Johnson                       email: address@hidden
Professor, Political Science          http://pj.freefaculty.org
1541 Lilac Lane, Rm 504 University of Kansas Office: (785) 864-9086
Lawrence, Kansas 66044-3177           FAX: (785) 864-5700



reply via email to

[Prev in Thread] Current Thread [Next in Thread]