Re: Dispose(bool), Idisposable, form closing etc.

Tech-Archive recommends: Fix windows errors by optimizing your registry



"Brian Gideon" <briangideon@xxxxxxxxx> wrote in message
news:5ac95686-8715-451f-a0d7-b7b8744db726@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Based on what I've seen, I'm not convinced of that. Even when there's
no actual use of the parameter in a method, the object referenced by it
is apparently not collectable until the method returns. It wouldn't
matter whether you called a managed or unmanaged function.


Yeah, after some more thought I think you're right. The interop
marshaler would likely prevent the reference from being eligible until
the function returns. Asynchronous functions and functions that
accept say...an IntPtr to a managed object would still be problematic.

I just tested this because I was pretty sure you were wrong but you are in
fact correct. When passing an instance of a class into an unmanaged function
I couldn't get it to dispose of the object while it was in the function.
Code is below if you want to test.

C++ code:
#include "stdafx.h"
#include "DelMeDll.h"

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

typedef int __stdcall SomeCallback(int Count);

// This is an example of an exported function.
int __stdcall DoIt(void* CallbackPtr, void* SomeClass)
{
SomeCallback* sc = (SomeCallback*)CallbackPtr;
for(int i = 0; i < 10; i++)
{
sc(i);
}
return 42;
}


C# code

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Runtime.InteropServices;

namespace DelMeCallDLL

{

public delegate int CallbackDelegate(int Count);

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Button button2;

private System.Windows.Forms.Button button1;


[DllImport("C:\\Stuff\\DelMeDll\\Debug\\DelMeDll.dll")]

static extern int DoIt(CallbackDelegate CallBack, SomeClass MyClass);

public Form1()

{

InitializeComponent();

}

#region Windows Form Designer generated code

private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.button2 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(104, 80);

this.button1.Name = "button1";

this.button1.Size = new System.Drawing.Size(80, 80);

this.button1.TabIndex = 0;

this.button1.Text = "button1";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// button2

//

this.button2.Location = new System.Drawing.Point(192, 208);

this.button2.Name = "button2";

this.button2.Size = new System.Drawing.Size(64, 48);

this.button2.TabIndex = 1;

this.button2.Text = "button2";

this.button2.Click += new System.EventHandler(this.button2_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(292, 266);

this.Controls.Add(this.button2);

this.Controls.Add(this.button1);

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void button1_Click(object sender, System.EventArgs e)

{

DoIt(new CallbackDelegate(this.Callback), new SomeClass());

}

private int Callback(int Count)

{

GC.Collect();

Console.WriteLine(Count.ToString());

return Count;

}

private void button2_Click(object sender, System.EventArgs e)

{

GC.Collect();

}

}

}


.


Quantcast