« first day (1475 days earlier)      last day (3472 days later) » 

6:00 PM
it only queues the callback on the threadpool
 
Mmn, that's true.. but how does it guarantee that it will fire in X seconds?
@KendallFrey So you are saying it will only queue once
Eh, @ReedCopsey is here. No offence Kendall. 2 heads > 1 :P
 
@LewsTherin there is no guarantee it will ever fire
 
@KendallFrey What?
 
 public static class BuysBll
    {
        private static readonly ILog log = LogManager.GetLogger(typeof (BuysBll));

        public static List<Buys> GetAllBuys()
        {
            log4net.Config.XmlConfigurator.Configure();
            try
            {
                object o = null;
                int j = (int) o;
            }
            catch (Exception exception)
            {
                log.Error("BuysBll", exception);
                return null;
            }
            return null;
 
The only guarantee is that it will not fire before the allotted time
@LewsTherin If the threadpool is infinitely busy, it won't fire
 
6:03 PM
@KendallFrey But it may never fire? Why not?
Oh right.
 
how can i avoid using log4net.config.xmlconfigurator.configure() in every function ??
 
In practice, you can depend on it firing, but not promptly
 
Why won't it create a new thread? Doesn't the threadpool handle that
 
because creating threads has too much overhead
 
@KendallFrey That's not a big issue I think. Even if it doesn't fire.. can live with it.
@KendallFrey Yeah, but afaik the threadpool creates a new thread if all other threads are used up. I didn't know it can decide not to create a new thread if the pool is dry.
 
6:05 PM
I think there's a maximum pool size
 
1
Q: log4net only works when XmlConfigurator.Configure() is called

BradI understand that this question has been asked several times, but unfortunately, I haven't been able to get my logging configuration working. I have to be making some very small mistake somewhere. I have a .NET 4.5 MVC 4/EF 5 web application and I'm trying to get logging to work. The solution ...

 
There is a Max and Min number of threads
 
Right, I see.
I wouldn't lose sleep over that (I think/hope)
Thanks for the brain storm Kendall
 
If I have a collection of unmanaged resources (a dictionary to be precise) is it safe to enumerate the collection to release the unmanaged resources from the finalizer?
 
just remove and dispose
 
6:17 PM
@CharlieBrown hm?
 
That's fine
 
get a local reference to it, remove it from the Dict, then dispose it
 
Inside my finalizer I have Dispose(false) which uses foreach (var resource in myDictionary.Values) ReleaseMyResource(resource);
I was just worried that the dictionary will have already been finalized before my object since the MSDN says something about ordering of finalizers being unpredictable.
I wasn't quite sure what that meant
 
you still have a reference inside the dictionary
 
So there is no chance that the .NET dictionary container will get finalized before `MyClass` and the container `IntPtr`'s?

class MyClass { Dictionary<MyKey, IntPtr> myDictionary; ...
 
6:23 PM
What do you mean finalized?
When that happens the Finalizer gets called right?
 
well well well well well
 
When it is, it executes the loop. And until then won't get disposed
 
There is what I have
DisposeEditors loops through _editors.Values and releases the unmanaged resource
public void DisposeEditors() {
foreach (var editor in _editors)
Object.DestroyImmediate(editor);
_editors.Clear();
}
 
Seems fine to me.
 
Awesome thanks :D
It was just one part of the MSDN which had me a little confused
"The finalizers of two objects are not guaranteed to run in any specific order, even if one object refers to the other."
I was worried that the dictionary could become inaccessible before I had a chance to release my unmanaged resources.
 
6:28 PM
Oh.. ok. Take it back :P
 
@LewsTherin ?
 
> That is, if Object A has a reference to Object B and both have finalizers, Object B might have already been finalized when the finalizer of Object A starts.
Thinking about it
Maybe it is best to inherit the dictionary and then override the finalizer
That way you are hundred percent certain
 
Agreed, that's a good idea.
Thanks!
I will do that
 
@LeaHayes Np, best to wait for other ideas though. I've not had to implement a dispose method yet. But I think what I said makes sense (but may not be necessary).
 
Anyone attend build 2014?
 
6:42 PM
@LeaHayes where are you disposing the dict?
 
@CharlieBrown I am disposing the content of the dictionary from my finalizer.
DisposeEditors as shown above
 
the dictionary wont be disposed until its out of scope and no references exist to it, or you dispose it yourself
just finalizing all the content, will not release the container
Now, if you dispose that Dict immediately following the loop, and there is no other reference to it outside that class... then yes, it might run out of order
 
> That is, if Object A has a reference to Object B and both have finalizers, Object B might have already been finalized when the finalizer of Object A starts.
@CharlieBrown It won't be disposed but the finalizer can be called, yes?
 
same thing
 
Isn't that preparing it for disposal?
So if the dictionary is finalized before the object that references it, there is a problem.
> just finalizing all the content, will not release the container
Are you sure that won't throw ObjectDisposedException?
 
6:48 PM
correct, but a dictionary doesnt have a finalizer
so it wont matter much
 
Isn't that something generated by the compiler?
 
yeah, but there is still a reference to it. its not going to finalize something that is still referenced
 
> That is, if Object A has a reference to Object B and both have finalizers, Object B might have already been finalized when the finalizer of Object A starts.
How are you interpreting that?
 
you call finalize on object A, you dont know if A or B gets finalized first
 
so if object A = MyClass
and object B = the Dictionary
if B is finalized before A
 
6:53 PM
@CharlieBrown Yeah, so what do you mean by: "yeah, but there is still a reference to it. its not going to finalize something that is still referenced" That doesn't hold true any longer
 
A cannot enumerate the dictionary
 
Although I have read that an object can't be disposed if there are references to it.
 
your calling finalize on A... in your finalizer (before the gc.finalize) you are doing a loop
im going to assume your not calling finalize before the loop
 
I am only calling .Dispose from IDisposable
 
You can't call finalize...
If I understand correctly, the GC can do:
dict.~Dictionary<K,T>()
before:
obj.~SomeObject()
 
6:57 PM
@LewsTherin that's how i've been interpreting that paragraph from the MSDN
but it's not written very clearly imo
 
sure, it can... during the ~SomeObject
if you implement IDisposable, you will write a method specifically for releasing resources, which will run before ~ finalize
 
@CharlieBrown The point is the dictionary's finalizer may have already executed. But if that's true, what happens if the finalized is called twice? The GC is probably able to tell
 
unless, your writing your loop in ~SomeObject, then your screwed
its not, omg
Dispose is called before ~
 
@CharlieBrown Yes, that's what he was doing.
 
once ~ is called, your fucked
 
6:59 PM
SomeObject.~ is calling Dispose in my case
as per MSDN guidelines
 
thats bassakwards
 
or do you mean you have ~SomeObject(){ Dispose(); } and you have your own Dispose method?
 
@CharlieBrown did you look at my paste?
 
no missed it
 
7:01 PM
@CharlieBrown hastebin.com/ujimiheget.cs
public void DisposeEditors() {
foreach (var editor in _editors)
Object.DestroyImmediate(editor);
_editors.Clear();
}
 
so yeah, your good. Your Suppressing finalize
 
?
Finalizer calls the other dispose method
 
except you should be calling Dispose() not Dispose(false)
 
which then accesses the dictionary
MSDN says call Dispose(false)
 
thats an old pattern, let me find a newer reference
ok, im wrong
but not about what happens
 
7:07 PM
So you're saying Dispose suppresses finalize?
 
So finalize when overriden is going to get called to your version, which calls Dispose(false) and kills your resources. Then, .net is going to Dispose() your class, at which point your call to GC.Suppress will stop the finalizer
 
You're confusing me :P You can't override finalize. Nor can .NET call Dispose
Forget about Dispose(). Focus on the destructor for now.
The spec is saying that the dictionary's finalize can execute before the wrapper's finalize.
In which case, the wrapper's finalize can attempt to dispose the managed resource twice.
Tbh, that's a shitty explanation from MS
@LeaHayes Actually looking at your code. It seems fine. But then again, what happen if the finalize on the dictionary is called. Before you call dispose? Lulz. Just ignore the spec :P
Memory leak ftw
 
@LewsTherin lol
This isn't system critical so memory leak isn't the end of the world.
I just hate not understanding how something works :P
 
VS 2012 is full of that shit anyways :P
@LeaHayes Same
I think that's a good SO question. If you ask it properly.
 
Dispose is designed for releasing resources in that manner, your using it for what its meant in the correct way
@LewsTherin you can create a finalizer in any class with ~SomeClass
 
7:16 PM
@LeaHayes If you tl;dr the question/discussion, I can try to help
 
@KendallFrey Middle finger :P
 
inserted about 3 inches
 
Did you like it?
I bet you did.
 
not as much as she did
 
@KendallFrey Answer the question anyways. It will help me.
@KendallFrey A "she"? What alien is this? Wake up boy
 
7:17 PM
your mom
 
Ugh, eww
I can't believe you did that. You dirty fecker!
 
I just got finished with a mom discussion in the other chat, and I've got momentum on my side
 
Hope they were talking about your mom
 
not for long
 
Perhaps you were bawling
 
7:20 PM
perhaps not
 
Ok srsly, answer teh question
Before I feck off.
 
Ok srsly, ask teh question
 
> @LeaHayes If you tl;dr the question/discussion, I can try to help
So you know the question.
 
wot
My scroll up button is broken, I can't see it
 
Biatch
> If I have a collection of unmanaged resources (a dictionary to be precise) is it safe to enumerate the collection to release the unmanaged resources from the finalizer?
> Inside my finalizer I have Dispose(false) which uses foreach (var resource in myDictionary.Values) ReleaseMyResource(resource);
I was just worried that the dictionary will have already been finalized before my object since the MSDN says something about ordering of finalizers being unpredictable.
I wasn't quite sure what that meant
Not quite (by a long shot) the same thing though :'(
 
7:25 PM
@LewsTherin Are they implementing IDisposable and a finalizer on these?
 
@MrDoom This is not me.
But I think he is suppressing the finalizer.
 
0
Q: How to properly dispose collection of unmanaged resources from finalizer?

Lea HayesHere is an example of which I am uncertain about: public class SomeClass : IDisposable { ~SomeClass() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private bool _disposed; protected virtual void Dispose...

 
So long as he's properly handling the GC calling the finalizer when it's already in the middle of being disposed manually
 
@MrDoom one should always implement a finalizer in IDisposable
@MrDoom that can't happen
 
@KendallFrey I agree, but I've seen some things man
@KendallFrey Why not?
 
7:28 PM
@MrDoom because if it's being disposed, there's a reference to it
 
Oh, I see what you're saying
m
 
So writing an app to turn the new MIcrosoft Watch (Band) into a lie detector. It has a Galvanic sensor
 
@KendallFrey Are you saying the GC has a reference to it already? Not sure what you actually mean.
 
I'm partial to this answer
593
Q: Proper use of the IDisposable interface

cwickI know from reading the MSDN documentation that the "primary" use of the IDisposable interface is to clean up unmanaged resources. To me, "unmanaged" means things like database connections, sockets, window handles, etc. But, I've seen code where the Dispose method is implemented to free managed...

 
> ~MyObject()
{
Dispose(false); //I am *not* calling you from Dispose, it's *not* safe
}
why doesnt formatting work for me in this chat? lol
so, MrDoom's link suggests that the dictionary is not safe to access
 
7:33 PM
Well
 
@LewsTherin If an object is being disposed manually, someone needed to call it, and for that person to call it, it needs a reference, and if it has a reference, the object isn't eligible for collection/finalization
 
That
 
@KendallFrey That makes sense. So what does that snippet in the spec, mean then?
Seems contradictory
Unless it falls to a condition where Dispose is irrelevant
And the GC is free to call the finalizer of dictionary or the wrapper (in any order).
 
@LewsTherin which one?
 
> The finalizers of two objects are not guaranteed to run in any specific order, even if one object refers to the other. That is, if Object A has a reference to Object B and both have finalizers, Object B might have already been finalized when the finalizer of Object A starts.
Forget about Dispose, or anyone calling Dispose.
 
7:37 PM
It's hard to do that, because not using Dispose is so bad
Basically, don't access any managed members in the finalizer
 
Yeah, I can see that. But in the event that someone neglects to call dispose. Then I can certainly see the dictionary cleaned out before the wrapper is.
@KendallFrey But you can create custom dictionary just for unmanaged memory. Just effort.. slightly
 
If the finalizer is running, that should indicate that nothing was disposed, and you should let each object's respective finalizer dispose itself
 
@LewsTherin
 
I agree with Kendall
 
that depends on whether IntPtr[] _unmanagedMemory can be safely accessed from the finalizer.
I dno how Dictionary is implemented, but internally it will have managed objects referencing the contained IntPtr's
 
7:40 PM
@KendallFrey Yes, and that's where the question arises. Because then you may be accessing a disposed object.
 
@LewsTherin accessing a disposed object and accessing a finalized object are two different things
 
@KendallFrey If you mean finalized object may just be added to the generation queue I get that.
 
I am a bit fuzzy on this, for example, what happens to an array
 
The linked answer above (only just scrolled and read it) is slightly inaccurate.
It is considered bad practice to ever use GC.SuppressFinalize(this);
 
When an object containing an IntPtr[] is placed in freachable, the array won't be collected, but it may be finalized (if it had a finalizer) before the main object's finalizer, and thus all those IntPtrs will be inaccessible and leaked
 
7:47 PM
There is a good eric lippert rant about it somewhere
 
I must be wrong somewhere in there
@TravisJ wot
 
@TravisJ 'splain? I've used this pattern in a few places with no noticed ill effects.
 
@MrDoom Doesn't mean it is good :P
 
@LewsTherin Very true
 
7:49 PM
@KendallFrey So after this long winded discussion. We end up at the same point.
 
@TravisJ That's the first I've ever heard that, you can't just say that and not back it up
 
Mmn, so a finalizer is not generated like a constructor?
 
@KendallFrey - Nope
@KendallFrey - I was also trying to find the link
 
@LewsTherin what do you mean?
 
I wonder if it was in relation to threading
Okay, well I retract it for now
I will try to find the article
 
7:52 PM
@LewsTherin It doesn't need to be.
 
@KendallFrey I always assumed that a finalizer will be compiler generated if one is not specified. So all classes must have a finalizer. At least I think that what happens in C++
 
@LewsTherin no, not at all
finalizers are expensive, and are only generated when they exist in code
 
Finalizers aren't general like in C++ where you must manually clean up memory. The GC already can handle cleaning up everything within managed code. Finalizers are for stuff that is not explicitly managed.
 
@MrDoom Yeah but even if you don't have anything to clean up the compiler still generates one for you.
@KendallFrey Ah I see. In that case...
 
@LewsTherin So why would the compiler bother generating a function that has an empty body, when it is optional to begin with ;)
 
7:55 PM
@LewsTherin In C#, the GC basically just deletes the memory (I think). Nothing special required after finalization
 
@MrDoom Wouldn't that be the same as asking why a default ctor is generated?
 
Hm, I was incorrect I believe.
There is nothing wrong with GC.SuppressFinalize(this);
 
Good
@LewsTherin ctors aren't optional
 
Sorry for the confusion
 
Even though in principle, they could be
 
7:56 PM
I guess -_-
If the compiler sees it as mandatory then yeah. Makes sense
 
On top of that, in order for an object to be created you need to have something to call with New
hence why they're mandatory
 
yes
in theory, there's no reason why a constructor would be required though
you could just zero-fill everything
 
That's still an initialization step
 
yes, initialization of an object is mandatory even in theory
 
    protected virtual T GetInstance<T>( int id ) where T : class, IBusinessObject
    {
        if( typeof( T ) == typeof( User ) )
        {
            return(T)(object) new User( id );
        }
        if( typeof( T ) == typeof( Department ) )
        {
            return (T)(object)new Department( id );
        }
        if( typeof( T ) == typeof( Patient ) )
        {
            return (T)(object)new Patient( id );
        }
        if( typeof( T ) == typeof( Request ) )
        {
            return (T)(object)new Request( id );
love it
 

« first day (1475 days earlier)      last day (3472 days later) »