Click here to Skip to main content
14,386,860 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.
 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
honey the codewitch14-May-19 14:52
Memberhoney the codewitch14-May-19 14:52 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
Super Lloyd14-May-19 15:04
MemberSuper Lloyd14-May-19 15:04 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
honey the codewitch14-May-19 15:28
Memberhoney the codewitch14-May-19 15:28 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
Super Lloyd14-May-19 16:31
MemberSuper Lloyd14-May-19 16:31 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
honey the codewitch14-May-19 17:16
Memberhoney the codewitch14-May-19 17:16 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
Super Lloyd14-May-19 17:44
MemberSuper Lloyd14-May-19 17:44 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
honey the codewitch14-May-19 17:48
Memberhoney the codewitch14-May-19 17:48 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
Super Lloyd14-May-19 18:13
MemberSuper Lloyd14-May-19 18:13 
codewitch honey crisis wrote:
I just don't have a good enough justification to use because it doesn't prevent resource leaks - win32 does that, as I said.


This class is potentially leaking, and Win32 is NOT magically fixing it.
public class Class : IDisposable
{
    IntPtr unmanaged;

    public Class()
    {
        unmanaged = Marshal.AllocHGlobal(10);
    }

    public void Dispose()
    {
        Marshal.FreeHGlobal(unmanaged);
        unmanaged = IntPtr.Zero;
    }
}

And this class is not leaking and is nicely garbage collected.
public class Class : IDisposable
{
    IntPtr unmanaged;

    public Class()
    {
        unmanaged = Marshal.AllocHGlobal(10);
    }
    ~Class() { Dispose(false); }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected void Dispose(bool disposing)
    {
        Marshal.FreeHGlobal(unmanaged);
        unmanaged = IntPtr.Zero;
    }

but the real question is... why do you refuse to do implementation 2? What so complicated or costly about it? Why do you so adamantly want to introduce memory leak? That doesn't make no sense! OMG | :OMG: Confused | :confused:

BTW bonus implementation with explicit memory pressure
public class Class : IDisposable
{
    IntPtr unmanaged;

    public Class()
    {
        unmanaged = Marshal.AllocHGlobal(10);
        GC.AddMemoryPressure(10);
    }
    ~Class() { Dispose(false); }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected void Dispose(bool disposing)
    {
        if (unmanaged != IntPtr.Zero)
        {
            Marshal.FreeHGlobal(unmanaged);
            GC.RemoveMemoryPressure(10);
            unmanaged = IntPtr.Zero;
        }
    }
}
A new .NET Serializer
All in one Menu-Ribbon Bar
Taking over the world since 1371!

GeneralRe: Some thoughts on the .net CLR/CLI Pin
honey the codewitch14-May-19 18:17
Memberhoney the codewitch14-May-19 18:17 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
honey the codewitch14-May-19 18:24
Memberhoney the codewitch14-May-19 18:24 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
Super Lloyd14-May-19 18:30
MemberSuper Lloyd14-May-19 18:30 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
honey the codewitch14-May-19 18:38
Memberhoney the codewitch14-May-19 18:38 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
Super Lloyd14-May-19 19:16
MemberSuper Lloyd14-May-19 19:16 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
honey the codewitch14-May-19 19:30
Memberhoney the codewitch14-May-19 19:30 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
honey the codewitch14-May-19 19:32
Memberhoney the codewitch14-May-19 19:32 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
Super Lloyd14-May-19 19:40
MemberSuper Lloyd14-May-19 19:40 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
honey the codewitch14-May-19 19:43
Memberhoney the codewitch14-May-19 19:43 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
Super Lloyd14-May-19 19:55
MemberSuper Lloyd14-May-19 19:55 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
honey the codewitch14-May-19 20:03
Memberhoney the codewitch14-May-19 20:03 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
Super Lloyd14-May-19 20:11
MemberSuper Lloyd14-May-19 20:11 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
honey the codewitch14-May-19 20:32
Memberhoney the codewitch14-May-19 20:32 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
Super Lloyd14-May-19 20:37
MemberSuper Lloyd14-May-19 20:37 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
honey the codewitch14-May-19 20:56
Memberhoney the codewitch14-May-19 20:56 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
Super Lloyd14-May-19 21:32
MemberSuper Lloyd14-May-19 21:32 
GeneralRe: Some thoughts on the .net CLR/CLI Pin
honey the codewitch15-May-19 4:38
Memberhoney the codewitch15-May-19 4:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.