# Segment fault when calling shared library from node plotting png using CairoMakie library

**URL:** https://discourse.julialang.org/t/segment-fault-when-calling-shared-library-from-node-plotting-png-using-cairomakie-library/130794
**Category:** New to Julia
**Tags:** plotting
**Created:** [July 16, 2025, 10:04pm UTC](https://discourse.julialang.org/t/segment-fault-when-calling-shared-library-from-node-plotting-png-using-cairomakie-library/130794 "2025-07-16T22:04:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![zkbitcoin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zkbitcoin/32/217764_2.png) [@zkbitcoin](https://discourse.julialang.org/u/zkbitcoin)
#### Post date: [July 16, 2025, 10:04pm UTC](https://discourse.julialang.org/t/segment-fault-when-calling-shared-library-from-node-plotting-png-using-cairomakie-library/130794/1 "2025-07-16T22:04:09Z")

</div>

have simple .jl (below are source files) program flow is: test.js calls c program which calls julia program

1. when using svg extension

const result = generate\_c(‘td’, 250, 250, ‘test\_draw.svg’);

image is generated

1. when using png extension get exception related to png library

const result = generate\_c(‘td’, 250, 250, 'test\_draw.png);

Segmentation fault

in expression starting at none:0

deflateReset at node (unknown line)

png\_deflate\_claim at /home/server/go/src/blockbook/ml/ca/server/recursiveDynamic/dist/share/julia/artifacts/79994d5aa1ffc0e9e42f2348a7f9c584dbe20204/lib/libpng16.so (unknown line)

this error is related to calling build so library from node

followed project structure PackageCompiler.jl for libs (al works just fine if one is calling it from a c program but not as shared (so) library

see also make file (below)

test\_draw.jl

using CairoMakie  
CairoMakie.activate!()

export generate\_test\_draw

function generate\_test\_draw(width::Integer, height::Integer, output\_path::String)  
fig = Figure(size = (width, height))  
ax = Axis(fig[1, 1]; title = “Simple Square”)

```
# Define square corners manually
points = [Point2f(0.25, 0.25), Point2f(0.75, 0.25),
          Point2f(0.75, 0.75), Point2f(0.25, 0.75)]

poly!(ax, points, color = :blue)

save(output_path, fig)

```

end

rdlib.js

export generate

using Base: Cint, Cstring, unsafe\_string

include(“cantor\_set.jl”)  
include(“hilbert\_curve.jl”)  
include(“test\_draw.jl”)

# C-callable function via @ccallable

Base.@ccallable function generate(ctype::Cstring, width::Cint, height::Cint, o::Cstring)::Cint  
ctype\_str = unsafe\_string(ctype)  
output\_str = unsafe\_string(o)

```
if ctype_str == "td"
    generate_test_draw(width, height, output_str)
else
    @warn "Unknown ctype: $ctype_str"
    return 1 # Non-zero to indicate error
end

return 0

```

end

c program

generate.c

#include \<stdio.h\>  
#include “julia\_init.h”  
#include “rdlib.h”

int generate\_c(const char \*ctype, int width, int height, const char \*output\_path)  
{  
init\_julia(0, NULL); // Initialize Julia runtime; adjust if you need argc/argv

```
int status = 0;
status = generate(ctype, width, height, output_path);
if (status != 0) {
    fprintf(stderr, "generate_%s failed with status %d\n", ctype, status);
}

shutdown_julia(0); // Shutdown Julia runtime

return status;

```

}

* * *

test.js

const koffi = require(‘koffi’);

// Load the shared library  
const lib = koffi.load(‘./dist/lib/librdlib\_c.so’);

// Define the C-callable function signature  
const generate\_c = lib.func(‘int generate\_c(const char\*, int, int, const char\*)’);

// Call the function  
const result = generate\_c(‘td’, 250, 250, ‘test\_draw.svg’);

console.log(‘generate() returned:’, result);

– Make file

.DEFAULT\_GOAL := all

JULIA ?= julia  
DLEXT := (shell (JULIA) --startup-file=no -e ‘using Libdl; print(Libdl.dlext)’)

TARGET=“dist”

RDLIB\_INCLUDES = (TARGET)/include/julia\_init.h (TARGET)/include/rdlib.h  
RDLIB\_PATH := (TARGET)/lib/librdlib.(DLEXT)

build-library: build/build.jl src/rdlib.jl  
(JULIA) --startup-file=no --project=. -e 'using Pkg; Pkg.instantiate()' (JULIA) --startup-file=no --project=build -e ‘using Pkg; Pkg.instantiate(); include(“build/build.jl”)’

INCLUDE\_DIR = $(TARGET)/include

build-c-library:  
gcc -shared -fPIC generate.c -o (TARGET)/lib/librdlib\_c.so \ -I(INCLUDE\_DIR) -L$(TARGET)/lib -ljulia -lrdlib

build-executable:  
gcc generate.c -o generate -I$(INCLUDE\_DIR) -L$(TARGET)/lib -ljulia -lrdlib

all: build-library build-c-library build-executable

clean:  
(RM) \*~ \*.o \*.(DLEXT)  
(RM) -Rf (TARGET)

.PHONY: build-library build-executable clean all

---

<div class="post-metadata">

### Author: ![hz-xiaxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hz-xiaxz/32/209585_2.png) [@hz-xiaxz](https://discourse.julialang.org/u/hz-xiaxz)
#### Post date: [July 17, 2025, 1:05am UTC](https://discourse.julialang.org/t/segment-fault-when-calling-shared-library-from-node-plotting-png-using-cairomakie-library/130794/2 "2025-07-17T01:05:16Z")

</div>

Don’t think I can help but a slight advice, you may envolope your code snippets in ``` notations to improve readibility and possibility that people come in to help 🙂

---

<div class="post-metadata">

### Author: ![zkbitcoin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zkbitcoin/32/217764_2.png) [@zkbitcoin](https://discourse.julialang.org/u/zkbitcoin)
#### Post date: [July 20, 2025, 1:07pm UTC](https://discourse.julialang.org/t/segment-fault-when-calling-shared-library-from-node-plotting-png-using-cairomakie-library/130794/3 "2025-07-20T13:07:35Z")

</div>

@hz-xiaxz hi sorry about this , I entered github bug (could be just usage) its all formatted there (here edit just expired on me)

> <https://github.com/MakieOrg/Makie.jl/issues/5195>
>
> \- \[\] what version of Makie are you running? (\`\]st -m Makie\`)
> 
> (@v1.10) pkg\> st …-m Makie
> Status \`~/.julia/environments/v1.10/Manifest.toml\`
> \[ee78f7c6\] Makie v0.24.3
> 
> 
> 
> \- \[\] can you reproduce the bug with a fresh environment ? (\`\]activate --temp; add Makie\`)
> 
> yes, every time 
> 
> \- \[\] What platform + GPU are you on?
> 
> Ubuntu 24.04.2 GeForce GTX 1080 Ti
> 
> \- \[\] Which backend are you using? (GLMakie, CairoMakie, WGLMakie, RPRMakie)
> 
> CairoMakie
> 
> \- \[\] Please provide an minimal example of the failing code. If the problem is with the visualization, please provide a picture or video of it.
> 
> 
> have simple .jl (below are source files) program flow is: test.js calls generate.c which calls julia program rdlib.js which calls test\_draw.jl
> 
> 1. when using svg extension 
> 
> \`\`\`
> const result = generate\_c('td', 250, 250, 'test\_draw.svg');
> \`\`\`
> 
> image is generated 
> 
> 2. when using png extension get exception related to png library
> 
> \`\`\`
> const result = generate\_c('td', 250, 250, 'test\_draw.png);
> 
> Segmentation fault
> 
> in expression starting at none:0
> 
> deflateReset at node (unknown line)
> 
> png\_deflate\_claim at /home/server/go/src/blockbook/ml/ca/server/recursiveDynamic/dist/share/julia/artifacts/79994d5aa1ffc0e9e42f2348a7f9c584dbe20204/lib/libpng16.so (unknown line)
> \`\`\`
> 
> this error is related to calling build so library from node 
> 
> followed project structure PackageCompiler.jl for libs (al works just fine if one is calling it from a c program but not as shared (so) library 
> 
> see also make file (below) 
> 
> test\_draw.jl
> 
> \`\`\`julia
> using CairoMakie
> CairoMakie.activate!()
> 
> export generate\_test\_draw
> 
> function generate\_test\_draw(width::Integer, height::Integer, output\_path::String)
> fig = Figure(size = (width, height))
> ax = Axis(fig\[1, 1\]; title = "Simple Square")
> 
> # Define square corners manually
> points = \[Point2f(0.25, 0.25), Point2f(0.75, 0.25),
> Point2f(0.75, 0.75), Point2f(0.25, 0.75)\]
> 
> poly!(ax, points, color = :blue)
> 
> save(output\_path, fig)
> end 
> \`\`\`
> 
> rdlib.js
> 
> \`\`\`javascript
> export generate
> 
> using Base: Cint, Cstring, unsafe\_string
> 
> include("test\_draw.jl")
> 
> \# C-callable function via @ccallable
> Base.@ccallable function generate(ctype::Cstring, width::Cint, height::Cint, o::Cstring)::Cint
> ctype\_str = unsafe\_string(ctype)
> output\_str = unsafe\_string(o)
> 
> if ctype\_str == "td"
> generate\_test\_draw(width, height, output\_str)
> else
> @warn "Unknown ctype: $ctype\_str"
> return 1 # Non-zero to indicate error
> end
> 
> return 0
> end
> \`\`\`
> 
> c program 
> 
> generate.c
> 
> \`\`\`c
> \#include \<stdio.h\>
> \#include "julia\_init.h"
> \#include "rdlib.h"
> 
> int generate\_c(const char \*ctype, int width, int height, const char \*output\_path)
> {
> init\_julia(0, NULL); // Initialize Julia runtime; adjust if you need argc/argv
> 
> int status = 0;
> status = generate(ctype, width, height, output\_path);
> if (status != 0) {
> fprintf(stderr, "generate\_%s failed with status %d\\n", ctype, status);
> }
> 
> shutdown\_julia(0); // Shutdown Julia runtime
> 
> return status;
> }
> \`\`\`
> \-------
> 
> test.js 
> 
> \`\`\`js
> const koffi = require('koffi');
> 
> // Load the shared library
> const lib = koffi.load('./dist/lib/librdlib\_c.so');
> 
> // Define the C-callable function signature
> const generate\_c = lib.func('int generate\_c(const char\*, int, int, const char\*)');
> 
> // Call the function
> const result = generate\_c('td', 250, 250, 'test\_draw.svg');
> 
> console.log('generate() returned:', result);
> \`\`\`
> 
> \-- Make file 
> 
> \`\`\`make
> .DEFAULT\_GOAL := all
> 
> JULIA ?= julia
> DLEXT := $(shell $(JULIA) --startup-file=no -e 'using Libdl; print(Libdl.dlext)')
> 
> TARGET="dist"
> 
> RDLIB\_INCLUDES = $(TARGET)/include/julia\_init.h $(TARGET)/include/rdlib.h
> RDLIB\_PATH := $(TARGET)/lib/librdlib.$(DLEXT)
> 
> build-library: build/build.jl src/rdlib.jl
> $(JULIA) --startup-file=no --project=. -e 'using Pkg; Pkg.instantiate()'
> $(JULIA) --startup-file=no --project=build -e 'using Pkg; Pkg.instantiate(); include("build/build.jl")'
> 
> INCLUDE\_DIR = $(TARGET)/include
> 
> build-c-library:
> gcc -shared -fPIC generate.c -o $(TARGET)/lib/librdlib\_c.so \\
> -I$(INCLUDE\_DIR) -L$(TARGET)/lib -ljulia -lrdlib
> 
> build-executable:
> gcc generate.c -o generate -I$(INCLUDE\_DIR) -L$(TARGET)/lib -ljulia -lrdlib
> 
> all: build-library build-c-library build-executable
> 
> clean:
> $(RM) \*~ \*.o \*.$(DLEXT)
> $(RM) -Rf $(TARGET)
> 
> .PHONY: build-library build-executable clean all
> \`\`\`\`

also if svg has any reference to png same problem will occur could be that unix png libraries are interfering with julia png framework

for instance in unrelated python problem I had to put in

LD\_PRELOAD=/usr/lib/x86\_64-linux-gnu/libjemalloc.so.2 before executing python interpreter

maybe something like this is required for julia png native execution

---

<div class="post-metadata">

### Author: ![zkbitcoin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zkbitcoin/32/217764_2.png) [@zkbitcoin](https://discourse.julialang.org/u/zkbitcoin)
#### Post date: [July 20, 2025, 4:01pm UTC](https://discourse.julialang.org/t/segment-fault-when-calling-shared-library-from-node-plotting-png-using-cairomakie-library/130794/4 "2025-07-20T16:01:08Z")

</div>

@hz-xiaxz this worked for me (so koffi (so bridge with node loads a different libz below forces system lib which I assume julia requires )

LD\_PRELOAD=/usr/lib/x86\_64-linux-gnu/libz.so.1 node test.js
