Unfortunately the solution here (moving the call to `gtk_check_menu_item_set_active()` to the popup creation) is wrong because the setting in the menu no longer follows the value set by the plugins preferences dialog.
In my first commit this not exist.
The variable `popup_menu` needs to be moved from being a static in the `on_button_press()` function to a file wide static that is created in the plugin init, (as @codebrainz said) properly freed in plugin free and then it can be updated by the plugin preferences when thats changed and by the menu function when its changed in the menu and not on each time the menu is displayed.
I don't understand, did you mean to declare the popup_menu in the plugin init and then passing as function argument? Or global (static) to file as my first commit?