How to actively delete a Dart object? - class

I'm building a Dart app which contains a variety of class objects. The particular class object I'm dealing with contains a variety of stream event listeners on DOM elements. When I remove these objects from the DOM and untrack the class object these listeners persist.
I know that Dart runs garbage collection eventually, but I'm not even 100% sure it will come along and delete these class objects since there is a Watcher and Stream listeners that continue.
My question is, is there a way to actively delete a class object immediately? I tried setting the class object to null but that doesn't seem to work for some reason. When I check if the object exists afterward with a print statement, it still lists it as an instance of that class object.
Furthermore, for what I'm trying to accomplish, canceling streams doesn't seem to be enough. I need to destroy the class object.

Setting references to null is all you can do. Your test seems very weird. How can you print the object if you don't have a reference? If you still have a reference, how can you expect the instance to be collected.

Related

How to destroy a class in C#

i was trying to destroy a class when i want, but it just doesn't get destroyed.
I want to destroy it even if it had references in other places
and and turn those references into nulls.
Like in unity when you create a component and you destroy it and it gets destroyed.
And all references turn into nulls.
if there is a way please tell me, Thanks.
I tried IDispose but i don't know if it's wrong.
A reference to it should be null but its not.
I called the dispose function but a reference to it says its not null.

Scala: Making singleton to process a client request

Is it a good practice to make use of singleton object to process client requests?
As object instance is singleton, and if its in middle of processing a client request , and meanwhile another request arrives then same instance is invoked with new client data. Won't it make things messy?
When using singleton objects we must ensure everything inside it as well as everything it calls must be thread-safe. For example, javax.crypto.Cipher does not seem to be thread-safe, so it should probably not be called from a singleton. Consider how guice uses #Singleton to specify threadsafety intention:
#Singleton
public class InMemoryTransactionLog implements TransactionLog {
/* everything here should be threadsafe! */
}
Also consider the example of Play Framework which starting version 2.4 began moving away from singleton controllers and started encouraging class controllers.
It depends on whether the object holds any mutable data or not.
If the object is just a holder for pure functions and immutable state, it does not matter how many threads are using it at the same time because they can't affect each other via shared state.
If the object has mutable state then things can definitely go wrong if you access it from multiple threads without some kind of locking, either inside the object or externally.
So it is good practice as long as there is no mutable state. It is a good way of collecting related methods under the same namespace, or for creating a global function (by defining an apply method).

Composing IObservables and cleaning up after registrations

I have some code in a class that takes FileSystemWatcher events and flattens them into an event in my domain:
(Please note, the *AsObservable methods are extensions from elsewhere in my project, they do what they say šŸ™‚.)
watcher = new FileSystemWatcher(ConfigurationFilePath);
ChangeObservable = Observable
.Merge(
watcher.ChangedAsObservable().Select((args) =>
{
return new ConfigurationChangedArgs
{
Type = ConfigurationChangeType.Edited,
};
}),
watcher.DeletedAsObservable().Select((args) =>
{
return new ConfigurationChangedArgs
{
Type = ConfigurationChangeType.Deleted,
};
}),
watcher.RenamedAsObservable().Select((args) =>
{
return new ConfigurationChangedArgs
{
Type = ConfigurationChangeType.Renamed,
};
})
);
ChangeObservable.Subscribe((args) =>
{
Changed.Invoke(this, args);
});
Something that I'm trying to wrap my head around as I'm learning are best practices around naming, ownership and cleanup of the IObservable and IDisposable returned by code like this.
So, some specific questions:
Is it okay to leak IObservables from a class that creates them? For example, is the property I'm assigning this chain to okay to be public?
Does the property name ChangeObservable align with what most people would consider best practice when using the .net reactive extensions?
Do I need to call Dispose on any of my subscriptions to this chain, or is it safe enough to leave everything up to garbage collection when the containing class goes out of scope? Keep in mind, I'm observing events from watcher, so there's some shared lifecycle there.
Is it okay to take an observable and wire them into an event on my own class (Changed in the example above), or is the idea to stay out of the native .net event system and leak my IObservable?
Other tips and advice always appreciated! šŸ˜€
Is it okay to leak IObservables from a class that creates them? For
example, is the property I'm assigning this chain to okay to be
public?
Yes.
Does the property name ChangeObservable align with what most
people would consider best practice when using the .net reactive
extensions?
Subjective question. Maybe FileChanges? The fact that it's an observable is clear from the type.
Do I need to call Dispose on any of my subscriptions to
this chain, or is it safe enough to leave everything up to garbage
collection when the containing class goes out of scope?
The ChangeObservable.Subscribe at the end could live forever, preventing the object from being garbage collected if the event is subscribed to, though that could also be your intention. Operator subscriptions are generally fine. I can't see the code for your ChangedAsObservable like functions. If they don't include a Subscribe or an event subscription, they're probably fine as well.
Keep in mind,
I'm observing events from watcher, so there's some shared lifecycle
there.
Since FileWatcher implements IDisposable, you should probably use Observable.Using around it so you can combine the lifecycles.
Is it okay to take an observable and wire them into an event on
my own class (Changed in the example above), or is the idea to stay
out of the native .net event system and leak my IObservable?
I would prefer to stay in Rx. The problem with event subscriptions is that they generally live forever. You lose the ability to control subscription lifecycle. They're also feel so much more primitive. But again, that's a bit subjective.

Cannot create class in AHK after destruction

