Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 tasks
yuThomas opened this issue Jan 12, 2023 · 6 comments
Closed
2 tasks

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

yuThomas opened this issue Jan 12, 2023 · 6 comments
Labels
kind: bug state: needs more info the author of the issue needs to provide more details

Comments

@yuThomas
Copy link

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
Copy link
Owner

  • What compiler are you using?
  • Can you please provide a complete example?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Jan 14, 2023
@yuThomas
Copy link
Author

  • 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::ConcurrentQueuenlohmann::json* que;
std::vectormoodycamel::ProducerToken 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
Copy link
Contributor

gregmarr commented Feb 3, 2023

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
Copy link
Author

yuThomas commented Feb 4, 2023

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
Copy link
Owner

nlohmann commented Feb 4, 2023

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

@nlohmann
Copy link
Owner

@yuThomas Any updates?

@nlohmann nlohmann closed this as not planned Won't fix, can't repro, duplicate, stale Mar 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug state: needs more info the author of the issue needs to provide more details
Projects
None yet
Development

No branches or pull requests

3 participants