wizzomafizzo / lispbuilder

Automatically exported from code.google.com/p/lispbuilder
0 stars 0 forks source link

sbcl: c-string decoding error #11

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

(sdl:with-init ()
  (sdl:window 500 500 :resizable t
          :title-caption "without setting the title caption,
                              the error pops up from the first start")
  (sdl:with-events ()
    (:quit-event () t)
    (:video-resize-event ()
             (sdl:resize-window 500 500))
      (:idle ()
         ;run external program, for example bash command "ls"
         (sb-ext:run-program "/bin/bash" (list "-c" "ls")))))

1. Running the above program without setting the title caption
2. Resizing the window
3. Restarting the programm exactly 4 times

What is the expected output? What do you see instead?

The expected output is of course a black window.
Instead i get the following Error Message:

c-string decoding error (:external-format :UTF-8):
  the octet sequence 3 cannot be decoded.
   [Condition of type SB-INT:C-STRING-DECODING-ERROR]
Backtrace:
  0: (SB-INT:C-STRING-DECODING-ERROR :UTF-8 1)
  1: (SB-IMPL::READ-FROM-C-STRING/UTF-8 #.(SB-SYS:INT-SAP #X080E3918)
CHARACTER)
  2: (SB-INT:C-STRINGS->STRING-LIST #<SB-ALIEN-INTERNALS:ALIEN-VALUE :SAP
#X080C59D8 :TYPE (* (C-STRING :EXTERNAL-FORMAT :DEFAULT :ELEMENT-TYPE
CHARACTER))>)
  3: (RUN-PROGRAM "/bin/bash" ("-c" "ls"))[:EXTERNAL]
  4: ((LAMBDA (SB-PCL::.PV. SB-PCL::.NEXT-METHOD-CALL. SB-PCL::.ARG0.
SB-PCL::.ARG1.)) ..)
  5: ((SB-PCL::FAST-METHOD LISPBUILDER-SDL::PROCESS-TIMESTEP :AROUND
(LISPBUILDER-SDL::FPS-MANAGER T)) ..)
  6: ((LAMBDA ()))
  7: (SB-INT:SIMPLE-EVAL-IN-LEXENV ..)

What version of the product are you using? On what operating system?

- Lispbuilder-SDL 0.9.8
- SBCL 1.0.34
- Ubuntu 9.10

Please provide any additional information below.

After writing this, i realized that the solution is here:
https://bugs.launchpad.net/sbcl/+bug/460455
But the fix doesn't seem to work?? 

However replacing "putenv" in
./lispbuilder-sdl/cffi/stdinc.lisp:    (defcfun ("putenv" SDL-put-env)
by "setenv" seems to solve the problem.

Damn it, a full day wasted :)

Original issue reported on code.google.com by stoned-j...@gmx.net on 15 Jan 2010 at 4:44

GoogleCodeExporter commented 9 years ago
I can't reproduce this on SBCL win32/1.0.29. I'll download 1.0.32 and try it 
out. Some 
comments;

#1 - A carriage return in the Window :title-caption is never good.
#2 - Anything in :IDLE is called once per frame. The default frame-rate is 
30fps. This 
means that (sb-ext:run-program "/bin/bash" (list "-c" "ls")) is being run 30 
times per 
second!

Original comment by Clruok...@gmail.com on 17 Jan 2010 at 3:20

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
"putenv" takes a string of the form NAME=VALUE, which is what is passed into 
the 
foreign function.  int putenv(char *string);

"setenv" does not match the foreign function definition so I'm not sure why 
this would 
work. I would expect this to cause an immediate visit to the debugger.

Original comment by Clruok...@gmail.com on 17 Jan 2010 at 3:43

GoogleCodeExporter commented 9 years ago

Original comment by Clruok...@gmail.com on 17 Jan 2010 at 6:34

GoogleCodeExporter commented 9 years ago
This is over my head, and i don't really understand the issue.
#1 I'm sorry, i don't even know what a carriage return is.
#2 Wait is set true by defaul, so run-programm shoud waits the call is finished
   And this doesn't seem to be the problem, since it's also accours if it's called
only once at the beginning.

I really don't know, but a Faré in the above link states:
Note that setenv and unsetenv seem to be better interfaces to provide than 
putenv,
and do the malloc'ing for you.

Thank you for caring, but i'm fine as long as it works for me.

Original comment by stoned-j...@gmx.net on 17 Jan 2010 at 3:07

