Specifically, the context is within VSCode, but it might not matter.
I have a function that runs correctly in the debugger, but not when chosing “Julia: Execute active File in REPL” or “Julia: Run file in New Process” from the pull-down menu. When I mean “correctly”, it (1) does what it is supposed to do (save a screen capture from and SDL window to a PNG file) and (2) does not crash.
However, when I try to run the non-debug compiled versions, I get a segmentation fault. What is it about the debugger that allows it to run successfully, yet fails miserably when compiled? I’d prefer both behaviors to be identical, so that I could (1) use the debugger to figure out what is wrong, and (2) not have any surprises when using the compiled version.
I’m not sure if it matters (as I’m looking for the “meta” answer “why would it work in the debugger but not elsewhere”), but here is the source code of the function (with some lines omitted for brevity). The gist is that it flips an image to the screen, and optionally, saves a screenshot to disk prior to flipping the image to the screen :
function flip(win::Window, screenshot::Bool)
if screenshot == true
w = Ref{Cint}()
h = Ref{Cint}()
SDL_GL_GetDrawableSize(win.win, w, h)
mySurfacePtr = SDL_CreateRGBSurfaceWithFormat(0, w[], h[], 32, SDL_PIXELFORMAT_ARGB8888); # create empy mySurfacePtr
dereference(T::DataType, ptr::Ptr) = unsafe_load(Ptr{T}(ptr)) # generic function
surfaceStruct = dereference(SDL_Surface, mySurfacePtr) # try to put the C-based SDL_Surface struct into a Julia struct
result = SDL_RenderReadPixels(win.renderer, C_NULL, SDL_PIXELFORMAT_ARGB8888, surfaceStruct.pixels, surfaceStruct.pitch);
if result != 0
error("SDL_RenderReadPixels failed: ", unsafe_string(SDL_GetError()) )
end
surfaceStructPtr = Ptr{SDL_Surface}(pointer_from_objref(Ref(surfaceStruct)))
#----
newFileName = "screenshot.png"
result = IMG_SavePNG(surfaceStructPtr, newFileName)
if result != 0
error("IMG_SavePNG failed: ", unsafe_string(SDL_GetError()) )
end
end
SDL_RenderPresent(win.renderer) # Present the backbuffer (memory) to the screen
SDL_PumpEvents()
SDL_SetRenderDrawColor(win.renderer, win.color[1], win.color[2], win.color[3], 255) # sets window background color
SDL_RenderClear(win.renderer) # Clears the window in memory, getting it read for the next drawing session.
end
A snapshot of the error:
[90270] signal (11.2): Segmentation fault: 11
in expression starting at /Users/MyAccount/.julia/dev/PsychoJL/Examples/complex occlusions/ComplexOcclusionSearchMain.jl:613
IMG_SavePNG_RW at /Users/MyAccount/.julia/artifacts/5ebb4b9114677f6288d27366c03555cca9d7ab98/lib/libSDL2_image-2.0.601.2.0.dylib (unknown line)
IMG_SavePNG at /Users/MyAccount/.julia/packages/SimpleDirectMediaLayer/wjMsP/src/LibSDL2.jl:5846 [inlined]
flip at /Users/MyAccount/.julia/dev/PsychoJL/src/window.jl:267
doATrial at /Users/MyAccount/.julia/dev/PsychoJL/Examples/complex occlusions/ComplexOcclusionSearchMain.jl:352
main at /Users/MyAccount/.julia/dev/PsychoJL/Examples/complex occlusions/ComplexOcclusionSearchMain.jl:83
unknown function (ip: 0x2a93a8463)
_jl_invoke at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-honeycrisp-HL2F7YQ3XH.0/build/default-honeycrisp-HL2F7YQ3XH-0/julialang/julia-release-1-dot-10/src/gf.c:0 [inlined]