Inconsistency of "var" type between executions


#1

I have a dynamic library A.so that compiles chaiscript this way:

std::shared_ptr < chaiscript::Module > createChaiscriptStdlib() {
return chaiscript::Std_Lib::library();
}

Then I have a program B for testing purposes that links to A.so and when I execute a function in a script it works as expected.

def update() {
var dt = 0.1f;
var speed = 50.0f;
var movement = speed * dt;
print("dt: " + to_string(dt) + " speed: " + to_string(speed) + " movement: " + to_string(movement));

prints:

 dt: 0.1 speed: 50 movement: 5

BUT!
Now I’m linking A.so inside a QT application, the script is the same, but the output is this:

 dt: 0 speed: 50 movement: 0

When using B program that links to the same A.so lib, it prints the floats OK.
When using C program (QT) that links to the same A.so, floats BAD :confused:

Do you know what’s causing that strange behavior? I’m really out of ideas…
(I tried to change “var” -> “auto” but I’m getting the same behavior)

Thank you.


#2

That’s very odd. That implies that somehow Qt is messing with the built in types, but that seems unlikely.

I need you to share a very small test case that reproduces the issue, so I can test it.

-Jason


#3

OK, I will try to make an small test case.


#4

Hello Lefticus,

Here you can download the test case I did:
https://drive.google.com/file/d/0B3ZVsrvmIvN7Wl9JZ1FnR3BDNGs/view?usp=sharing

I’m using Debian Stretch.
(I included the include directory of chaiscript inside the library folder)
Steps to reproduce that:

#1- go to library folder and build the test shared library:
gcc -g -std=c++11 -shared -o libtestchai.so -fPIC testchai.cpp

#2- copy the library and header to /usr/local/lib/…
sudo cp testchai.hpp /usr/local/include/
sudo cp libtestchai.so /usr/local/lib/
sudo ldconfig

#3- Compile and execute the sample program that links to our library
# go to test_program folder then:
g++ -g -std=c++11 -o test_program test_program.cpp -L/usr/local/lib -ltestchai -lpthread -ldl
./test_program

test int: 256
test float: 1.8

Wonderful World :slight_smile:

#4 Now install qt5 and qtcreator (I’m using Debian Stretch)
sudo apt-get install qt5-default
sudo apt-get install qtcreator

#5 Open the qtcreator project, compile and execute

qtcreator
"Open Project"-> “…/QTExe/testQt” folder -> “testQt.pro” file

now build (control+B) and execute (F5) and you will see in the terminal that will open:

test int: 256
test float: 1

Argggh … :frowning:

I Hope that you can find what’s causing this. I don’t have the suficient skill/knowledge to do that sorry :(.
If you have questions, I will answer as fast as I can.

Thank you!


#5

More Info. My brother tested the test case on MAC (clang) and it works perfect. mmmm :confused:


#6

I haven’t tried had a chance to look into this yet, but my main assumption is that Qt is messing with the IO stream settings and the values are correct, but printing incorrectly?


#7

It has to be something else because we are calling a C++ function that moves a sprite based on that variable, and on Linux is receiving a 0 (it doesn’t move), and the same version and code, in MACOS (clang) receives a float 0.x (it’s moving).


#8

I’m unable to reproduce this so far, so I need you to try a few things.

Please verify:

ChaiScript version
Qt version
G++ version

Also:

Please try updating to the latest chaiscript 5.8.0, and please try using clang on linux to see if either of those make a difference.

-Jason


#9

Tested now:
Chaiscript updated to 5.8.0.
QT version (debian repository version): QTCreator (3.6.0) , QT (5.5.1)
G++ (debian repository version): g++ (Debian 5.3.1-7) 5.3.1 20160121

And same results: test_program OK, QT float truncated BAD.

Now I will try with clang… I never used it…


#10
  • Chaiscript: 5.8.0.
  • QT version (debian repository version): QTCreator (3.6.0) , QT (5.5.1)
  • clang version 3.6.2-3 (tags/RELEASE_362/final) (based on LLVM 3.6.2)

All rebuilt using clang and the same results: test_program OK, QT float truncated BAD. :confused:

Which versions are you using to try to reproduce that?
If you know where inside chaiscript code I should have to look using the debugger I can try to debug to see which parameters the print function receive.


#11

gcc 5.2.1
qt 5.4.2
chaiscript 5.8.0/5.7.0 just to make sure

The question really isn’t where it’s being printed, the question is where it’s being parsed. And that will be much easier for you to put a break point in on.

you can also try things like:

print(1.8 > 1.0)
print(1.8 > 1)
print(1.8 < 2)
print(1.8 < 2.0)
print(1.8 == 1.8)

And see what you get


#12

Thanks! I tried with :

print(1.8 > 1.0)
print(1.8 > 1)
print(1.8 < 2)
print(1.8 < 2.0)
print(1.8 == 1.8)

And I think that confirms that this is a strange problem/bug…
test_program:

daniel@debian:~/workspace/test_chaiscript/test_program$ ./test_program
true
true
true
true
true

Now the same with QT:

false
false
true
true
true

(the last true: 1.8 == 1.8 is true but i think it assumes an 1 == 1)

I will go to the debugger… : /


#13

Ok I found the “problem” but don’t know how to fix it.
(I modified a bit your function to see it step by step)

    std::string sub_str_ = t_val.substr(0,i);
    if (float_) {
        float f =  std::stof(sub_str_);
        return const_var(f);
    } else if (long_) {
        long double ld =  std::stold(sub_str_);
        return const_var(ld);
    } else {
        double d =  std::stod(sub_str_);
        return const_var(d);
    }

The function std::stof(sub_str_), when sub_str = “1.8” is returning 1

Mmm… any ideas ? xDDD this is crazy…

EDIT: Ok confirmed, it’s not a bug in chaiscript :smile: . Making a new clean QT Project and:

#include < QCoreApplication >
#include < iostream >
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    std::string num("1.8");
    float f =  std::stof(num);
    std::cout << num << " :: " << f << std::endl;
    return a.exec();
}

outputs:
1.8 :: 1

Then if I do this:

#include < QCoreApplication >
#include < iostream >
int main(int argc, char *argv[])
{
    std::string num("1.8");
    float f =  std::stof(num);
    std::cout << num << " :: " << f << std::endl;
    return 1
}

outputs:
1.8 :: 1.8

This is insane, how can QT change the behavior of this function? :confused:


#14

Apparently exactly one other person on the internet had the same issue 2 years ago:

It must be something goofy with that specific build of Qt, since I don’t see it. QApplication does lots of things like looking for Qt plugins to load. There’s a chance a plugin that’s loaded is redefining its own version of std::stof but that seems like a very rude thing to do.


#15

You could try posting a question to stackoverflow with that simple example or to Qt’s issue tracker.


#16

Thank you for your help, seriously.


#17

As a guess, might Qt be changing your locale such that it thinks you are in Europe where 1.8 would be presented as 1,8 (one comma eight)


#18

I just confirmed that std::stof does respect the locale. I’m going to have to address that in ChaiScript.


#19

@roig try:

std::setlocale(LC_ALL, "POSIX");

immediately after your call to QCoreApplication


#20

annnnnddddd…

#include < QCoreApplication>

#include < string>

#include < iostream >

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::string num(“1,8”); //NOTE 1,8 not 1.8
float f = std::stof(num);
std::cout << num << " :: " << f << std::endl;
return 0;
}

Outputs:
1,8 :: 1.8

So you are right… it’s the locale…