why crash immediatly when adding an notifier to the scene via ctypes DLL

I try to add an notifier to the scene,but it crash immediatly.
here is my code in dll:


void BLI_addtail(ListBase*listbase, void*vlink)
    {
    Link*link =(Link*) vlink;
    if (link == NULL) return;

    link->next = NULL;
    link->prev = (Link*)listbase->last;

    if (listbase->last)
        {
        ((Link*)listbase->last)->next = link;}
    if (listbase->first == NULL)
        {
        listbase->first = link;
        }
    listbase->last = link;
    }

void WM_event_add_notifier(const bContext*C, unsigned int type, void*reference)
    {
    ARegion*ar;wmWindowManager*wm= CTX_wm_manager(C);
    
    if (wm_test_duplicate_notifier(wm, type, reference))
        {
        return;
        }
    wmNotifier*note =(wmNotifier*)MEM_callocN(sizeof(wmNotifier), "notifier");//calloc memory
    printf("0 wm=  %d 
",note->wm);
    note->wm = wm;
    //printf("1 wm=  %s · %d 
",note->wm->id.name,note->wm);
   
    BLI_addtail(&note->wm->queue, note);//crash here!!!

    note->window = CTX_wm_window(C);
    
    ar = CTX_wm_region(C);
    if (ar)
        note->swinid = ar->swinid;
    
    note->category = type & NOTE_CATEGORY;
    note->data = type & NOTE_DATA;
    note->subtype = type & NOTE_SUBTYPE;
    note->action = type & NOTE_ACTION;
    
    note->reference = reference;
    
    }
    
//----the fuction to add an notifer--------------------------
extern "C" __declspec(dllexport)   void AddNotifer(bContext*C)
    {
    Main*bmain=CTX_data_main(C);
    BKE_main_lock(bmain);
    Scene*scene=C->data.scene;
    WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);//ΧΧcrash
    BKE_main_unlock(bmain);
    }

this C++ code will compiled to dll,and call by python sctrpt in blender via ctypes.
I notice that the BLI_addtail function cause the crash,if I disable this function It will not crash ,but why?
what’s the different of immpliment this inside blender.exe from outside via calling a dll?

Crashes usually happen when there is an illegal access to memory which means access memory that is freed or that is not used or that the application is not allowed to access. Which in turn means a problem with a pointer or more.

Finding crashes will definitely require debugging the dll and executable with C debugger like GDB or LLDB. You have to make sure your manage memory properly. Pointers are by far the biggest pain when coding in C. Ctypes may be a python library but are basically c coding too.

Also why you cram your pointer definitions ? add a space to make code more readable. Also just because you call the DLL from python does not mean it is also linked to the blender executable. The DLL will exist in a separate place with no access to the Blender internals and memory. One of the pains of Blender not relying on DLLs.

Unless you pass it Blender memory data via python but Blender rarely gives Python access to its internals when it does its either read only or very limited to avoid crashes like this happening.