[cffi-devel] Interface problem in LispWorks but not in SBCL

Martin Simmons martin at lispworks.com
Thu Nov 24 19:55:54 UTC 2011


I've tried to replicate what you are doing, by adding the following hacks to
your function in a file called gtest.cpp:

#include <iostream>
#include <sstream>
#include <string>

class GRelation {
private:
  int _arity;
public:
  int arity() { return _arity; };
  GRelation(int arity) { _arity = arity; };
};

std::ostream& operator<<(std::ostream& out, GRelation &g)
{
  out << "<GRelation" << g.arity() << ">";
  return out;
};

I then passed it through SWIG 1.3.31:

swig -cffi -c++ -module gtestswig gtest.cpp
cat gtest.cpp gtest_wrap.cxx > gtest_code_and_wrap.cpp

Beware that I've not read much C++ or SWIG documentation, so the above might
be somewhat bogus!

Following that, it ran without errors with CFFI 0.10.6 on Mac OS X 10.6.8 like
this:

(asdf:initialize-source-registry
 '(:source-registry
   (:directory "~/cffi_0.10.6")
   (:directory "~/alexandria")
   (:directory "~/babel_0.3.0")
   (:directory "~/trivial-features_0.1")
   :inherit-configuration))
(asdf:load-system :cffi)
(compile-file "gtestswig" :load t)
(defsystem gtestswig (:c-default-options "-lstdc++")
  :members (("gtest_code_and_wrap.cpp" :type :c-file)))
(compile-system 'gtestswig :force t :load t)
(test)

-- 
Martin Simmons
LispWorks Ltd
http://www.lispworks.com/




>>>>> On Wed, 23 Nov 2011 16:20:51 +0100, Sascha Van Cauwelaert said:
> 
> Thank you for your answer.
> 
> Actually, I have an additional clue, which make me think that it is not
> related with garbage collection. If, in Lispworks, I use a wrapped function
> on the pointer to the C object (e.g. to get the arity), it works fine !
> 
> It seems that I just can't call a function which print some information
> about the object in a C++ function, wrapped to be used in LispWorks ... If I
> do it, I get the error :
> 
> lispworks-personal-6-0-1-macos-universal(49641,0xb0314000) malloc: *** error for object 0x17bfc0f8: pointer being freed was not allocated
> *** set a breakpoint in malloc_error_break to debug
> 
> I don't get how it can work in SBCL and not in LispWorks, and I have no clue
> because I don't know the internal implementation of LispWorks.
> 
> I would really appreciate a little bit of your help.
> 
> Regards,
> 
> Sascha Van Cauwelaert
> 
> 
> Le 22 nov. 2011 à 21:16, Martin Simmons a écrit :
> 
> >>>>>> On Mon, 21 Nov 2011 10:28:56 +0100, Sascha Van Cauwelaert said:
> >> 
> >> Hello everybody,
> >> 
> >> I am using SWIG to generate CFFI code to be used into LispWorks 6. The
> >> interface is between C++ and Common Lisp. Then, I try to call that function
> >> from LispWorks :
> >> 
> >> std::string  test() {
> >> 	freopen ("/tmp/mystdout.txt","w",stdout);
> >> 	freopen ("/tmp/mystderr.txt","w",stderr);
> >> 	std::cout << "Redirected stdout." << std::endl;
> >> 	std::cerr << "Redirected stderr." << std::endl;
> >> 	
> >> 	GRelation testgr(3);
> >> 
> >> 	std::cout << "test ! " << std::endl;
> >> 	std::cerr << "test cerr! " << std::endl;
> >> 	std::cout << testgr.arity() << std::endl;
> >> 	std::cerr << testgr.arity() << std::endl;	
> >> 	std::cout << "test after! " << std::endl;
> >> 	std::cerr << "test cerr after! " << std::endl;
> >> 	std::stringstream os;
> >> 	os << testgr << std::endl ;
> >> 	return os.str();
> >> }
> >> 
> >> As you can see, I just create one object and print lots of stuff, including
> >> an object attribute. Everything I print before trying to print the attribute
> >> prints ok in the redirected outputs. The attribute is not printed and
> >> nothing else afterwards. I also get the following error when I add testgr to
> >> the stringstream os :
> >> 
> >> lispworks-personal-6-0-1-macos-universal(52881,0xb0314000) malloc: *** error for object 0x17be20f8: pointer being freed was not allocated
> >> 
> >> This is what happen in LispWorks. If I compile directly in C++, no
> >> problem. If I use SBCL (other Common Lisp implementation) instead of
> >> LispWorks, everything works correctly ! The problem is that I have to use
> >> LispWorks ...
> >> 
> >> Does anybody has had a similar problem ? I guess I am not the only one who
> >> tried to call some C++ from LispWorks. Any clue is very welcome. I think
> >> maybe the problem can be linked to the garbage collection from LispWorks but
> >> I don't know much about this.
> >> 
> >> I thank you in advance for your help, I am really stuck here. 
> > 
> > I don't see how garbage collection can cause it, because malloc is not
> > affected by the LispWorks GC (unless something is doing bad things with
> > finalization routines).
> > 
> > -- 
> > Martin Simmons
> > LispWorks Ltd
> > http://www.lispworks.com/
> > 
> > _______________________________________________
> > cffi-devel mailing list
> > cffi-devel at common-lisp.net
> > http://lists.common-lisp.net/cgi-bin/mailman/listinfo/cffi-devel
> 




More information about the cffi-devel mailing list