libheif icon indicating copy to clipboard operation
libheif copied to clipboard

Attempted to read or write protected memory. This is often an indication that other memory is corrupt

Open tariqgitspace opened this issue 6 months ago • 6 comments

I am getting "Attempted to read or write protected memory. This is often an indication that other memory is corrupt" when trying to get imagehandle (heif_context_get_primary_image_handle). heif_context_alloc and heif_context_read_from_file are fine.

Code: IntPtr ctx = heif_context_alloc(); Heif_error fileReadError = heif_context_read_from_file(ctx, filepath, IntPtr.Zero); IntPtr imageHandle; var getHandleErr = heif_context_get_primary_image_handle(ctx, out IntPtr imageHandle);

I compiled the binaries using "vcpkg.exe install libheif" and then using generated - heif.dll, libde265.dll and libx265.dll from folder ..\installed\x64-windows\bin

This is from dependency manager:

Image

Stack Trace: Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Repeat 2 times:

at Temp.heif_context_get_primary_image_handle(IntPtr, IntPtr ByRef)

at Temp+<ThumbnailUsingLibHeif>d__51.MoveNext() at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon ByRef) at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon ByRef) at Temp.ThumbnailUsingLibHeif(System.IO.Stream, System.String) at mainClass+<callAsyncMain>d__5.MoveNext() at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1+AsyncStateMachineBox1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].ExecutionContextCallback(System.Object) at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1+AsyncStateMachineBox1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext(System.Threading.Thread) at System.Runtime.CompilerServices.AsyncTaskMethodBuilder1+AsyncStateMachineBox1[[System.Threading.Tasks.VoidTaskResult, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext() at System.Runtime.CompilerServices.TaskAwaiter+<>c.<OutputWaitEtwEvents>b__12_0(System.Action, System.Threading.Tasks.Task) at System.Runtime.CompilerServices.AsyncMethodBuilderCore+ContinuationWrapper.Invoke() at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action, Boolean) at System.Threading.Tasks.Task.RunContinuations(System.Object) at System.Threading.Tasks.Task.FinishContinuations() at System.Threading.Tasks.Task1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].TrySetResult(System.__Canon) at System.Threading.Tasks.UnwrapPromise1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].TrySetFromTask(System.Threading.Tasks.Task, Boolean) at System.Threading.Tasks.UnwrapPromise1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].ProcessInnerTask(System.Threading.Tasks.Task) at System.Threading.Tasks.UnwrapPromise1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].ProcessCompletedOuterTask(System.Threading.Tasks.Task) at System.Threading.Tasks.UnwrapPromise`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Invoke(System.Threading.Tasks.Task) at System.Threading.Tasks.Task.RunOrQueueCompletionAction(System.Threading.Tasks.ITaskCompletionAction, Boolean) at System.Threading.Tasks.Task.RunContinuations(System.Object) at System.Threading.Tasks.Task.FinishContinuations() at System.Threading.Tasks.Task.FinishStageThree() at System.Threading.Tasks.Task.FinishStageTwo() at System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef, System.Threading.Thread) at System.Threading.Tasks.Task.ExecuteEntryUnsafe(System.Threading.Thread) at System.Threading.Tasks.Task.ExecuteFromThreadPool(System.Threading.Thread) at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading.PortableThreadPool+WorkerThread.WorkerThreadStart() at System.Threading.Thread.StartCallback()

tariqgitspace avatar Jun 18 '25 23:06 tariqgitspace

Does this only happen for a specific input file? If yes, please attach the file so that we can reproduce the issue.

farindk avatar Jun 25 '25 16:06 farindk

Thanks @farindk. I see issue with all files I tried. Attaching couple of them

sample1.zip

Also, this is the code snippet

Image

tariqgitspace avatar Jun 25 '25 17:06 tariqgitspace

It seems that the error is in your code (is that C# ?). You are passing an uninitialized imageHandle pointer as second parameter. However, this has to point to a heif_image_handle pointer variable that this function will write to.

I don't know C#, but this is the C code for it:

// get a handle to the primary image
heif_image_handle* handle;
heif_context_get_primary_image_handle(ctx, &handle);

The second parameter is a valid pointer to an (uninitialized) pointer variable.

farindk avatar Jun 25 '25 19:06 farindk

@farindk: Yes, its C#. Also, heif_image_handle is an out argument and need not be explicitly initialized. Would help to reopen issue. I dont have permission for same

tariqgitspace avatar Jun 25 '25 22:06 tariqgitspace

Maybe someone else with C# experience can help.

farindk avatar Jun 25 '25 22:06 farindk

Or maybe have a look here: https://github.com/0xC0000054/libheif-sharp/blob/7f45f0aab812fbdc5d691adcd28a3ecc7db859c0/src/HeifContext.cs#L936

farindk avatar Jun 25 '25 22:06 farindk