0

I got this problem when passing a static string to this delegate, I've tried copying the value of the static string to the normal string and it works fine.

((JsonHttpClient)connection).RequestFilter = httpReq =>
{
    string authtoken = MemoryCache.authToken;
    httpReq.Headers.Add(UdareConstants.AuthTokenKey, authtoken);
};

Also, hard coding the value works fine.

((JsonHttpClient)connection).RequestFilter = httpReq =>
{
    string authtoken = "62bebc52-fde3-4f47-beab-6a3e4e3440f0";
    httpReq.Headers.Add(UdareConstants.AuthTokenKey, authtoken);
};

MemoryCache it's a static class and the authToken property it's a static string.

This is driving me insane.

The call stack from the exception

Console output after app crashes

Beetee
  • 475
  • 1
  • 7
  • 18
Loucry
  • 23
  • 4

1 Answers1

0

I haven't coded in Java for ages and I have never used Xamarin but you might have run into some stuff related to Java memory model.

Java documentation which says:

  • Each action in a thread happens-before every action in that thread that comes later in the program's order.

  • An unlock (synchronized block or method exit) of a monitor happens-before every subsequent lock (synchronized block or method entry) of that same monitor. And because the happens-before relation is transitive, all actions of a thread prior to unlocking happen-before all actions subsequent to any thread locking that monitor.

  • A write to a volatile field happens-before every subsequent read of that same field. Writes and reads of volatile fields have similar memory consistency effects as entering and exiting monitors, but do not entail mutual exclusion locking.

  • A call to start on a thread happens-before any action in the started thread.

  • All actions in a thread happen-before any other thread successfully returns from a join on that thread.

You can read a bit more about it in Nathan Hughes' answer on SO.

Another option is that you've run into issue with the static field initialization order.

BTW: Static variables are usually a bad idea.

Community
  • 1
  • 1
Tomasz Maczyński
  • 973
  • 10
  • 24