# IncorrectDereferenceException on iOS with HttpClient

**URL:** <https://discuss.kotlinlang.org/t/incorrectdereferenceexception-on-ios-with-httpclient/19865>\
**Category:** Multiplatform\
**Created:** [November 9, 2020, 3:05pm UTC](https://discuss.kotlinlang.org/t/incorrectdereferenceexception-on-ios-with-httpclient/19865 "2020-11-09T15:05:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![amritanshtripathi](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@amritanshtripathi](https://discuss.kotlinlang.org/u/amritanshtripathi)\
**Post date:** [November 9, 2020, 3:05pm UTC](https://discuss.kotlinlang.org/t/incorrectdereferenceexception-on-ios-with-httpclient/19865/1 "2020-11-09T15:05:43Z")

</div>

ktorVersion = “1.4.1”  
coroutinesVersion = “1.3.9-native-mt-2”

I am trying to make a post request using Ktor HttpClient in commonMain on a singleThread using Dispatcher.Default and I am getting this exception in iOS

```auto
Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared <object>@c1c6c8 from other thread
    at 0 shared 0x000000010ac441bd kfun:kotlin.Throwable#<init>(kotlin.String?){} + 93 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/Throwable.kt:23:37)
    at 1 shared 0x000000010ac3d0ab kfun:kotlin.Exception#<init>(kotlin.String?){} + 91 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/Exceptions.kt:23:44)
    at 2 shared 0x000000010ac3d26b kfun:kotlin.RuntimeException#<init>(kotlin.String?){} + 91 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/Exceptions.kt:34:44)
    at 3 shared 0x000000010ac7006b kfun:kotlin.native.IncorrectDereferenceException#<init>(kotlin.String){} + 91 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/native/Runtime.kt:30:36)
    at 4 shared 0x000000010ac7438d ThrowIllegalObjectSharingException + 621 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/native/concurrent/Internal.kt:98:11)
    at 5 shared 0x000000010ad5a002 _ZN12_GLOBAL__N_128throwIllegalSharingExceptionEP9ObjHeader + 34
    at 6 shared 0x000000010ad5e52e _ZNK27BackRefFromAssociatedObject3refEv + 78
    at 7 shared 0x000000010ad44bd4 _ZN12_GLOBAL__N_111toKotlinImpEP11objc_objectP13objc_selectorPP9ObjHeader + 52
    at 8 shared 0x000000010ad87c81 Kotlin_Interop_refFromObjC + 65
    at 9 shared 0x000000010aff65ea _696f2e6b746f723a6b746f722d636c69656e742d696f73_knbridge28 + 330 (/opt/buildAgent/work/a85294440dc5c6e/ktor-client/ktor-client-ios/darwin/src/io/ktor/client/engine/ios/IosResponseReader.kt:100:14)
    at 10 shared 0x000000010afff37a _696f2e6b746f723a6b746f722d636c69656e742d696f73_kncfun30 + 74
    at 11 CFNetwork 0x00007fff22f31ade _CFNetworkHTTPConnectionCacheSetLimit + 207606
    at 12 Foundation 0x00007fff25752d51 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 7
    at 13 Foundation 0x00007fff25752c49 -[NSBlockOperation main] + 98
    at 14 Foundation 0x00007fff25755bd6 __NSOPERATION_IS_INVOKING_MAIN__ + 17
    at 15 Foundation 0x00007fff25751e49 -[NSOperation start] + 731
    at 16 Foundation 0x00007fff25756530 __NSOPERATIONQUEUE_IS_STARTING_AN_OPERATION__ + 17
    at 17 Foundation 0x00007fff2575603a __NSOQSchedule_f + 182
    at 18 libdispatch.dylib 0x000000010b75a3c9 _dispatch_block_async_invoke2 + 83
    at 19 libdispatch.dylib 0x000000010b74bd48 _dispatch_client_callout + 8
    at 20 libdispatch.dylib 0x000000010b759de6 _dispatch_main_queue_callback_4CF + 1500
    at 21 CoreFoundation 0x00007fff23bd4049 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    at 22 CoreFoundation 0x00007fff23bceca9 __CFRunLoopRun + 2329
    at 23 CoreFoundation 0x00007fff23bce066 CFRunLoopRunSpecific + 438
    at 24 GraphicsServices 0x00007fff384c0bb0 GSEventRunModal + 65
    at 25 UIKitCore 0x00007fff48092d4d UIApplicationMain + 1621
    at 26 iosApp-sample 0x000000010a8b905b main + 75 (/User/iosApp-sample/iosApp-sample/AppDelegate.swift:4:7)
    at 27 libdyld.dylib 0x00007fff5227ec25 start + 1

```

This is what my implementation looks like

```auto
fun postRequest() {
        MainScope().launch {
            withContext(Dispatchers.Default) {           
                 HttpClient().post("URL"){
                   body = "some json"
                 }
            }
        }
    }

```

This request is working fine for the Android application and only throws the exception in iOS. Also if I try to run HttpClient().post on the main thread, it works fine on iOS and I am successfully able to send the request.

Github issue : [[native] IncorrectDereferenceException on iOS with HttpClient · Issue #2372 · Kotlin/kotlinx.coroutines · GitHub](https://github.com/Kotlin/kotlinx.coroutines/issues/2372)

---

<div class="post-metadata">

**Author:** ![TomWayne](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/tomwayne/32/8959_2.png) [@TomWayne](https://discuss.kotlinlang.org/u/TomWayne)\
**Post date:** [November 8, 2021, 2:28pm UTC](https://discuss.kotlinlang.org/t/incorrectdereferenceexception-on-ios-with-httpclient/19865/2 "2021-11-08T14:28:30Z")

</div>

I have exactly the same problem with `ktorVersion = "1.6.0"` and coroutines “1.5.0-native-mt”.

---

<div class="post-metadata">

**Author:** ![amritanshtripathi](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@amritanshtripathi](https://discuss.kotlinlang.org/u/amritanshtripathi)\
**Post date:** [November 8, 2021, 4:57pm UTC](https://discuss.kotlinlang.org/t/incorrectdereferenceexception-on-ios-with-httpclient/19865/3 "2021-11-08T16:57:31Z")

</div>

@TomWayne I think the issue was fixed as you can see in the GitHub link above. are you passing a mutable object when making the request? iOS does not allow passing mutable objects between threads.