I'm trying to wrap my head around classes in AHK. I'm C++ dev, so would like to make use of RAII (__New, __Delete), but it looks like I miss some concepts since things look very contra-intuitive for me.
After some tries I came up with this simple example:
class Scenario
{
__New()
{
MsgBox, NEW
}
__Delete()
{
MsgBox, DELETE
}
}
scenario := new Scenario
scenario := new Scenario
scenario := 1
scenario := {}
scenario := new Scenario
Return
As a result I get the following messages:
NEW
NEW
DELETE
DELETE
Questions:
Why doesn't the object get destroyed during the second assignment? I'd assume the number of refs going to 0, no?
How come I get 2 destructions in a row? Where was that object stored meanwhile? How could scenario variable hold both references?
Why was not the third construction called?
Why doesn't the object get destroyed during the second assignment?
Garbage collection had not been triggered yet
I'd assume the number of refs going to 0, no?
References going to 0 does not necessarily trigger GC
How come I get 2 destructions in a row?
Garbage collection cleaned both references at the same time
Where was that object stored meanwhile?
The heap
How could scenario variable hold both references?
scenario does not hold both references
Why was not the third construction called?
Only two Scenario objects are constructed. The variable scenario is a dynamic variable and is not always an instance of the class Scenario. The last assignment scenario := {} just creates an empty object.
Ok, found out what was missing. Two things:
AHK script is case-insensitive.
Since class is an object by itself in AHK it's possible to override the class by another object.
Here is a piece of the documentation:
Because the class is referenced via a variable, the class name cannot be used to both reference the class and create a separate variable (such as to hold an instance of the class) in the same context. For example, box := new Box would replace the class object in Box with an instance of itself. [v1.1.27+]: #Warn ClassOverwrite enables a warning to be shown at load time for each attempt to overwrite a class.
This explains what happened in the code above: variable name scenario is effectively the same as a class name Scenario, so I just quietly overrode my class with an empty object.
Also, since the new instance of the class is created before assignment, I got two 'NEW' in a row, only than 'DELETE'.

How to reference a stage instance of a MovieClip from a non-document Class without causing Error 1120?

I have a basic AS3 question that has to do with the relationship between object on the stage and objects that are created and controlled by AS3 script. This question has already been addressed tons of times, and I've researched it for hours, but I found drastically different answers, many which were really complicated. I'm looking for the simplest, most general solution to this issue that is humanly (or perhaps I should say computerly) possible.
I want to be able reference an instance of an object on the stage inside a class that is not the main document class. Before I try to explain in my own terms, it might be better to view one of these posts, which cover the exact topic I'm confused about. If any of you understand the issue, and understand the solutions to one of these posts, perhaps you could translate one of the correct answers into n00banese for me. Rather than just focusing on how they did it, you could explain WHY it was necessary to do it that. I would be very thankful if someone could do that much, and if you can it is not necessary to read any further into this post.
AS3 - Access MovieClip from stage within a class
How do I access a MovieClip on the stage from the Document Class?
AS3 Modify stage Objects from a class
how do I make non-document-class classes 'aware' of stage components in Flash AS3?
Accessing ActionScript3 Nested Movie Clips from Class
Since it's highly possible that all of the above-mentioned situations are too complicated for me, I recreated the problem in the simplest, most general way possible in hopes that it would be easier to explain in this case.
I have a MovieClip on the stage with instance name scene_mc. scene_mc does nothing except separate one scene from another. It's linked to a .as file named Scene. Nested inside scene_mc there is a movieclip called thing_mc, linked to .as file Thing. I want to move thing_mc across the scene. I could easily do this by changing its coordinates in the main document class, something like this:
thing_mc.x = thing_mc.x + 100;
But since thing_mc is nested inside of scene_mc, I would like to control thing_mc's position inside the SCENE class so that the coordinates are relevant to other nested symbols in this class. And because in more complicated projects, it just makes sense to control a nested symbol in the code of its parent. So inside Scene.as, I might create this method:
public function moveThing() {
thing_mc.x = thing_mc.x + 200;
}
This causes this error: "1120: Access of undefined property thing_mc."
Error 1120 happens any time one tries to reference a stage instance of a symbol a .as file that is not the main document class.
So to reiterate my question, how can I let the Scene class know what thing_mc is? Or how can I let any class know what any stage instance is? How do you reference a stage instance of an object through a non-document class?
Oh! This might be helpful. The Actioscript 3.0 docs say this- Note: Children instances placed on the Stage in the Flash authoring tool cannot be accessed by code from within the constructor of a parent instance since they have not been created at that point in code execution. Before accessing the child, the parent must instead either create the child instance by code or delay access to a callback function that listens for the child to dispatch itsĀ Event.ADDED_TO_STAGEĀ event.
So I mean, really I get why what I'm trying to do would cause an error, but I still don't get how to work around it.
Anyone who actually read this far into my gibberish, thank you. You are a kind and patient soul, haha. This issue is really consuming me so any attempt to help me understand the theory of how this works would be IMMENSELY appreciated. :D
...From what I understood after an all nighter (and sorry if this isn't the answer you're looking for)
You need to link Scene (the object in your library in Adobe Flash (or Adobe Animate if you're using that)) to your class. (Export for Actionscript) BUT (and here's the kicker) you need to also check export for frame 1 and make sure it's loaded on frame 1.
I run into issues like this when I'm not paying attention. Make sure the movieclip nested inside your main movieclip, somehow, gets loaded. Be it through the constructor, or on frame 1. Say your nested movieclip is on frame 2. Well, flash hasn't loaded frame 2. Therefor, it hasn't constructed anything inside frame 2. So it has no idea what you're talking about.
At times, i've had to do a gotoAndStop(2) and then right back to 1 just so that an object on frame 2 was named and ready to be handled in the future.