DEV Community

HarmonyOS
HarmonyOS

Posted on

An exception occurred when calling createPixelMap, causing the application to crash.

Read the original article:An exception occurred when calling createPixelMap, causing the application to crash.

An exception occurred when calling createPixelMap, causing the application to crash.

Problem Description

An exception occurs when calling createPixelMap, causing the application to crash. The fault is due to SIGSEGV (SEGV_MAPERR). From the stack trace, it can be seen that the exception occurred while executing the OHOS::Media::CreatePixelMapInner function in libimage_napi.z.so.

Process life time:18446744073709038485s
Reason:Signal:SIGSEGV(SEGV_MAPERR)@0x006e6572272c3042 
Fault thread info:
Tid:26089, Name:OS_FFRT_3_23
#00 pc 00000000000b2d84 /system/lib64/platformsdk/libimage_napi.z.so(OHOS::Media::CreatePixelMapInner(OHOS::Media::ImageSourceNapi*, std::__h::shared_ptr<OHOS::Media::ImageSource>, unsigned int, OHOS::Media::DecodeOptions, unsigned int&)+100)(e23baa3bbe78b77fdcc59cfecd0dc7d0)
#01 pc 00000000000b37f8 /system/lib64/platformsdk/libimage_napi.z.so(OHOS::Media::CreatePixelMapExecute(napi_env__*, void*) (.1837.cfi)+320)(e23baa3bbe78b77fdcc59cfecd0dc7d0)
#02 pc 000000000006718c /system/lib64/platformsdk/libace_napi.z.so(NativeAsyncWork::AsyncWorkCallback(uv_work_s*)+508)(6c1a8d774d3ae619f3694983d88e25d3)
#03 pc 00000000000129d4 /system/lib64/platformsdk/libuv.so(uv__ffrt_work+52)(2e9ebccba34c7e0ddc6168806c4a01b7)
#04 pc 000000000006ea6c /system/lib64/ndk/libffrt.so(ffrt::CPUWorker::Run(ffrt_executor_task*, int)+488)(b21a7dcfe0f0c16de7fc6c82247993e2)
#05 pc 000000000006f2b8 /system/lib64/ndk/libffrt.so(ffrt::CPUWorker::RunTask(ffrt_executor_task*, ffrt::CPUWorker*)+132)(b21a7dcfe0f0c16de7fc6c82247993e2)
#06 pc 000000000006f648 /system/lib64/ndk/libffrt.so(ffrt::CPUWorker::WorkerLooperDefault(ffrt::WorkerThread*)+232)(b21a7dcfe0f0c16de7fc6c82247993e2)
#07 pc 000000000006f13c /system/lib64/ndk/libffrt.so(ffrt::CPUWorker::Dispatch(ffrt::CPUWorker*)+148)(b21a7dcfe0f0c16de7fc6c82247993e2)
#08 pc 000000000006f090 /system/lib64/ndk/libffrt.so(ffrt::CPUWorker::WrapDispatch(void*)+28)(b21a7dcfe0f0c16de7fc6c82247993e2)
#09 pc 00000000001bdcac /system/lib/ld-musl-aarch64.so.1(start+236)(f77c0346c0084ebbadf721ea319f5f77)Copy codeCopy code
Enter fullscreen mode Exit fullscreen mode

Background Knowledge

createPixelMap: Creates a PixelMap object using image decoding parameters.

SIGSEGV(SEGV_MAPERR): Signal string indicating an invalid memory address, which means the process attempted to access a memory address that does not exist or is not mapped to the process's address space. This situation is usually caused by pointer errors or memory leaks in the program.

Troubleshooting Process

From the logs, it can be observed that when calling the CreatePixelMapInner method, a parameter of type ImageSource is passed, which represents the image-related information. If there is an issue of accessing a non-existent memory address during the method call, it is likely that the passed ImageSource object has already been invalidated. Therefore, you should check whether the ImageSource object is being prematurely released before the createPixelMap method is called in the code.

In the following key code, it can be seen that the release() method is called immediately after executing createPixelMap(). Since createPixelMap is an asynchronous method and takes time to execute, the ImageSource object is released before the createPixelMap method completes its execution. As a result, when attempting to access the passed ImageSource object, an issue of accessing a non-existent memory address occurs.

let imageSource = image.createImageSource(this.buffer);
imageSource.createPixelMap().then((pixelMap: image.PixelMap) => {
  // handle pixelMap
}).catch((error: Error) => {
  console.error(`error = ${JSON.stringify(error)}`);
})
imageSource.release();
Enter fullscreen mode Exit fullscreen mode

Analysis Conclusion

The ImageSource object was released prematurely, causing the createPixelMap method to access a non-existent memory address when accessing the ImageSource object during execution, which led to the application crash.

Solution

Release the ImageSource object after creating and processing the PixelMap object.

let imageSource = image.createImageSource(this.buffer);
imageSource.createPixelMap().then((pixelMap: image.PixelMap) => {
  // handle pixelMap
  imageSource.release();
}).catch((error: Error) => {
  console.error(`error = ${JSON.stringify(error)}`);
  imageSource.release();
})
Enter fullscreen mode Exit fullscreen mode

Written by Mehmet Karaaslan

Top comments (0)