0

[Xamarin - Android]

I have an object (that extends from Android.App.Service) and saves its context into a public static (volatile) variable so that calls across the application can use it as the context for method calls from context [or that requires a context].

However, after about several hours later, it gets collected and start throwing null object reference exception everywhere that calls this variable.

How can I avoid it and is there a better way to do it?


Edit: The purpose for the variable is to make static calls that requires context for some functions like "Toast".

Zananok
  • 121
  • 1
  • 3
  • 12
  • This has nothing to do with the GC. If a `static` variable is unexpectedly cleared, it's because the Android framework killed the entire VM while your app was in the background. This is a normal occurrence that you must design around. Generally, you should not save a `Context` at all. Explain in more detail what you think you need that for, and then we can tell you a better way. – Kevin Krumwiede Mar 09 '17 at 17:42
  • Although I can't say if it is GC or the VM, I can at least say that your comment is partially incorrect. My application's notification service is still operational and I can load activities from another public static. The application is very much alive, just lost 1 object. How do I know about this null exception error? I create a single notification popup which prints the exception. – Zananok Mar 09 '17 at 17:47
  • Variables are not randomly set to null. If you unexpectedly find your variable null, the app was killed and restarted and the code that initializes the variable didn't run. – Kevin Krumwiede Mar 09 '17 at 18:01
  • I know for a fact that that is incorrect. Yes variables can "suddenly" become null, specially public static variables as there are no clear reference to them, thus GC can kill them if no activity is done with that reference for a long time, whereas if it was a public non-static variable it would not touch it at all (unless disposed on purpose or the object holding that variable was disposed). I do understand and agree that saving the context is not the best practice, i just don't know what is the best practice for mobile development with Xamarin on this given context (no pun intended). – Zananok Mar 09 '17 at 18:05
  • You are completely wrong about how the GC works. – Kevin Krumwiede Mar 09 '17 at 18:09
  • Don't state with no sources please. Here are some of mine. Can't find the one I really trusted now, but these should suffice: https://stackoverflow.com/questions/13126833/does-the-garbage-collector-work-on-static-variables-or-methods-in-java https://books.google.no/books?id=bG_Aqb6iOUYC&pg=PA341&lpg=PA341&dq=GC+and+public+static&source=bl&ots=PDV9tGhHz3&sig=Wl4QspRqyLHoDSPzIys5qI-Xkhg&hl=en&sa=X&ved=0ahUKEwjnzq7lgsrSAhUJCpoKHWCXB7sQ6AEITzAI#v=onepage&q=GC%20and%20public%20static&f=false – Zananok Mar 09 '17 at 18:13
  • That first link is about `WeakReference`, and the accepted answer says exactly the opposite of what you're claiming. The GC does not set variables to null; it collects objects after something else sets the variable to null. – Kevin Krumwiede Mar 09 '17 at 18:17
  • What are you talking about? Xamarin is C#, its the same language. Glad you are trying to check the validity of my sources instead of providing one or focusing on the question. – Zananok Mar 09 '17 at 18:21
  • Your question is unanswerable because its entire premise is invalid. – Kevin Krumwiede Mar 09 '17 at 18:23
  • No, I just think you lack the knowledge to understand or acknowledge it. However, if it were true that my premise was invalid, none of your comments, aside from your first one, attempted to solve or care for the situation. Instead just made blind statement and saying that my sources are from another language, which makes no sense, when we are clearly talking about C# and microsoft technology. Regardless of what you think about how GC is handled, you should either provide a source for your statements, or assist me with providing a better design as it was clear that the problem is saving context – Zananok Mar 09 '17 at 18:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137692/discussion-between-kevin-krumwiede-and-zananok). – Kevin Krumwiede Mar 09 '17 at 18:43

0 Answers0