Access violation writing location 0x0000000C


I'm struggling with one thing. I'm trying to call game's functions from python api via injected dll. It works sometimes but mostly throws an error:

Exception thrown at 0x1E07F731 (python27.dll) in 0xC0000005: Access violation writing location 0x0000000C

the above is log from debugger.

My code:

 std::vector<int> mobList()
        PyObject* mod = PyObject_GetAttrString(PyImport_AddModule("player"), "GetCharacterDistance");
        PyObject* mod2 = PyObject_GetAttrString(PyImport_AddModule("chr"), "GetInstanceType");
        PyObject* args = PyTuple_New(1);
        std::vector<int> mobs;
        for (int i = 1; i < 100000; i++) {
            try {
                PyTuple_SetItem(args, 0, PyInt_FromLong(i));
                PyObject* mob = PyObject_CallObject(mod, args);
                if (PyInt_AsLong(mob) > 0 && PyInt_AsLong(mob) < 400) {
                    PyObject* enemy = PyObject_CallObject(mod2, args);
                    if (PyInt_AsLong(enemy) == 0) {
                    if (enemy != NULL) {
                if (mob != NULL) {
            catch (int e) {
                std::cout << e << std::endl;
        return mobs;

and problematic part:

PyObject* mob = PyObject_CallObject(mod, args);

It sometimes works but mostly throw the mentioned error after random amount of iteration (50k, 60k, 70k etc). I tried everything and still cannot figure it out :/ Appreciate for any help.

asked on Stack Overflow Jul 14, 2020 by jsonikx • edited Jul 14, 2020 by jsonikx

1 Answer


I think @chi is right:

void Py_DECREF(PyObject *o)

Decrement the reference count for object o. The object must not be NULL; if you aren’t sure that it isn’t NULL, use Py_XDECREF(). If the reference count reaches zero, the object’s type’s deallocation function (which must not be NULL) is invoked.

void Py_XDECREF(PyObject *o)

Decrement the reference count for object o. The object may be NULL, in which case the macro has no effect; otherwise the effect is the same as for Py_DECREF(), and the same warning applies.


The deallocation function can cause arbitrary Python code to be invoked (e.g. when a class instance with a del() method is deallocated). While exceptions in such code are not propagated, the executed code has free access to all Python global variables. This means that any object that is reachable from a global variable should be in a consistent state before Py_DECREF() is invoked. For example, code to delete an object from a list should copy a reference to the deleted object in a temporary variable, update the list data structure, and then call Py_DECREF() for the temporary variable.

You should Py_XDECREF(args) after the for loop.

Rereading the docs I see an example:

arglist = Py_BuildValue("(l)", eventcode);
result = PyObject_CallObject(my_callback, arglist);
if (result == NULL)
    return NULL; /* Pass error back */
/* Here maybe use the result */

With an explicit note:

Note the placement of Py_DECREF(arglist) immediately after the call, before the error check!

So as you said in one of your comments, you may move the args creation inside the loop.

answered on Stack Overflow Jul 14, 2020 by Manuel • edited Jul 15, 2020 by Manuel

User contributions licensed under CC BY-SA 3.0