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

3
  • how do you initialize MemoryCache.authToken? Commented Aug 19, 2016 at 23:02
  • on the start of the code i do MemoryCache.authToken = "62bebc52-fde3-4f47-beab-6a3e4e3440f0"; Commented Aug 22, 2016 at 18:54
  • You can try adding data breakpoints to investigate that bug. This might be of interest: stackoverflow.com/questions/4086039/… Commented Aug 22, 2016 at 19:50

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.