unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* smob pointer moving
@ 2003-02-02  3:51 Noah Roberts
  2003-02-02  6:38 ` Steve Tell
  0 siblings, 1 reply; 3+ messages in thread
From: Noah Roberts @ 2003-02-02  3:51 UTC (permalink / raw)


I have been trying to figure out how to get a C++ class working in guile 
through smobs.  I have suceeded in getting it to create an instance, and 
the print call works appropriately.  However, when I am calling a 
specific method (only one tried so far) the pointer to the object has 
been altered so that it no longer points at the correct 'this'.  Here is 
some relevant pieces of code:

class Database
{
  char *filename;
 public:
  Database(char *name); // prints filename
  ~Database() { printf("Database freed.\n"); }
  void open(); // prints filename
};


#define DB(X) ((Database*)(SCM_CDR(X)))

int print_db(SCM obj, SCM port, scm_print_state *pstate)
{
  char string[32];
  sprintf(string, "#<db %p>", DB(obj));
  scm_puts(string, port);
  return 1;
}
SCM db_create_new(SCM filename)
{
  Database *db;
  SCM smob_blob;
  printf("db_create_new called.\n");
  if (SCM_STRINGP(filename))
    {
      db = new Database(SCM_STRING_CHARS(filename));
      SCM_NEWCELL(smob_blob);
      SCM_SETCAR(smob_blob, db_id);
      SCM_SETCDR(smob_blob, (SCM)(db));
     
      return smob_blob;
    }
  else return SCM_BOOL_F;
}

SCM db_open(SCM obj)
{
  Database *db = DB(db);
  printf("Database is at %p\n", db); 
      db->open();
  printf("db_open called.\n");
  return SCM_EOL;
}

  scm_c_define_gsubr("db-create-new", 1, 0, 0, (SCM (*)(...))db_create_new);
  scm_c_define_gsubr("db-open", 1, 0, 0, (SCM (*)(...))db_open);

  scm_set_smob_print(db_id, print_db);

Right now, Database simply prints out some information.  When db-open is 
called from guile I get a crash or "frm" is the filename spit out by 
Database.

Here is some sample output:
guile> (define d (db-create-new "SNTH"))
db_create_new called.
Database SNTH requested at 0x809d210.
guile> d
#<db 0x809d210>
guile> (db-open d)
Database is at 0x805ed78
DB frm OPEN
db_open called.
()
guile> d
#<db 0x809d210>
guile>

as you can see, when db-open is called the pointer returned by the DB 
macro is not correct, why?

Thanks for any help,
NR



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: smob pointer moving
  2003-02-02  3:51 smob pointer moving Noah Roberts
@ 2003-02-02  6:38 ` Steve Tell
  2003-02-02  6:56   ` Noah Roberts
  0 siblings, 1 reply; 3+ messages in thread
From: Steve Tell @ 2003-02-02  6:38 UTC (permalink / raw)
  Cc: Guile Mailing List

On Sat, 1 Feb 2003, Noah Roberts wrote:

> I have been trying to figure out how to get a C++ class working in guile 
> through smobs.  I have suceeded in getting it to create an instance, and 
> the print call works appropriately.  However, when I am calling a 
> specific method (only one tried so far) the pointer to the object has 
> been altered so that it no longer points at the correct 'this'.  Here is 
> some relevant pieces of code:

> SCM db_open(SCM obj)
> {
>   Database *db = DB(db);

I think you mean DB(obj) above.   Typo in email, or perhaps your real
problem?

One other thing wrong that I see here:  You're not checking that 
obj really is a database smob before casting it and chasing the
(possibly random) pointer.

Following the example of some guile-based code, I always define a C macro
for this (named MyType_P in the scheme style), and sometimes also a
"VALIDATE_ARG" macro that does the comparison and throws a guile error if
my produre is called with some other scheme value.

Assuming db_id is the value you got back from scm_make_smob_type(), I'd
write somthing like this (adapted on the fly to your example, may have
minor errors):

#define Database_P(X) (SCM_NIMP(X) && SCM_CAR(X) == (SCM)db_id)

#define VALIDATE_ARG_Database(pos,scm) \
  do { \
  if (!Database_P(scm)) scm_wrong_type_arg(FUNC_NAME,pos,scm); \
  } while (0)

(recent docs indicate that SCM_SMOB_PREDICATE() and SCM_ASSERT() are
available for this, probably a better choice.)

>   printf("Database is at %p\n", db); 
>       db->open();
>   printf("db_open called.\n");
>   return SCM_EOL;
> }

See also:  "Defining New Types (Smobs)" in the guile reference
manual (section 18.3 in the guile-1.6.3 manual), and note

"18.2.6 Signalling Type Errors:
"Every function visible at the Scheme level should aggressively check the
types of its arguments, to avoid misinterpreting a value, and perhaps
causing a segmentation fault. Guile provides some macros to make this
easier."

I'm sure someone will (please) correct me if I've misstated anything above
:-)

Steve

--
Steve Tell  tell@telltronics.org 



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: smob pointer moving
  2003-02-02  6:38 ` Steve Tell
@ 2003-02-02  6:56   ` Noah Roberts
  0 siblings, 0 replies; 3+ messages in thread
From: Noah Roberts @ 2003-02-02  6:56 UTC (permalink / raw)


Steve Tell wrote:

>>SCM db_open(SCM obj)
>>{
>>  Database *db = DB(db);
>>    
>>
>
>I think you mean DB(obj) above.   Typo in email, or perhaps your real
>problem?
>
Actually it looks like it was very possibly a typo in code.  The 
argument was named db to begin with and I had decided to change it in 
favor of having that name for the converted variable - probably forgot 
to change the above.  Pretty embarasing actually :P

>One other thing wrong that I see here:  You're not checking that 
>obj really is a database smob before casting it and chasing the
>(possibly random) pointer.
>
Yeah, I hadn't gotten to that part in my searching - seems that finding 
thurough information on guile is a bit tough.  It is all out there, but 
scattered between various mailing lists, info pages, tutorials, and of 
course the source code itself, but I am beginning to get all the parts I 
require mapped out.

>(recent docs indicate that SCM_SMOB_PREDICATE() and SCM_ASSERT() are
>available for this, probably a better choice.)
>
That is what I found and preliminary testing indicates that it is what 
is "supposed" to be used....
SCM_ASSERT(SCM_SMOB_PREDICATE(db_id, obj), obj, SCM_ARG1, "db-open");

It was actually in this document that I found SCM_SMOB_DATA as well.

>>  printf("Database is at %p\n", db); 
>>      db->open();
>>  printf("db_open called.\n");
>>  return SCM_EOL;
>>}
>>
Thanks for the info, it looks like it was plain sloppyness and not 
missinformation or lack of understanding that caused my problem.  After 
seing what caused the error I think it best to use SCM_SMOB_DATA in all 
cases so that my code is not relying on a specific implementation of 
smobs in case they change in later releases.  That probably won't 
happen, but better to use the existing macro than to make up my own, I 
just have to know about them first :P

Thanks,
NR



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-02-02  6:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-02-02  3:51 smob pointer moving Noah Roberts
2003-02-02  6:38 ` Steve Tell
2003-02-02  6:56   ` Noah Roberts

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).