GoogleCodeExporter commented 9 years ago
Carriage return is the newline in :title-caption between "without setting the 
title 
caption," and "the error pops up from the first start". This should be; 

:title-caption "without setting the title caption, the error pops up from the 
first 
start"

Original comment by Clruok...@gmail.com on 17 Jan 2010 at 3:49

GoogleCodeExporter commented 9 years ago
Ah OK, I didn't realize that sb-ext:run-program was a synchronous call.

I still don't understand how renaming 'putenv' to 'setenv' works for you. 
Because now 
the code will be making the following call...

(setenv <string>)

...when in fact the correct API is for 'setenv' should be 

(setenv <string> <string> <int>)

I agree that setenv is the better call to use though.

Original comment by Clruok...@gmail.com on 17 Jan 2010 at 3:57

GoogleCodeExporter commented 9 years ago
Hey, this still hasn't been fixed and it's been a year.

In fact, this problem has broader scope than an obscure error in SBCL on Linux. 
 It happens on Macs, too, and it's caused by _anything_ that sets up an SDL 
display surface.  Such as, for example, the "mandelbrot" demonstration program 
in lispbuilder-sdl-examples.  Try it, run "(sdl-examples:mandelbrot)" a few 
times and then call run-program on anything; likely you will get a 
c-string-decoding-error.

I ran into this problem when attempting to write a game.  It was pretty 
irritating in itself (I like accessing the clipboard), but more seriously, it 
led me to think (as a hopeful newcomer to lispbuilder-sdl) that this library 
wasn't well-supported on Macs, or on SBCL, or that SBCL wasn't well-supported 
on Macs, or something--whatever the reason, I shouldn't be surprised if 
everything else is also subtly broken, and if I want to write my cross-platform 
Lisp game I should look elsewhere.  Only my failure to find acceptable 
alternatives led me to come back here and try to fix this stuff.  (And lo and 
behold, it turns out that it's a simple misuse of "putenv".)  I think this is 
pretty bad and it should be fixed (Priority-Low, my invalid argument to 
putenv!).

Quick fix: Replace the definition of "sdl-put-env" in 
lispbuilder-sdl/cffi/stdinc.lisp with this:

(defun sdl-put-env (string)
  #-sbcl
  (cond
    ((cffi:foreign-symbol-pointer "SDL_putenv" :library 'sdl)
     (cffi:foreign-funcall-pointer (cffi:foreign-symbol-pointer "SDL_putenv") () :string string :int))
    ((cffi:foreign-symbol-pointer "putenv")
     (cffi:foreign-funcall-pointer (cffi:foreign-symbol-pointer "putenv") () :string string :int)))
  #+sbcl
  (let ((p (position #\= string)) ;taken from sbcl-1.0.47/contrib/sb-posix/interface.lisp
          (n (length string)))
      (if p
          (if (= p n)
              (sb-posix:unsetenv (subseq string 0 p))
              (sb-posix:setenv (subseq string 0 p) (subseq string (1+ p)) 1))
          (error "Invalid argument to putenv: ~S" string))))

It works perfectly now, as far as I can tell.

Future fix: Ideally you wouldn't need platform-specific code there.  Does 
"putenv" (as opposed to "setenv") work on other Lisp implementations?  If so, 
perhaps it could be considered a problem with SBCL, and they might fix it.  Or 
the CFFI library might provide a unified "putenv" function (they already have 
platform-specific code).  Or lispbuilder-sdl could just switch over to "setenv" 
for all implementations (probably using CFFI to call it).

I suspect this last solution is both easiest and best.  I get the impression 
that the only drawback of setenv is that it allocates memory (copying the env 
string), and that conceivably the GC might not know how to free it (?), but 
that even if that happens it's unlikely to be a problem, because a program is 
likely to use setenv only a couple of times during startup and not in the main 
program.  (In this case, it's used by "set-video-driver", "set-audio-driver", 
and "set-window-position".)

Original comment by JohntheS...@gmail.com on 22 Apr 2011 at 5:17

GoogleCodeExporter commented 9 years ago
The GC not knowing how to free memory is a problem. It may decide to free the 
object minutes into the game, and crash suddenly and unexpectedly. Very hard to 
debug.

I want to play around with SBCL some more to see what's up. Thanks for the 
feedback.

Original comment by Clruok...@gmail.com on 24 Apr 2011 at 5:11