singleton

Static context saved in Application class and used in a singleton toast builder, does this create a memory leak?

一世执手 提交于 2019-12-25 09:26:19
问题 I've got a singleton Toast-showing class, which keeps track of the current toast and cancels it when it is started again: public enum SingleToast { INSTANCE; private Toast currentToast; private String currentMessage; public void show(String message, int duration) { if (message.equals(currentMessage)) { currentToast.cancel(); } currentToast = Toast.makeText(App.getContext(), message, duration); currentToast.show(); currentMessage = message; } } (Extended explanation of this example can be

Static context saved in Application class and used in a singleton toast builder, does this create a memory leak?

回眸只為那壹抹淺笑 提交于 2019-12-25 09:21:12
问题 I've got a singleton Toast-showing class, which keeps track of the current toast and cancels it when it is started again: public enum SingleToast { INSTANCE; private Toast currentToast; private String currentMessage; public void show(String message, int duration) { if (message.equals(currentMessage)) { currentToast.cancel(); } currentToast = Toast.makeText(App.getContext(), message, duration); currentToast.show(); currentMessage = message; } } (Extended explanation of this example can be

What does it mean that singleton DCL broken?

本小妞迷上赌 提交于 2019-12-25 08:59:37
问题 After reading dozens of articles about DCL. I feel that I should not use this concept without volatile.If I will not lead this technique my code will not thread save and very very bad according one hundreed different reasons. Recently I reread basics Happens Before and I have a bit another view Lets research singleton code listing: public class Singleton{ private static Something instance = null; public static Singleton getInstance() { if (instance == null) { // point 1 synchronized

Is a singleton controller possible in JavaFX(ML)?

本小妞迷上赌 提交于 2019-12-25 07:54:03
问题 I tried to implement a controller with the singleton pattern like described a couple of times on the web. However I cant get the application to run because of the following exception. java.lang.IllegalAccessException: Class sun.reflect.misc.ReflectUtil can not access a member of class testapp.Controller with modifiers "private" I guess thats because the constructor is declared private. I dont see what I am doing wrong at this point. In case I wasn't clear what my problem is I will describe

Flush StreamWriter at the end of its lifetime

人盡茶涼 提交于 2019-12-25 06:50:57
问题 I have a singleton-like class that can do some logging output: class Foo { private static Foo instance; private System.IO.StreamWriter os; private Foo() { this.os = System.IO.File.CreateText( "D:/tmp/test" ); } public static Foo Instance { get { if ( instance == null ) instance = new Foo(); return instance; } } public void Log( string s ) { os.Write( s ); } } When I use that in a sample program like class Program { private static void Main( string[] args ) { Foo.Instance.Log( "asdf\n" ); } }

Core Data context and singleton data controller

有些话、适合烂在心里 提交于 2019-12-25 05:59:09
问题 I have a singleton data controller to hold an array of objects. See for example bananas question for my solution: singelton dataController banansArray Now I want to save the array of bananas to persistant state. This core data tutorial: core data - store images have given me a good general understanding of Core Data and I was able to include it in my application before changing my data Controller to singleton. Now what is best? Do I need to move the generated Core Data stack within the

Singleton for Embeded RavenDb

一曲冷凌霜 提交于 2019-12-25 05:25:27
问题 Why when I use this singleton the load retreive always null? public class DataLayer { private EmbeddableDocumentStore d; private static object lockerSingleton = new object(); private static DataLayer _current; public static DataLayer RavenDB { get { lock (lockerSingleton) { if (_current == null) _current = new DataLayer(); } return _current; } } public DataLayer() { d = new EmbeddableDocumentStore() { DataDirectory = "csv" }; d.Initialize(); } public void store<T>(T obj) { using (var session

How to use a Singleton Signalr client within an MVC application

大城市里の小女人 提交于 2019-12-25 04:19:15
问题 I have a need to use a .net client to connect to a Signalr enabled application. The client class needs to be a singleton and loaded for use globally. I want to know what is the best technique for using singletons globally within an MVC application. I have been looking into using the application start to get the singleton, where I keep it is a mystery to me. 回答1: The HUB cant be a singleton by design SignalR creates a instance for each incoming request. On the client I would use a IoC

Using a singleton in C++

末鹿安然 提交于 2019-12-25 03:26:29
问题 I have a class which is a Singleton class. In the CPP file I have: static std::unique_ptr<CStage> s; MSOCPPAPITYPE_(ICStage&) GetInstance() { try { s = std::make_unique<CStage>(); } catch (...) { } return *s; } Within another file I call GetInstance().SetMValue(true); which is a function that sets a member variable of the class to true . The default value of the member variable is false . Then I call GetInstance().GetMValue(); which returns the value of the member variable. Instead of

Is the performance of non singleton sql connections better?

*爱你&永不变心* 提交于 2019-12-25 02:58:53
问题 This is a follow up question to: Is it necessary to deconstruct singleton sql connections? As a few comments there stated it is bad design to use a singleton for sql connection instead of doing multiple usings. What intrigues me there though is one statement that the performance of the using variant is better than that of the singleton variant. Now as stated by me that it is a bad design is clear to me (I know most pros and cons for singletons there...especially the cons). What surprised me