nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
42.97k stars 6.72k forks source link

how does a normal basic_json<> object cuase assertion `false` #3918

Closed yuThomas closed 1 year ago

yuThomas commented 1 year ago

Description

string_t dump(const int indent = -1, const char indent_char = ' ', const bool ensure_ascii = false, const error_handler_t error_handler = error_handler_t::strict) const

cause "assertion false" coredump

Reproduction steps

  1. i use moody::cocurrentqueue as the data queue, and nlohmann::json* as the enqueued item in one thread, key-value with type string
  2. use another thread consume from queue, and call method nlohmann::json.dump, i'm sure all items has been allcated, but error above occurred frequently.

Expected vs. actual results

Expected: a normal json string or empty

actual results: assertion failed

Minimal code example

No response

Error messages

void nlohmann::detail::serializer<BasicJsonType>::dump(const BasicJsonType&, bool, bool, unsigned int, unsigned int) [with BasicJsonType = nlohmann::basic_json<>]: Assertion `false' failed.

Compiler and operating system

linux 5.4.17-2011.6.2.el7uek.x86_64

Library version

3.10.5

Validation

nlohmann commented 1 year ago
yuThomas commented 1 year ago
  • What compiler are you using?
  • Can you please provide a complete example?

sorry too late, example as below:

int main(int argc, char* argv) { moodycamel::ConcurrentQueue<nlohmann::json> que; std::vector vecP;

auto *data = new nlohmann::json;
(*data)["ac"] = "1";
(*data)["state"] = "1";
// more else key-value items

for( int i=0; i<3; i++ )
{
    vecP.emplace_back(moodycamel::ProducerToken(que));
    std::thread th([&]{
        std::vector<nlohmann::json*> dataBuck(100);
        size_t num;
        for(;;)
        {
            num = que.try_dequeue_bulk_from_producer(vecP[i], &dataBuck[0], 100);
            if ( num > 0 )
            {
                for ( size_t j=0; j<num; j++)
                {
                    // do something else
                }
            }
        }
    });
    th.detach();
}

for( int i=0; i<8; i++)
{
    std::thread th([=,&que]{
        for(;;)
        {
            std::hash<std::string> h;
            que.enqueue(vecP[h(data->dump()+ std::to_string(i))/3], data);
        }
    });
    th.detach();
}

getchar();

return 0;

}

gregmarr commented 1 year ago

This error is not related to the library at all.

        std::hash<std::string> h;
        que.enqueue(vecP[h(...) / 3], data);

You want % not / here to convert the hash into a vector index. Even better would be

vecP[h(...) % vecP.size()]
yuThomas commented 1 year ago

This error is not related to the library at all.

        std::hash<std::string> h;
        que.enqueue(vecP[h(...) / 3], data);

You want % not / here to convert the hash into a vector index. Even better would be

vecP[h(...) % vecP.size()]

sorry to give the wrong code ,actually, i do use % instead of /

nlohmann commented 1 year ago

Can you then please share a complete working example, including main and everything?

nlohmann commented 1 year ago

@yuThomas Any updates?