1. Forum moved (you can use login and pass from old forum)
  2. Many discussions moved to the bugtracker

Coding questions

Discussion in 'Coding' started by Istrebitel, Aug 26, 2016.

  1. Istrebitel

    Istrebitel Well-Known Member Official Developer Administrator

    Joined:
    Aug 8, 2016
    Messages:
    189
    Likes Received:
    84
    Best Answers:
    1
    I hope some experienced C++ people can advise me. I keep running into some bizzare errors I just can't comprehend and online search yields nothing. Since I'm mostly coding when others are absent at IRC, I'm going to post them here, hopefully someone can help.
     
  2. Istrebitel

    Istrebitel Well-Known Member Official Developer Administrator

    Joined:
    Aug 8, 2016
    Messages:
    189
    Likes Received:
    84
    Best Answers:
    1
    Okay, so this is beyond wierd
    I found a small bug in progress bar calculation formula for manufacturing and decided to fix it.
    After I did it I decided to test it. Ran the game. When I tried to assign manufacturing topic I got exception ACCESS VIOLATION in research.cpp:234. It has this:

    LogAssert(state->player->balance >= topic->cost);

    This is inside setResearch function, it checks wether an engineering topic is affordable (as we can only call setResearch if we selected an affordable project).
    It was giving access violation despite the fact that during break, both state->player->balance and topic->cost were evaluating normally.

    I added these two lines before that line it to debug the problem:

    LogWarning("%d", state->player->balance);
    LogWarning("%d", topic->cost);

    Error was gone.

    I removed them.

    Error did not return! There's no error anymore!
    I now have exactly the same code that was giving the error repeatedly over 7 runs!
    WTF?!? How's that even possible?
     
  3. Istrebitel

    Istrebitel Well-Known Member Official Developer Administrator

    Joined:
    Aug 8, 2016
    Messages:
    189
    Likes Received:
    84
    Best Answers:
    1
    I have this in BattleMapPartType class declaration:

    public:
    enum class Type {...types listed...}
    static const std::map<Type, UString> TypeMap;
    Type type;

    In the following code, map_part->type is of type "BattleMapPartType"

    Now, why can I do:
    std::map<BattleMapPartType::Type,UString> m = BattleMapPartType::TypeMap;
    BattleMapPartType::Type t = map_part->type->type;
    UString s = m[t];

    But I cannot do:
    UString s = BattleMapPartType::TypeMap[map_part->type->type]; // gives error "binary '[': no operator found which takes a left-hand operand of type .... (or there is no acceptable conversion)"

    But then I can do:
    UString s = BattleMapPartType::TypeMap.at(map_part->type->type);

    WTF?
     
  4. Treonin

    Treonin Registered

    Joined:
    Feb 1, 2015
    Messages:
    31
    Likes Received:
    2
    Best Answers:
    0
    When you are using a reference which references nothing, then it may have memory garbage, which sometimes points at forbidden memory location. It may be why it sometimes gives you the error and sometimes it does not. Be sure every reference and variable is assigned.

    [] is used to access lists (or if you override it you can use it for something else). Are you sure BattleMapPartType::TypeMap is a list?

    I am not an expert, but maybe it could help you a bit.
     
  5. Istrebitel

    Istrebitel Well-Known Member Official Developer Administrator

    Joined:
    Aug 8, 2016
    Messages:
    189
    Likes Received:
    84
    Best Answers:
    1
    well, as you see, I can do this

    std::map<BattleMapPartType::Type,UString> m = BattleMapPartType::TypeMap;
    BattleMapPartType::Type t = map_part->type->type;
    UString s = m[t];

    without a problem. So, if I assign BattleMapPartType::TypeMap to a variable ofthe same type, then I can use [] on it. But until I assign it to a variable, even though it is still of a type std::map, I cannot use [] on it.
     
  6. Treonin

    Treonin Registered

    Joined:
    Feb 1, 2015
    Messages:
    31
    Likes Received:
    2
    Best Answers:
    0
    I think it is because TypeMap is a const and it does not allow []. When I removed the const the [] started working for me.

    The new variable you make is not a const, and you copy the contents into it.

    [] has the potential to add a new element, so for a const it is not allowed.
     
  7. Istrebitel

    Istrebitel Well-Known Member Official Developer Administrator

    Joined:
    Aug 8, 2016
    Messages:
    189
    Likes Received:
    84
    Best Answers:
    1
    Can I pass constructor parameters to mksp<Battle>()? I tried to make Battle have several const members and I need to initialise them in the constructor. However, when I try to pass consturctor parameters to mksp, it bugs out for some reason, and doesn't allow that.
     
  8. Treonin

    Treonin Registered

    Joined:
    Feb 1, 2015
    Messages:
    31
    Likes Received:
    2
    Best Answers:
    0
    Did you try using initialization list? I think I remember that it needs to be used for const members.

    Function() : var1(7)
    {
    }
     
  9. JonnyH

    JonnyH Well-Known Member Official Developer Administrator

    Joined:
    Jul 17, 2014
    Messages:
    185
    Likes Received:
    39
    Best Answers:
    0
    mksp<T> is just an alias for std::make_shared<T>, so it should be able to call constructors fine:

    If I have a constructor MyClass(TypeA a, TypeB b);

    mksp<MyClass>(variableA, variableB);

    should call that
     
  10. Istrebitel

    Istrebitel Well-Known Member Official Developer Administrator

    Joined:
    Aug 8, 2016
    Messages:
    189
    Likes Received:
    84
    Best Answers:
    1
    can it accept consts? It gives an error, something about const getting lost?

    Another one:

    When building soluition, I am getting error at this point:

    Code:
    template <typename T, typename Generator>
    T probabilityMapRandomizer(Generator &g, const std::map<T, float> &probabilityMap)
    {
    	if (probabilityMap.empty())
    	{
    at the "T"

    The error:

    Severity Code Description Project File Line Suppression State
    Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int test_rng E:\Projects\GitHub\OpenApoc\library\xorshift.h 101

    However, re-building makes the error disappear! Wtf?
     
  11. Istrebitel

    Istrebitel Well-Known Member Official Developer Administrator

    Joined:
    Aug 8, 2016
    Messages:
    189
    Likes Received:
    84
    Best Answers:
    1
    also, travis gives this error....

    /home/travis/build/Istrebitel/OpenApoc/library/xorshift.h:101:53: error: C++
    requires a type specifier for all declarations
    T probabilityMapRandomizer(Generator &g, const std::map<T, float> &proba...
    ~~~~~ ^
    /home/travis/build/Istrebitel/OpenApoc/library/xorshift.h:101:53: error:
    parameter declarator cannot be qualified


    and for some reason, this:

    [ 27%] Building CXX object forms/CMakeFiles/OpenApoc_Forms.dir/checkbox.cpp.o

    /usr/include/boost/any.hpp:243:16: runtime error: downcast of address 0x60200000e7b0 which does not point to an object of type 'any::holder<const basic_string<char> >'

    0x60200000e7b0: note: object is of type 'boost::any::holder<std::string>'

    02 00 00 49 b0 e5 e6 19 be 2a 00 00 98 ad 00 00 b0 60 00 00 03 00 00 00 00 00 00 04 10 00 00 00

    ^~~~~~~~~~~~~~~~~~~~~~~

    vptr for 'boost::any::holder<std::string>'

    SUMMARY: AddressSanitizer: undefined-behavior /usr/include/boost/any.hpp:243:16 in

    /usr/include/boost/any.hpp:243:73: runtime error: member access within address 0x60200000e7b0 which does not point to an object of type 'any::holder<const basic_string<char> >'

    0x60200000e7b0: note: object is of type 'boost::any::holder<std::string>'

    02 00 00 49 b0 e5 e6 19 be 2a 00 00 98 ad 00 00 b0 60 00 00 03 00 00 00 00 00 00 04 10 00 00 00

    ^~~~~~~~~~~~~~~~~~~~~~~

    vptr for 'boost::any::holder<std::string>'

    SUMMARY: AddressSanitizer: undefined-behavior /usr/include/boost/any.hpp:243:73 in
     
  12. Istrebitel

    Istrebitel Well-Known Member Official Developer Administrator

    Joined:
    Aug 8, 2016
    Messages:
    189
    Likes Received:
    84
    Best Answers:
    1
    okay, that's solved, another one:

    When I try this:
    fw().data->loadImage(UString::format("PCK:xcom3/tacdata/oshadow.pck:xcom3/tacdata/oshadow.tab:%d:xcom3/tacdata/tactical.pal", i));

    I get an error: "this was 0x08" inside UString comparison function

    When I try this:
    fw().data->loadImage(UString::format("PCK:xcom3/tacdata/gameobj.pck:xcom3/tacdata/gameobj.tab:%d:xcom3/tacdata/tactical.pal", i));

    I works!

    At the same place. The files "oshadow" do exist. And they work, since they were mass extracted by JonnyH tool.

    wtf???

    PS: Dug a bit further and it seems that our PCK reader has problems reading this file. Strange. I'm fully updated from pmprog/master.
     
  13. Istrebitel

    Istrebitel Well-Known Member Official Developer Administrator

    Joined:
    Aug 8, 2016
    Messages:
    189
    Likes Received:
    84
    Best Answers:
    1
    aaand since it bugs with stratico file as well, I guess the code has not been added yet, the one used to dump images?
     

Share This Page