Skip to content

Conversation

@myk002
Copy link
Member

@myk002 myk002 commented Dec 28, 2024

current code (with a vector being constructed inline in a for loop initializer) produces the following error (for me, at least; seems to pass fine in CI)

FAILED: plugins/CMakeFiles/cursecheck.dir/cursecheck.cpp.o 
/usr/bin/ccache /usr/lib/ccache/bin/g++ -DDFHACK64 -DHAVE_CUCHAR -DLINUX_BUILD -DLUA_BUILD_AS_DLL -DPROTOBUF_USE_DLLS -D_LINUX -Dcursecheck_EXPORTS -I/home/myk/src/dfhack/depends/SDL2/SDL2-2.26.2/include -I/home/myk/src/dfhack/depends/protobuf -I/home/myk/src/dfhack/depends/lua/include -I/home/myk/src/dfhack/depends/md5 -I/home/myk/src/dfhack/depends/tinyxml -I/home/myk/src/dfhack/depends/lodepng -I/home/myk/src/dfhack/depends/clsocket/src -I/home/myk/src/dfhack/depends/xlsxio/include -I/home/myk/src/dfhack/library/include -I/home/myk/src/dfhack/library/proto -I/home/myk/src/dfhack/plugins/proto -I/home/myk/src/dfhack/library/depends/xgetopt -fvisibility=hidden -mtune=generic -Wall -Werror -Wl,--disable-new-dtags -Wno-unknown-pragmas -m64 -mno-avx -Wl,-z,defs -O2 -g  -std=c++20 -fPIC -MD -MT plugins/CMakeFiles/cursecheck.dir/cursecheck.cpp.o -MF plugins/CMakeFiles/cursecheck.dir/cursecheck.cpp.o.d -o plugins/CMakeFiles/cursecheck.dir/cursecheck.cpp.o -c /home/myk/src/dfhack/plugins/cursecheck.cpp
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/x86_64-pc-linux-gnu/bits/c++allocator.h:33,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/allocator.h:46,
                 from /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/list:63,
                 from /home/myk/src/dfhack/library/include/ColorText.h:28,
                 from /home/myk/src/dfhack/library/include/Console.h:27,
                 from /home/myk/src/dfhack/plugins/cursecheck.cpp:19:
In member function ‘void std::__new_allocator<_Tp>::deallocate(_Tp*, size_type) [with _Tp = df::unit*]’,
    inlined from ‘constexpr void std::allocator< <template-parameter-1-1> >::deallocate(_Tp*, std::size_t) [with _Tp = df::unit*]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/allocator.h:210:35,
    inlined from ‘static constexpr void std::allocator_traits<std::allocator<_Up> >::deallocate(allocator_type&, pointer, size_type) [with _Tp = df::unit*]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/alloc_traits.h:517:23,
    inlined from ‘constexpr void std::_Vector_base<_Tp, _Alloc>::_M_deallocate(pointer, std::size_t) [with _Tp = df::unit*; _Alloc = std::allocator<df::unit*>]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/stl_vector.h:390:19,
    inlined from ‘constexpr std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = df::unit*; _Alloc = std::allocator<df::unit*>]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/stl_vector.h:369:15,
    inlined from ‘constexpr std::vector<_Tp, _Alloc>::~vector() [with _Tp = df::unit*; _Alloc = std::allocator<df::unit*>]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/stl_vector.h:738:7,
    inlined from ‘DFHack::command_result cursecheck(DFHack::color_ostream&, std::vector<std::__cxx11::basic_string<char> >&)’ at /home/myk/src/dfhack/plugins/cursecheck.cpp:209:5:
/usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/new_allocator.h:172:33: error: ‘void operator delete(void*, std::size_t)’ called on pointer ‘<unknown>’ with nonzero offset 8 [-Werror=free-nonheap-object]
  172 |         _GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p, __n));
      |                                 ^
In member function ‘_Tp* std::__new_allocator<_Tp>::allocate(size_type, const void*) [with _Tp = df::unit*]’,
    inlined from ‘constexpr _Tp* std::allocator< <template-parameter-1-1> >::allocate(std::size_t) [with _Tp = df::unit*]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/allocator.h:198:40,
    inlined from ‘static constexpr _Tp* std::allocator_traits<std::allocator<_Up> >::allocate(allocator_type&, size_type) [with _Tp = df::unit*]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/alloc_traits.h:482:28,
    inlined from ‘constexpr std::_Vector_base<_Tp, _Alloc>::pointer std::_Vector_base<_Tp, _Alloc>::_M_allocate(std::size_t) [with _Tp = df::unit*; _Alloc = std::allocator<df::unit*>]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/stl_vector.h:381:33,
    inlined from ‘constexpr std::_Vector_base<_Tp, _Alloc>::pointer std::_Vector_base<_Tp, _Alloc>::_M_allocate(std::size_t) [with _Tp = df::unit*; _Alloc = std::allocator<df::unit*>]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/stl_vector.h:378:7,
    inlined from ‘constexpr void std::_Vector_base<_Tp, _Alloc>::_M_create_storage(std::size_t) [with _Tp = df::unit*; _Alloc = std::allocator<df::unit*>]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/stl_vector.h:398:44,
    inlined from ‘constexpr std::_Vector_base<_Tp, _Alloc>::_Vector_base(std::size_t, const allocator_type&) [with _Tp = df::unit*; _Alloc = std::allocator<df::unit*>]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/stl_vector.h:335:26,
    inlined from ‘constexpr std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = df::unit*; _Alloc = std::allocator<df::unit*>]’ at /usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/stl_vector.h:603:61,
    inlined from ‘DFHack::command_result cursecheck(DFHack::color_ostream&, std::vector<std::__cxx11::basic_string<char> >&)’ at /home/myk/src/dfhack/plugins/cursecheck.cpp:160:81:
/usr/lib/gcc/x86_64-pc-linux-gnu/13/include/g++-v13/bits/new_allocator.h:151:55: note: returned from ‘void* operator new(std::size_t)’
  151 |         return static_cast<_Tp*>(_GLIBCXX_OPERATOR_NEW(__n * sizeof(_Tp)));
      |                                                       ^

@myk002 myk002 merged commit 1d85610 into DFHack:develop Dec 28, 2024
14 checks passed
@myk002 myk002 deleted the myk_gcc_wiggy branch December 28, 2024 17:00

// check whole map if no unit is selected
for(df::unit *unit : selected_unit ? vector{ selected_unit } : world->units.all)
for(df::unit *unit : selected_unit[0] ? selected_unit : world->units.all)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have just made a temporary vector for the loop here. Small thing, though.

Copy link
Member Author

@myk002 myk002 Dec 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my first version did just that, but it looked messy (because of the required null check, but it's not like what I ended up with was entirely "clean" either), but maybe I can clean it up more

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#5150 is arguably cleaner

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while i'm not going to change the code we have now, this would arguably have been a good use for a std::span

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for my own education, how would that have worked? a ternary expression must have the same type in both of its branches. How would we wrap units.all to have the same type as the inline span?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was thinking that we knew where in the all vector the selected unit is, but we don't, so there's no value in having either the entire units vector or just part of it, which is what std::span would have done for us here

we want here an object that holds a reference to a vector which is either owning or nonowning, and the object knows which. this is because in the single-unit case, we need a temporary vector which will be destructed when we're done with it, but in the all-units case we do not want to destruct the vector to be iterated. there are ways to do this with shared_ptr but they're not less oblique than the solution used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants