HarmonyOS 鸿蒙Next 子线程taskwork使用drawing.Canvas绘制image.PixelMap异常崩溃 Reason: Signal: SIGSEGV(SEGV_MAPERR)@0x0064000000000010
HarmonyOS 鸿蒙Next 子线程taskwork使用drawing.Canvas绘制image.PixelMap异常崩溃 Reason: Signal: SIGSEGV(SEGV_MAPERR)@0x0064000000000010
Device info:HUAWEI Mate X5
Build info:ALT-AL10 5.0.0.102(SP8C00E73R6P17log)
Fingerprint:4b7349195102885e6000ba885dd25d0f83e52ca249b1e494d3db8ddb9bac73de
Module name:com.midea.connect.hm.sit
Version:1.0.0.241030004
VersionCode:241030004
PreInstalled:No
Foreground:Yes
Timestamp:2024-11-21 17:25:13.845
Pid:63395
Uid:20020001
Process name:com.midea.connect.hm.sit
Process life time:480s
Reason:Signal:SIGSEGV(SEGV_MAPERR)@0x0064000000000010
Fault thread info:
Tid:1404, Name:OS_TaskWorker
#00 pc 00000000000cb3f8 /system/lib64/lib2d_graphics.z.so(OHOS::Rosen::Drawing::SkiaCanvas::DrawTextBlob(OHOS::Rosen::Drawing::TextBlob const*, float, float, OHOS::Rosen::Drawing::Paint const&)+88)(6199e58d809828f84034694349af7c82)
#01 pc 00000000000b8454 /system/lib64/lib2d_graphics.z.so(OHOS::Rosen::Drawing::CoreCanvas::DrawTextBlob(OHOS::Rosen::Drawing::TextBlob const*, float, float)+372)(6199e58d809828f84034694349af7c82)
#02 pc 000000000002dfe8 /system/lib64/platformsdk/libdrawing_napi_impl.z.so(OHOS::Rosen::Drawing::JsCanvas::OnDrawText(napi_env__, napi_callback_info__)+236)(1259a2fc7f182323abd1ce80edd408e3)
#03 pc 000000000002dec4 /system/lib64/platformsdk/libdrawing_napi_impl.z.so(OHOS::Rosen::Drawing::JsCanvas::DrawText(napi_env__, napi_callback_info__)+120)(1259a2fc7f182323abd1ce80edd408e3)
#04 pc 000000000003bff8 /system/lib64/platformsdk/libace_napi.z.so(panda::JSValueRef ArkNativeFunctionCallBack<true>(panda::JsiRuntimeCallInfo*)+216)(e14950c654205757a26744aaff615d4e)
#05 pc 0000000000332130 /system/lib64/module/arkcompiler/stub.an(RTStub_PushCallArgsAndDispatchNative+40)
#06 at generaTextImage (features/commonUI/avatar/src/main/ets/AvatarUtils/AvatarCommonUtil.ets:105:11)
#07 at customGetImg (features/commonUI/avatar/src/main/ets/AvatarComponent.ets:165:14)
#08 at getImageArrayBuffer (oh_modules/.ohpm/@ohos+imageknife@3.0.3/oh_modules/@ohos/imageknife/src/main/ets/ImageKnifeLoader.ets:221:26)
#09 at requestJob (oh_modules/.ohpm/@ohos+imageknife@3.0.3/oh_modules/@ohos/imageknife/src/main/ets/ImageKnifeDispatcher.ets:405:20)
#10 pc 00000000003278a8 /system/lib64/platformsdk/libark_jsruntime.so(panda::ecmascript::InterpreterAssembly::Execute(panda::ecmascript::EcmaRuntimeCallInfo*)+216)(63c6ed6518fd7e93e66407f4d66095fc)
#11 pc 00000000004ff800 /system/lib64/platformsdk/libark_jsruntime.so(panda::FunctionRef::CallForNapi(panda::ecmascript::EcmaVM const*, panda::JSValueRef*, panda::JSValueRef* const*, int)+336)(63c6ed6518fd7e93e66407f4d66095fc)
#12 pc 00000000000582f4 /system/lib64/platformsdk/libace_napi.z.so(napi_call_function+308)(e14950c654205757a26744aaff615d4e)
#13 pc 0000000000036230 /system/lib64/module/libtaskpool.z.so(Commonlibrary::Concurrent::TaskPoolModule::Worker::PerformTask(uv_async_s const*) (.cfi)+1604)(05e877f6a2645010a68eadbcffa202c8)
#14 pc 0000000000016868 /system/lib64/platformsdk/libuv.so(uv__async_io+340)(6149b8d1328bd0502c0c830d4fa6768c)
#15 pc 0000000000027fd8 /system/lib64/platformsdk/libuv.so(uv__io_poll+1016)(6149b8d1328bd0502c0c830d4fa6768c)
#16 pc 0000000000016e40 /system/lib64/platformsdk/libuv.so(uv_run+396)(6149b8d1328bd0502c0c830d4fa6768c)
#17 pc 000000000003589c /system/lib64/module/libtaskpool.z.so(Commonlibrary::Concurrent::TaskPoolModule::Worker::ExecuteInThread(void const*) (.cfi)+948)(05e877f6a2645010a68eadbcffa202c8)
#18 pc 00000000000311a0 /system/lib64/module/libtaskpool.z.so(Commonlibrary::Concurrent::TaskPoolModule::TaskRunner::TaskInnerRunner::Run()+68)(05e877f6a2645010a68eadbcffa202c8)
#19 pc 00000000001ba2d4 /system/lib/ld-musl-aarch64.so.1(start+236)(2869c16473050fa2addbe1ee1a3d23c3)
========SubmitterStacktrace========
#00 pc 0000000000012a78 /system/lib64/platformsdk/libuv.so(uv_queue_work+88)(6149b8d1328bd0502c0c830d4fa6768c)
#01 pc 0000000000063814 /system/lib64/platformsdk/libace_napi.z.so(NativeAsyncWork::Queue()+260)(e14950c654205757a26744aaff615d4e)
#02 pc 000000000006b5d0 /system/lib64/platformsdk/libace_napi.z.so(napi_queue_async_work+32)(e14950c654205757a26744aaff615d4e)
#03 pc 000000000009bec8 /system/lib64/platformsdk/libimage_napi.z.so(OHOS::Media::ImageSourceNapi::Release(napi_env__, napi_callback_info__) (.cfi)+908)(97d30952131e8317bfa35587cac1a40a)
#04 pc 000000000003bffc /system/lib64/platformsdk/libace_napi.z.so(panda::JSValueRef ArkNativeFunctionCallBack<true>(panda::JsiRuntimeCallInfo*)+220)(e14950c654205757a26744aaff615d4e)
#05 pc 0000000000332134 /system/lib64/module/arkcompiler/stub.an(RTStub_PushCallArgsAndDispatchNative+44)
#06 pc 0000000000009f34 /system/lib64/module/arkcompiler/stub.an(BCStub_HandleCallthis0Imm8V8+308)
#07 pc ffffffa545fc0002 /system/lib64/module/arkcompiler/stub.an
Registers:
x0:0000005c025253b8 x1:0000005bce3d2b98 x2:0000005c0141a608 x3:0000005c157d6170
x4:0000000000000000 x5:0000000000000000 x6:000000000000000d x7:4043000000000000
x8:c264000000000000 x9:0000000000000000 x10:0000000000000000 x11:0000000000000001
x12:60000000000003fb x13:0000000000020000 x14:0000000000000000 x15:6be437e7eeff8a66
x16:0000005aa0c49ec8 x17:0000005aa0b3a4f0 x18:ffff000000000006 x19:0000005bd5592800
x20:0000005c0141a608 x21:0000005c025253b8 x22:0000005c0141a608 x23:0000000000000000
x24:0000000000000000 x25:0000005a1def5478 x26:0000002f7d845cb8 x27:0000005aa4ff8848
x28:0000005c157d6290 x29:0000005c157d5f30
lr:0000005aa0b38458 sp:0000005c157d5f10 pc:0000005aa0b4b3f8
Other thread info:
Tid:519, Name:OS_IPC_4_519
#00 pc 00000000001729fc /system/lib/ld-musl-aarch64.so.1(ioctl+176)(2869c16473050fa2addbe1ee1a3d23c3)
#01 pc 0000000000006f68 /system/lib64/chipset-pub-sdk/libipc_common.z.so(OHOS::BinderConnector::WriteBinder(unsigned long, void*)+104)(82b55bbcb95e0ab0a9221084940c97d6)
#02 pc 000000000005d580 /system/lib64/platformsdk/libipc_core.z.so(OHOS::BinderInvoker::TransactWithDriver(bool)+288)(fcd52a60c6d362e4da62013affba05d8)
#03 pc 000000000005d8bc /system/lib64/platformsdk/libipc_core.z.so(OHOS::BinderInvoker::StartWorkLoop()+60)(fcd52a60c6d362e4da62013affba05d8)
#04 pc 000000000005f648 /system/lib64/platformsdk/libipc_core.z.so(OHOS::BinderInvoker::JoinThread(bool)+80)(fcd52a60c6d362e4da62013affba05d8)
#05 pc 0000000000056444 /system/lib64/platformsdk/libipc_core.z.so(OHOS::IPCWorkThread::ThreadHandler(void*)+596)(fcd52a60c6d362e4da62013affba05d8)
#06 pc 00000000001ba2d4 /system/lib/ld-musl-aarch64.so.1(start+236)(2869c16473050fa2addbe1ee1a3d23c3)
Tid:1174, Name:OS_FFRT_3_54
#00 pc 00000000001b5b94 /system/lib/ld-musl-aarch64.so.1(__timedwait_cp+192)(2869c16473050fa2addbe1ee1a3d23c3)
#01 pc 00000000001b7b98 /system/lib/ld-musl-aarch64.so.1(__pthread_cond_timedwait+188)(2869c16473050fa2addbe1ee1a3d23c3)
#02 pc 00000000000c11c0 /system/lib64/libc++.so(std::__h::condition_variable::__do_timed_wait(std::__h::unique_lock<std::__h::mutex>&, std::__h::chrono::time_point<std::__h::chrono::system_clock, std::__h::chrono::duration<long long, std::__h::ratio<1l, 1000000000l>>>)+108)(dfedb06268d7f896c86362ced8e6c5c77e36a0da)
#03 pc 0000000000067fac /system/lib64/ndk/libffrt.so(ffrt::SCPUWorkerManager::WorkerIdleAction(ffrt::WorkerThread const*)+272)(cda5b088193b0140f6b9f9456296d828)
#04 pc 000000000005f7c4 /system/lib64/ndk/libffrt.so(ffrt::CPUWorker::WorkerLooperDefault(ffrt::WorkerThread*)+156)(cda5b088193b0140f6b9f9456296d828)
#05 pc 000000000005f294 /system/lib64/ndk/libffrt.so(ffrt::CPUWorker::Dispatch(ffrt::CPUWorker*)+152)(cda5b088193b0140f6b9f9456296d828)
#06 pc 000000000005f1e4 /system/lib64/ndk/libffrt.so(ffrt::CPUWorker::WrapDispatch(void*)+28)(cda5b088193b0140f6b9f9456296d828)
#07 pc 00000000001ba2d4 /system/lib/ld-musl-aarch64.so.1(start+236)(2869c16473050fa2addbe1ee1a3d23c3)
Tid:1200, Name:OS_FFRT_2_30
#00 pc 00000000001b5b94 /system/lib/ld-musl-aarch64.so.1(__timedwait_cp+192)(2869c16473050fa2addbe1ee1a3d23c3)
#01 pc 00000000001b7b98 /system/lib/ld-musl-aarch64.so.1(__pthread_cond_timedwait+188)(2869c16473050fa2addbe1ee1a3d23c3)
#02 pc 00000000000c11c0 /system/lib64/libc++.so(std::__h::condition_variable::__do_timed_wait(std::__h::unique_lock<std::__h::mutex>&, std::__h::chrono::time_point<std::__h::chrono::system_clock, std::__h::chrono::duration<long long, std::__h::ratio<1l, 1000000000l>>>)+108)(dfedb06268d7f896c86362ced8e6c5c77e36a0da)
#03 pc 0000000000067fac /system/lib64/ndk/libffrt.so(ffrt::SCPUWorkerManager::WorkerIdleAction(ffrt::WorkerThread const*)+272)(cda5b088193b0140f6b9f9456296d828)
#04 pc 000000000005f7c4 /system/lib64/ndk/libffrt.so(ffrt::CPUWorker::WorkerLooperDefault(ffrt::WorkerThread*)+156)(cda5b088193b0140f6b9f9456296d828)
#05 pc 000000000005f294 /system/lib64/ndk/libffrt.so(ffrt::CPUWorker::Dispatch(ffrt::CPUWorker*)+152)(cda5b088193b0140f6b9f9456296d828)
#06 pc 000000000005f1e4 /system/lib64/ndk/libffrt.so(ffrt::CPUWorker::WrapDispatch(void*)+28)(cda5b088193b0140f6b9f9456296d828)
#07 pc 00000000001ba2d4 /system/lib/ld-musl-aarch64.so.1(start+236)(2869c16473050fa2addbe1ee1a3d23c3)
static generaTextImage(str: string): Promise<ArrayBuffer | undefined> {
MLog.I(TAG, "draw pixelMap begin:", str)
const width = 200
const height = 200
const drawBuffer: ArrayBuffer = new ArrayBuffer(height * width * 4)
let opts: image.InitializationOptions =
{ editable: true, pixelFormat: image.PixelMapFormat.RGBA_8888, size: { height: width, width: width } }
let pixelMap: image.PixelMap = image.createPixelMapSync(drawBuffer, opts)
let canvas: drawing.Canvas = new drawing.Canvas(pixelMap)
<span class="hljs-comment"><span class="hljs-comment">//画蓝色背景</span></span>
<span class="hljs-keyword"><span class="hljs-keyword">let</span></span> color: common2D.Color = {
red: <span class="hljs-number"><span class="hljs-number">0x24</span></span>,
green: <span class="hljs-number"><span class="hljs-number">0x96</span></span>,
blue: <span class="hljs-number"><span class="hljs-number">0xff</span></span>,
alpha: <span class="hljs-number"><span class="hljs-number">0xff</span></span>,
}
canvas.drawColor(color)
<span class="hljs-keyword"><span class="hljs-keyword">try</span></span> {
<span class="hljs-keyword"><span class="hljs-keyword">if</span></span> (str.length > <span class="hljs-number"><span class="hljs-number">0</span></span>) {
<span class="hljs-comment"><span class="hljs-comment">//画白色文字</span></span>
<span class="hljs-keyword"><span class="hljs-keyword">const</span></span> brush = <span class="hljs-keyword"><span class="hljs-keyword">new</span></span> drawing.Brush();
brush.setColor({
alpha: <span class="hljs-number"><span class="hljs-number">0xff</span></span>,
red: <span class="hljs-number"><span class="hljs-number">0xff</span></span>,
green: <span class="hljs-number"><span class="hljs-number">0xff</span></span>,
blue: <span class="hljs-number"><span class="hljs-number">0xff</span></span>
})
<span class="hljs-keyword"><span class="hljs-keyword">let</span></span> font: drawing.Font = <span class="hljs-keyword"><span class="hljs-keyword">new</span></span> drawing.Font()
font.enableEmbolden(<span class="hljs-literal"><span class="hljs-literal">true</span></span>)
font.setSize(<span class="hljs-number"><span class="hljs-number">64</span></span>)
<span class="hljs-keyword"><span class="hljs-keyword">let</span></span> text: drawing.TextBlob = drawing.TextBlob.makeFromString(str, font, drawing.TextEncoding.TEXT_ENCODING_UTF8)
<span class="hljs-keyword"><span class="hljs-keyword">if</span></span> (text && font && brush) {
<span class="hljs-keyword"><span class="hljs-keyword">let</span></span> bounds: common2D.Rect = text.bounds()
canvas.attachBrush(brush)
canvas.drawTextBlob(text, width / <span class="hljs-number"><span class="hljs-number">2</span></span> - (bounds.right - bounds.left) / <span class="hljs-number"><span class="hljs-number">2</span></span>,
height / <span class="hljs-number"><span class="hljs-number">2</span></span> + (bounds.bottom - bounds.top) / <span class="hljs-number"><span class="hljs-number">2</span></span>)
canvas.detachBrush()
}
}
} <span class="hljs-keyword"><span class="hljs-keyword">catch</span></span> (e) {
<span class="hljs-keyword"><span class="hljs-keyword">const</span></span> error = e as BusinessError
MLog.E(TAG, `draw ${str} to pixelMap has error:`, error.message + <span class="hljs-string"><span class="hljs-string">','</span></span> + error.code + <span class="hljs-string"><span class="hljs-string">''</span></span> + error)
}
MLog.I(TAG, <span class="hljs-string"><span class="hljs-string">"draw pixelMap finsihed:"</span></span>, str)
<span class="hljs-keyword"><span class="hljs-keyword">let</span></span> resultBuffer = AvatarCommonUtil.packPixelMapToBuffer(pixelMap, str)
<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> resultBuffer
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
taskpool子线程绘制图片代码如上:
在HarmonyOS鸿蒙系统中,当您在子线程中使用drawing.Canvas
绘制image.PixelMap
时遇到SIGSEGV(SEGV_MAPERR)
异常崩溃,这通常是由于线程安全问题或资源访问不当引起的。
HarmonyOS的图形绘制操作通常需要在UI线程(主线程)中进行,因为UI组件和绘图资源(如Canvas、PixelMap)往往不是线程安全的。如果在子线程中直接进行绘图操作,可能会导致资源访问冲突或非法访问,从而触发SIGSEGV异常。
解决此问题的一种方法是确保所有绘图操作都在UI线程中执行。您可以使用Handler、AsyncTask或其他线程间通信机制将绘图任务从子线程发送到UI线程。
另外,检查PixelMap
对象在使用前是否已正确初始化且未被释放或销毁。任何对未初始化或已释放资源的访问都可能导致崩溃。
确保您的代码在访问和修改UI元素时遵循了线程安全的原则。如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html。这将有助于进一步诊断问题并提供更具体的解决方案。