Hi Matthew,

Thanks for you quick response. 

Regardless of the following I like your suggestion as it seems like a good safe 
way to store global static type variables.

I'm not sure in my case this is going to solve the issue (although hopefully 
I'm wrong).  The reason being (apologies for not putting this in my original 
post) that within my activity I declare a variable that I then assign my 
singleton to in OnCreate (see snippet below _dp being the class in question).  
It should therefore still be in scope and safe from GC whilst the activity is 
running.  Please let me know if this is just my poor understanding and I'm 
wrong on this or whether this is reasonable.


        public class Home : Activity
        {
                private DataProvider _dp;
                private ProgressBar _progressUndefined;
                private ProgressBar _progressDefined;
                private TextView _tvProgress;
                private ViewSwitcher _vsHome;
                private CustomGallery _gHomeSliders;
                private HomeSliderAdapter _slideAdapter;
                private ImageView[] _pagerItems;
                private LinearLayout _llProgress;
                
                protected override void OnCreate (Bundle bundle)
                {
                        
                        base.OnCreate (bundle);

                        SetContentView(Resource.Layout.Home);
                        
                        _dp = DataProvider.GetInstance();
                . . .


                . . .
                        ThreadPool.QueueUserWorkItem(state => 
_dp.CheckCacheAge());
                                        }


Thanks for your help

Keith


Keith Raven
Buzz Interactive
25 Trenance Road, Newquay, Cornwall UK TR7 2LT
t: +44 (0)1637 839173
m: +44 (0)7971 699127
www.buzzinteractive.co.uk

This message contains confidential information and is intended only for the 
person to whom it is addressed. If you are not the named addressee you should 
not disseminate, distribute or copy this email. Please notify the sender 
immediately by email if you have received this message by mistake and delete 
this message from your system. The sender does not accept liability for any 
errors or omissions in the contents of this message, which arise as a result of 
email transmission. The recipient should check this email and any attachments 
for the presence of viruses. Buzz Interactive Solutions Ltd accepts no 
liability for any damage caused by any virus transmitted by this email.


On 25 Jun 2012, at 12:16, Matthew Leibowitz wrote:

> Hi,
> 
> What may be the problem is that Android is destroying you singleton class. 
> Android does not ensure that static classes/members will keep their values. 
> On lower end devices, the garbage collector is obviously working hard to keep 
> memory usage at a minimum.
> 
> If you wish to do something like this, you can create an instance of whatever 
> class, and assign it to a member on a derived Application.
> 
> I also had this problem with my application, so I changed the class to be an 
> instance class. I then created a class that derived from 
> Android.App.Application (remember the [Application] attribute). And in the 
> application's OnCreate, I created an instance of my no-longer-static class. 
> This did help.
> 
> You can then access you Application by using 
> (MyApplication)Context.ApplicationContext.
> 
> Hope this is useful (and if inaccurate, please don't hesitate to inform me)
> 
> Regards,
> Matthew L.
> 
> On Mon, Jun 25, 2012 at 12:49 PM, keith <ke...@buzzinteractive.co.uk> wrote:
> Hi
> I have been getting random null reference exceptions in my app when running
> on lower end devices (galaxy ace in this case), but not on a desire s or
> emulators.  They mostly seem to be in and around my data access to sqlite
> (using sqlite-net) although I haven't been able to pin point them to any one
> bit of code.  Generally it will crash with the only stacktrace being that
> there was a null reference exception and nothing to indicate where and so I
> have been using logging to try to get an idea of the source of the issues
> but it seems to shift around.  Then today the debugger actually caught one
> of these occurences but when checking out the variables in scope 'this' was
> null (I have a screen shot if you would like it as in theory this isn't
> possible).  'this' being a singleton instance of a class which was running
> some data updates on a background thread (started from my activity using
> ThreadPool.QueueUserWorkItem(state => _dp.CheckCacheAge()); ).  At this
> point there is not a lot going on in the current activity which is simply
> waiting on the updates and has some event handlers attached to update the ui
> with progress.  This particular crash happened at a point where there is not
> a great deal loaded into memory.
> 
> stacktrace:
> 
> System.NullReferenceException: Object reference not set to an instance of an
> object
> at (wrapper stelemref) object:virt_stelemref_class (intptr,object)
>  at MyDevon.DataProvider.PopulateConfiguration () [0x0001b] in
> /Projects/NVG/Around Me Android/branches/My Devon/Around
> Me/DataAccess/DataProvider.cs:225
> 
> The log from another occurence (included last reference to garbage
> collection):
> 
> 06-25 11:22:09.119: D/Mono    ( 3076): GC_MINOR: pause 37.06ms, bridge
> 0.00ms promoted 912K major 5008K los 7416K
> .....
> 06-25 11:22:09.659: D/SQLite(3076): ExecuteNonQuery
> 06-25 11:22:09.659: D/SQLite(3076): ExecuteNonQuery
> 06-25 11:22:09.659: D/SQLite(3076): ExecuteNonQuery
> 06-25 11:22:09.669: D/MD-DataProvider(3076): started insert
> 06-25 11:22:09.669: D/SQLite(3076): ExecuteNonQuery
> 06-25 11:22:09.669: I/mono(3076): Stacktrace:
> 06-25 11:22:09.679: E/mono(3076): [0x7f5420:] EXCEPTION handling:
> System.NullReferenceException: Object reference not set to an instance of an
> object
> 
> These crashes are intermitent as in they do not occur in the same place each
> time and only occur roughly 1 in every 3 runs of the full update routine.
> There doesn't seem to be any correlation with timings of the GC either, but
> it does feel like something external is killing off my static instance of
> this class.
> 
> Any advice of where to look on this one would be very helpful as I'm
> completely at a loss.
> 
> thanks
> 
> Keith
> 
> 
> --
> View this message in context: 
> http://mono-for-android.1047100.n5.nabble.com/this-is-null-null-reference-exception-tp5710505.html
> Sent from the Mono for Android mailing list archive at Nabble.com.
> _______________________________________________
> Monodroid mailing list
> Monodroid@lists.ximian.com
> 
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid
> 
> _______________________________________________
> Monodroid mailing list
> Monodroid@lists.ximian.com
> 
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to