Refcount doesn't work between threads #212

Open
EmielBruijntjes opened this Issue Dec 1, 2015 · 0 comments

Projects

None yet

1 participant

@EmielBruijntjes

As posing a pull request to solve it doesn't seem to work, i'll stick with a bug report instead:

/**
 *  JsonBug.cpp
 * 
 *  Example program that makes the JSON-C library crash because it does
 *  not do atomic refcounts.
 * 
 *  Compile with "g++ -std=c++11 jsonbug.cpp -ljson-c -lpthread"
 * 
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2015 Copernica BV
 */

/**
 *  Dependencies
 */
#include <json-c/json.h>
#include <thread>

/**
 *  Function that runs in a thread
 *  @param  object
 */
void threadfunction(json_object *object)
{
    // we are going to make a lot of copies of the object
    for (size_t i = 0; i < 1000; ++i)
    {
        // make a new reference to the original object
        auto *copy = json_object_get(object);

        // and get rid of the reference again
        json_object_put(copy);
    }

    // we no longer need the original object either
    json_object_put(object);
}

/**
 *  Main procedure
 *  @return int
 */
int main()
{
    // Just run the test a crazy amount of times - until one fails
    for (size_t i = 0; true; ++i)
    {
        // create a json object
        auto *obj = json_object_new_object();

        // start a thread with a new reference to the original json object
        std::thread t(std::bind(&threadfunction, json_object_get(obj)));

        // we no longer need the original object, the thread uses its own copy
        json_object_put(obj);

        // wait for the thread to be ready
        t.join();
    }

    // done
    return 0;
}

After a while it crashes with this error:

*** Error in `./a.out': double free or corruption (top): 0x00000000016f1aa0 ***
Aborted (core dumped)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment