Csound Csound-dev Csound-tekno Search About

[Csnd] Segmentation fault with widget library running Lua inside Csound

Date2013-08-10 17:04
FromMark Brophy
Subject[Csnd] Segmentation fault with widget library running Lua inside Csound
Attachmentsluagui.csd  
I'm trying to use the wxLua library inside Csound but I receive an error message, "Csound tidy up: Segmentation violation" when I try to set the background color of a button. I'm loading the GUI in the init function of a lua_iopcall opcode, which tries to display a tabbed notebook with a button on the first page. The program does not crash when I comment out the line that sets the background color of the button. How can this be fixed?

<CsoundSynthesizer>
<CsInstruments>
lua_opdef "luaGUI", {{
package.cpath = package.cpath..";./?.dll;./?.so;../lib/?.so;../lib/vc_dll/?.dll;../lib/bcc_dll/?.dll;../lib/mingw_dll/?.dll;"
require ("wx")
local ffi = require("ffi")
local string = require("string")
local csoundAPI = ffi.load('csound64.dll.5.2')
ffi.cdef[[
    struct luaGUI_arguments {double *out; double *stringout; char *stringin; };
]]
ID_PARENT_SCROLLEDWINDOW = 1000
ID_NOTEBOOK = 923
ID_BUTTON   = 1000

frame = nil

function CreateControlsWindow(parent)
-- Create tabbed notebook with button on first page
    local scrollWin = wx.wxScrolledWindow(parent, ID_PARENT_SCROLLEDWINDOW,
                                    wx.wxDefaultPosition, wx.wxDefaultSize,
                                    wx.wxHSCROLL + wx.wxVSCROLL)

    local control = nil    
    local p = wx.wxPanel(scrollWin, wx.wxID_ANY)
    control = wx.wxNotebook(p, ID_NOTEBOOK, wx.wxDefaultPosition, wx.wxSize(900, 450))
    button = wx.wxButton(p, ID_BUTTON, '', wx.wxPoint(20, 20), wx.wxSize(40, 40))
    local black = wx.wxColour(0, 0, 0)
    --button:SetBackgroundColour(black)      -- This line causes segmentation fault
end

function LoadGUI()
    -- create the wxFrame window
    frame = wx.wxFrame( wx.NULL,                   -- no parent for toplevel windows
                        wx.wxID_ANY,               -- don't need a wxWindow ID
                        "My Minimal Demo",         -- caption on the frame
                        wx.wxPoint(300, 100),
                        wx.wxSize(450, 450),       -- set the size of the frame
                        wx.wxDEFAULT_FRAME_STYLE ) -- use default frame styles

    -- create a single child window, wxWidgets will set the size to fill frame
    panel = wx.wxPanel(frame, wx.wxID_ANY)

    -- create a simple status bar
    frame:CreateStatusBar(1)
    frame:SetStatusText("Welcome to wxLua.")
    controlsWin = CreateControlsWindow(frame)
    frame:Show(true)
end

function luaGUI_init(csound, opcode, carguments)
    local arguments = ffi.cast("struct luaGUI_arguments *", carguments)
    ffi.copy(arguments.stringout, 'Hello, world!')
LoadGUI()
wx.wxGetApp():MainLoop()
    return 0
end
}}

instr 43
    iresult = 0
    Stringin = "stringin"
    Stringout = "stringout"
    lua_iopcall "luaGUI", iresult, Stringout, Stringin, p2, p3
    prints Stringout
endin
</CsInstruments>

<CsScore>
f0 1
i 43 0 0.1
e
</CsScore>
</CsoundSynthesizer>