From jan@swi.psy.uva.nl Thu Sep 27 11:26:57 2001
Received: from gollem.swi.psy.uva.nl (root@gollem [145.18.152.30])
	by swi.psy.uva.nl (8.11.2/8.11.2) with ESMTP id f8R9Qvv05819;
	Thu, 27 Sep 2001 11:26:57 +0200 (MET DST)
Received: from localhost (localhost [[UNIX: localhost]])
	by gollem.swi.psy.uva.nl (8.11.2/8.11.2/SuSE Linux 8.11.1-0.5) id f8R9QvZ26741;
	Thu, 27 Sep 2001 11:26:57 +0200
From: Jan Wielemaker <jan@swi.psy.uva.nl>
Organization: SWI, University of Amsterdam
To: "Nissim Broudo" <nissim@math.ufl.edu>, <prolog@swi.psy.uva.nl>
Subject: Re: [SWIPL] PL_unify
Date: Thu, 27 Sep 2001 11:22:04 +0200
X-Mailer: KMail [version 1.0.29.2]
Content-Type: text/plain
References: <002801c118ea$4530bb40$b153e30a@computer>
In-Reply-To: <002801c118ea$4530bb40$b153e30a@computer>
MIME-Version: 1.0
Message-Id: <01092711265709.25592@gollem>
Content-Transfer-Encoding: 8bit

On Mon, 30 Jul 2001, Nissim Broudo wrote:
>
>I'm calling SWI-Prolog from C.  I need to use the bindings produced by one query in the input to a second query and I am encountering a problem.  I am using PL_unify.  The problem I am encountering is in a call like this:
>
>foreign_t my_query(term_t in) {
>
>int rval;
>term_t temp;
>char *st = "atom_string";
>PL_unify_atom_chars(temp, st);
>rval = PL_unify(temp,in);
>
>return rval; }
>
>Prolog reports an error:
>
>1 ?- my_query(X).
>
>ERROR: my_query/1: Caught signal 11 (segv)

temp is not initialised.  Use 

term_t temp = PL_new_term_ref();

None of these functions can be used on a not-initialised term_t
variable.  A term-reference is the address of a cell on the local
stack that holds the actual Prolog term.  This way Prolog knows
which terms are referenced by foreign code and should thus be
updated by the garbage collector.  I think the idea for this
originated at Quintus.  If you do a lot with foreign code make
sure you understand term-references.

	Regards --- Jan

