segmentation fault while taking snapshot of image using Skia

Skip to first unread message

Ninad Dafale

May 19, 2023, 7:48:37 AMMay 19
to skia-discuss
I have built a static SKIA library for capturing a snapshot of the provided image.

The build process followed was as follows -:
   git clone
    bin/gn gen out/Static --args='is_official_build=false cc="clang" cxx="clang++"'
    ninja -C out/Static

code I am using (snapshot.cpp) -:
#include <filesystem>
#include <iostream>
#include "include/codec/SkCodec.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkData.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkStream.h"
#include "include/core/SkString.h"
#include "include/core/SkSurface.h"
// #include "include/core/SkImageEncoder.h"
#include "include/core/SkEncodedImageFormat.h"
#include "include/core/SkImageGenerator.h"
#include "include/core/SkRect.h"
#include "include/encode/SkWebpEncoder.h"
#include "include/core/SkGraphics.h"
#include "include/core/SkImage.h"
#include "include/core/SkPaint.h"
#include "include/core/SkSurfaceProps.h"
#include "tools/Resources.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkData.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "gm/gm.h"
#include "include/codec/SkEncodedImageFormat.h"

using namespace std;
int main() {
     SkString path = SkStringPrintf("path_to_image");
     sk_sp<SkImage> image =
     int width = image->width();
     int height = image->height();

     // Create a surface with the same dimensions as the input image
     SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
     sk_sp<SkSurface> surface = SkSurface::MakeRaster(info);

     // Get the canvas from the surface
     SkCanvas* canvas = surface->getCanvas();

     // Draw the input image onto the canvas
     canvas->drawImage(image, 0, 0);

     // Save the snapshot to a file in WebP format
     const char* snapshot_path = "path_to_store_webp";
     sk_sp<SkImage> snapshot_image = surface->makeImageSnapshot();

     SkPixmap pixmap2;
     if (!image->peekPixels(&pixmap2)) {
        // return 1;

     // Encode the SkPixmap to a WebP file
     SkFILEWStream file_stream(snapshot_path);
     SkWebpEncoder::Encode(&file_stream, pixmap2, SkWebpEncoder::Options());

     return 0;

compiled the code using the command -:

 g++ -g -std=c++17 snapshot.cpp -o snapshot -I/home/maint/code/skia -L/home/maint/code/skia/out/Static -lskia -pthread -lfreetype -lfontconfig -lGL

it is giving me a seg fault which I have backtrace using gdb as shown in the screenshot -:

pls, can anyone tell us where it is going wrong.

thanks in advance...!!

John Stiles

May 19, 2023, 7:53:18 AMMay 19
to skia-discuss
This won't solve the problem, but try setting `is_debug = true` in your GN args and set -O0 when building your program. Having assertions enabled and optimizations off will often make the problem a lot easier to find.

K. Moon

May 19, 2023, 12:32:58 PMMay 19
Can you post the gdb output as text, rather than a screenshot?

You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

Ninad Dafale

May 22, 2023, 8:48:18 AMMay 22
thanks for the reply but no luck by changing GN args setting `is_debug = true`

You received this message because you are subscribed to a topic in the Google Groups "skia-discuss" group.
To unsubscribe from this topic, visit
To unsubscribe from this group and all its topics, send an email to

Ninad Dafale

May 22, 2023, 8:48:27 AMMay 22
thanks for reply @k.Moon

gdb output 

(gdb) r
Starting program: /home/maint/code/skia/snapshot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/".
*********** main start ****************

Program received signal SIGSEGV, Segmentation fault.
__memcpy_ssse3 () at ../sysdeps/x86_64/multiarch/memcpy-ssse3.S:1799
1799    ../sysdeps/x86_64/multiarch/memcpy-ssse3.S: No such file or directory.
(gdb) bt
#0  __memcpy_ssse3 () at ../sysdeps/x86_64/multiarch/memcpy-ssse3.S:1799
#1  0x00005555555ccb88 in SkMemoryStream::read (this=0x5555568deab0, buffer=0x7fffffffe270, size=32) at ../../src/core/SkStream.cpp:349
#2  0x00005555555ccc12 in SkMemoryStream::peek (this=0x5555568deab0, buffer=0x7fffffffe270, size=32) at ../../src/core/SkStream.cpp:360
#3  0x00005555561d6565 in SkCodec::MakeFromStream (stream=std::unique_ptr<SkStream> = {...}, outResult=0x7fffffffe2a0, chunkReader=0x0,
    selectionPolicy=SkCodec::SelectionPolicy::kPreferStillImage) at ../../src/codec/SkCodec.cpp:128
#4  0x00005555561d6946 in SkCodec::MakeFromData (data=..., reader=0x0) at ../../src/codec/SkCodec.cpp:193
#5  0x0000555555f82245 in SkCodecImageGenerator::MakeFromEncodedCodec (data=..., at=std::optional<SkAlphaType> = {...})
    at ../../src/codec/SkCodecImageGenerator.cpp:23
#6  0x000055555581e853 in SkImageGenerators::MakeFromEncodedImpl (data=..., at=std::optional<SkAlphaType> = {...})
    at ../../src/ports/SkImageGenerator_skia.cpp:16
#7  0x0000555555649c0b in SkImageGenerators::MakeFromEncoded (data=..., at=std::optional<SkAlphaType> = {...}) at ../../src/core/SkImageGenerator.cpp:101
#8  0x00005555555d65ad in SkImages::DeferredFromEncodedData (encoded=..., alphaType=std::optional<SkAlphaType> = {...})
    at ../../src/image/SkImage_LazyFactories.cpp:32
#9  0x00005555555c9a66 in main () at snapshot.cpp:36
(gdb) Quit

K. Moon

May 22, 2023, 1:40:09 PMMay 22
Is the stack trace the same with is_debug=true? I don't have any bright ideas, but it looks like SkMemoryStream::read() is trying to copy memory that either doesn't exist, or isn't writable.

Reply all
Reply to author
0 new messages