pcbnew action plugin can crash kicad (lp:#1843700)

[Original report](https://bugs.launchpad.net/bugs/1843700) created by **Dino Ghilardi (dino-ghilardi)**
```
In master branch a python script can crash pcbnew.

Tested with ReplicateLayout plugin from

https://github.com/MitjaNemec/Kicad_action_plugins

To replicate it:

-Install the replicateLayout plugin
-Open the attached project
-Open pcbnew
-Select R15
-Tools->External Plugins->Replicate Layout
-in the dialog in "Hierarchy level select file5D...FA.sch
-Click OK
-Now the layout seems correctly replicated but exiting pcbnew (don't save)
there is a segfault.
If, after running the script you try to undo it with ctrl-z you can get
some other weird behaviour (like some new components without any label and
not selectable on the board).


On 5.1.2 and 5.1.4 stable versions the script seems to work correctly
(although it needed some modifications since the "Flip" function in dev
version gets 3 arguments and in stable it did get two. May be overloading
it with the old behaviour can help in not braking old scripts when V6 will
be released)


gdb reports:

Thread 1 "kicad" received signal SIGSEGV, Segmentation fault.
0x00007ffff3b11078 in main_arena () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) backtrace
#0  0x00007ffff3b11078 in main_arena () from /lib/x86_64-linux-
gnu/libc.so.6
#1  0x00007fffd34da849 in PICKED_ITEMS_LIST::ClearListAndDeleteItems() ()
from /home/dinoghi/SANDBOXKICAD5/usr/bin/_pcbnew.kiface
#2  0x00007fffd2e74e80 in
PCB_SCREEN::ClearUndoORRedoList(UNDO_REDO_CONTAINER&, int) () from
/home/dinoghi/SANDBOXKICAD5/usr/bin/_pcbnew.kiface
#3  0x00007fffd31b09c9 in BASE_SCREEN::ClearUndoRedoList() () from
/home/dinoghi/SANDBOXKICAD5/usr/bin/_pcbnew.kiface
#4  0x00007fffd2deb985 in PCB_EDIT_FRAME::Clear_Pcb(bool, bool) () from
/home/dinoghi/SANDBOXKICAD5/usr/bin/_pcbnew.kiface
#5  0x00007fffd2e1e5ef in PCB_EDIT_FRAME::OnCloseWindow(wxCloseEvent&) ()
from /home/dinoghi/SANDBOXKICAD5/usr/bin/_pcbnew.kiface
#6  0x00007ffff634440e in wxAppConsoleBase::CallEventHandler(wxEvtHandler*,
wxEventFunctor&, wxEvent&) const ()
   from /usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#7  0x00007ffff64c9ea5 in
wxEvtHandler::ProcessEventIfMatchesId(wxEventTableEntryBase const&,
wxEvtHandler*, wxEvent&) ()
   from /usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#8  0x00007ffff64c9f9b in wxEventHashTable::HandleEvent(wxEvent&,
wxEvtHandler*) () from /usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#9  0x00007ffff64ca34b in wxEvtHandler::TryHereOnly(wxEvent&) () from
/usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#10 0x00007fffd3462e99 in EDA_BASE_FRAME::ProcessEvent(wxEvent&) () from
/home/dinoghi/SANDBOXKICAD5/usr/bin/_pcbnew.kiface
#11 0x00007ffff64ca153 in wxEvtHandler::DoTryChain(wxEvent&) () from
/usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#12 0x00007ffff64ca435 in wxEvtHandler::ProcessEvent(wxEvent&) () from
/usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#13 0x00007ffff64ca1a7 in wxEvtHandler::SafelyProcessEvent(wxEvent&) ()
from /usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#14 0x00007ffff6e57be7 in wxWindowBase::Close(bool) () from
/usr/lib/x86_64-linux-gnu/libwx_gtk2u_core-3.0.so.0
#15 0x00007ffff634440e in wxAppConsoleBase::CallEventHandler(wxEvtHandler*,
wxEventFunctor&, wxEvent&) const ()
   from /usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#16 0x00007ffff64c9ea5 in
wxEvtHandler::ProcessEventIfMatchesId(wxEventTableEntryBase const&,
wxEvtHandler*, wxEvent&) ()
   from /usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#17 0x00007ffff64c9f9b in wxEventHashTable::HandleEvent(wxEvent&,
wxEvtHandler*) () from /usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#18 0x00007ffff64ca34b in wxEvtHandler::TryHereOnly(wxEvent&) () from
/usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#19 0x00007fffd3462e99 in EDA_BASE_FRAME::ProcessEvent(wxEvent&) () from
/home/dinoghi/SANDBOXKICAD5/usr/bin/_pcbnew.kiface
#20 0x00007ffff64ca153 in wxEvtHandler::DoTryChain(wxEvent&) () from
/usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#21 0x00007ffff64ca435 in wxEvtHandler::ProcessEvent(wxEvent&) () from
/usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#22 0x00007ffff6e562f8 in wxWindowBase::TryAfter(wxEvent&) () from
/usr/lib/x86_64-linux-gnu/libwx_gtk2u_core-3.0.so.0
#23 0x00007ffff64ca1a7 in wxEvtHandler::SafelyProcessEvent(wxEvent&) ()
from /usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#24 0x00007ffff6dfb2df in wxMenuBase::SendEvent(int, int) () from
/usr/lib/x86_64-linux-gnu/libwx_gtk2u_core-3.0.so.0
#25 0x00007ffff6cdec1b in ?? () from /usr/lib/x86_64-linux-
gnu/libwx_gtk2u_core-3.0.so.0
#26 0x00007ffff2c35f75 in g_closure_invoke () from /usr/lib/x86_64-linux-
gnu/libgobject-2.0.so.0
#27 0x00007ffff2c47f82 in ?? () from /usr/lib/x86_64-linux-
gnu/libgobject-2.0.so.0
#28 0x00007ffff2c50bdc in g_signal_emit_valist () from
/usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#29 0x00007ffff2c50fbf in g_signal_emit () from /usr/lib/x86_64-linux-
gnu/libgobject-2.0.so.0
---Type <return> to continue, or q <return> to quit---
#30 0x00007ffff337662e in gtk_widget_activate () from
/usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#31 0x00007ffff32714dd in gtk_menu_shell_activate_item () from
/usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#32 0x00007ffff3271846 in ?? () from /usr/lib/x86_64-linux-
gnu/libgtk-x11-2.0.so.0
#33 0x00007ffff325f7bc in ?? () from /usr/lib/x86_64-linux-
gnu/libgtk-x11-2.0.so.0
#34 0x00007ffff2c35f75 in g_closure_invoke () from /usr/lib/x86_64-linux-
gnu/libgobject-2.0.so.0
#35 0x00007ffff2c4837d in ?? () from /usr/lib/x86_64-linux-
gnu/libgobject-2.0.so.0
#36 0x00007ffff2c5067f in g_signal_emit_valist () from
/usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
#37 0x00007ffff2c50fbf in g_signal_emit () from /usr/lib/x86_64-linux-
gnu/libgobject-2.0.so.0
#38 0x00007ffff33778ac in ?? () from /usr/lib/x86_64-linux-
gnu/libgtk-x11-2.0.so.0
#39 0x00007ffff325df84 in gtk_propagate_event () from
/usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0
#40 0x00007ffff325e33b in gtk_main_do_event () from /usr/lib/x86_64-linux-
gnu/libgtk-x11-2.0.so.0
#41 0x00007ffff2ed3cbc in ?? () from /usr/lib/x86_64-linux-
gnu/libgdk-x11-2.0.so.0
#42 0x00007ffff21ac7f7 in g_main_context_dispatch () from
/lib/x86_64-linux-gnu/libglib-2.0.so.0
#43 0x00007ffff21aca60 in ?? () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#44 0x00007ffff21acd82 in g_main_loop_run () from /lib/x86_64-linux-
gnu/libglib-2.0.so.0
#45 0x00007ffff325d3b7 in gtk_main () from /usr/lib/x86_64-linux-
gnu/libgtk-x11-2.0.so.0
#46 0x00007ffff6c41e65 in wxGUIEventLoop::DoRun() () from
/usr/lib/x86_64-linux-gnu/libwx_gtk2u_core-3.0.so.0
#47 0x00007ffff638627b in wxEventLoopBase::Run() () from
/usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#48 0x00007ffff634b776 in wxAppConsoleBase::MainLoop() () from
/usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#49 0x00005555555d3536 in ?? ()
#50 0x00007ffff63d7cd0 in wxEntry(int&, wchar_t**) () from
/usr/lib/x86_64-linux-gnu/libwx_baseu-3.0.so.0
#51 0x00005555555af568 in ?? ()
#52 0x00007ffff37972e1 in __libc_start_main (main=0x5555555af540, argc=1,
argv=0x7fffffffe158, init=<optimized out>, fini=<optimized out>,
    rtld_fini=<optimized out>, stack_end=0x7fffffffe148) at ../csu/libc-
start.c:291
#53 0x00005555555bac8a in _start ()

*******************************************

Application: Pcbnew
Version: (5.99.0-114-g271465a64-dirty), release build
Libraries:
    wxWidgets 3.0.2
    libcurl/7.52.1 OpenSSL/1.0.2s zlib/1.2.8 libidn2/0.16 libpsl/0.17.0
(+libidn2/0.16) libssh2/1.7.0 nghttp2/1.18.1 librtmp/2.3
Platform: Linux 4.9.0-8-amd64 x86_64, 64 bit, Little endian, wxGTK
Build Info:
    wxWidgets: 3.0.2 (wchar_t,wx containers,compatible with 2.8) GTK+ 2.24
    Boost: 1.69.0
    OpenCASCADE Community Edition: 6.8.0
    Curl: 7.52.1
    Compiler: GCC 6.3.0 with C++ ABI 1010

Build settings:
    KICAD_SCRIPTING=ON
    KICAD_SCRIPTING_MODULES=ON
    KICAD_SCRIPTING_PYTHON3=OFF
    KICAD_SCRIPTING_WXPYTHON=ON
    KICAD_SCRIPTING_WXPYTHON_PHOENIX=OFF
    KICAD_SCRIPTING_ACTION_MENU=ON
    BUILD_GITHUB_PLUGIN=ON
    KICAD_USE_OCE=ON
    KICAD_USE_OCC=OFF
    KICAD_SPICE=ON
```
Original tags: pcbnew python

Story points: 6.0