PC Review


Reply
Thread Tools Rate Thread

.net object dispose

 
 
laputa
Guest
Posts: n/a
 
      16th Jan 2009
Dear all :

I create a simple code (window application) like following
Private Sub Button1_Click()
Dim Obj As Object
Dim asm As Assembly = Assembly.LoadFrom("TestApp.dll")
Dim ty As Type = asm.GetType("TestApp.appServ")
Obj = Activator.CreateInstance(ty)
Dim Istring As String = Obj.funct()
MessageBox.Show(Istring)
Obj.Dispose()
GC.SuppressFinalize(Obj)

Obj = Nothing
end sub

while TestApp.dll code follow that
using System;
using System.Collections.Generic;
using System.Text;

namespace TestApp
{
public class appServ : IDisposable
{
private bool disposed = false;
public string funct()
{
return "aad";
}
~appServ()
{

// call Dispose with false. Since we're in the
// destructor call, the managed resources will be
// disposed of anyways.
Dispose(false);
}

protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// dispose-only, i.e. non-finalizable logic
}

// shared cleanup logic
disposed = true;
}
}



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

}
}



Before the button click the "TestApp.dll" allow be overwrite
but after button click , the file is locked until the main program exit.
Can anyone tell me how to solve it ?

Thanks

Michael




 
Reply With Quote
 
 
 
 
TDC
Guest
Posts: n/a
 
      16th Jan 2009
Dispose has nothing to do with the dll being locked. That's just they
way dlls are treated when loaded by a process wthin Windows.


On Jan 16, 4:23*am, "laputa" <laputa....@gmail.com> wrote:
> Dear all :
>
> I create a simple code (window application) like *following
> Private Sub Button1_Click()
> * * Dim Obj As Object
> * * * * Dim asm As Assembly = Assembly.LoadFrom("TestApp.dll")
> * * * * Dim ty As Type = asm.GetType("TestApp.appServ")
> * * * * Obj = Activator.CreateInstance(ty)
> * * * * Dim Istring As String = Obj.funct()
> * * * * MessageBox.Show(Istring)
> * * * * Obj.Dispose()
> * * * * GC.SuppressFinalize(Obj)
>
> * * * * Obj = Nothing
> end sub
>
> while TestApp.dll code follow that
> using System;
> using System.Collections.Generic;
> using System.Text;
>
> namespace TestApp
> {
> * * public class appServ : IDisposable
> * * {
> * * * * private bool disposed = false;
> * * * * public string funct()
> * * * * {
> * * * * * * return "aad";
> * * * * }
> * * * * ~appServ()
> * *{
>
> * * * // call Dispose with false. *Since we're in the
> * * * // destructor call, the managed resources will be
> * * * // disposed of anyways.
> * * * Dispose(false);
> * *}
>
> * * protected virtual void Dispose(bool disposing)
> * * {
> * * * * if (!disposed)
> * * * * {
> * * * * * * if (disposing)
> * * * * * * {
> * * * * * * * * // dispose-only, i.e. non-finalizable logic
> * * * * * * }
>
> * * * * * * // shared cleanup logic
> * * * * * * disposed = true;
> * * * * }
> * * }
>
> * * public void Dispose()
> * * {
> * * * * Dispose(true);
> * * * * GC.SuppressFinalize(this);
> * * * * GC.Collect();
> * * }
>
> * * }
>
> }
>
> Before the button click the "TestApp.dll" allow be overwrite
> but after button click , the file is locked until the main program exit.
> Can anyone tell me how to solve it ?
>
> Thanks
>
> Michael


 
Reply With Quote
 
Michel Posseth [MCP]
Guest
Posts: n/a
 
      17th Jan 2009

AFAIK


dll`s are locked as long as the app domain is loaded , so create seperate
app domains if you want the dll to be freed
hth

michel


"laputa" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...
> Dear all :
>
> I create a simple code (window application) like following
> Private Sub Button1_Click()
> Dim Obj As Object
> Dim asm As Assembly = Assembly.LoadFrom("TestApp.dll")
> Dim ty As Type = asm.GetType("TestApp.appServ")
> Obj = Activator.CreateInstance(ty)
> Dim Istring As String = Obj.funct()
> MessageBox.Show(Istring)
> Obj.Dispose()
> GC.SuppressFinalize(Obj)
>
> Obj = Nothing
> end sub
>
> while TestApp.dll code follow that
> using System;
> using System.Collections.Generic;
> using System.Text;
>
> namespace TestApp
> {
> public class appServ : IDisposable
> {
> private bool disposed = false;
> public string funct()
> {
> return "aad";
> }
> ~appServ()
> {
>
> // call Dispose with false. Since we're in the
> // destructor call, the managed resources will be
> // disposed of anyways.
> Dispose(false);
> }
>
> protected virtual void Dispose(bool disposing)
> {
> if (!disposed)
> {
> if (disposing)
> {
> // dispose-only, i.e. non-finalizable logic
> }
>
> // shared cleanup logic
> disposed = true;
> }
> }
>
>
>
> public void Dispose()
> {
> Dispose(true);
> GC.SuppressFinalize(this);
> GC.Collect();
> }
>
> }
> }
>
>
>
> Before the button click the "TestApp.dll" allow be overwrite
> but after button click , the file is locked until the main program exit.
> Can anyone tell me how to solve it ?
>
> Thanks
>
> Michael
>
>
>
>



 
Reply With Quote
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      18th Jan 2009
There is no net object dispose in the context of your subject.

Every object inherited from components has a method to release unmanaged
resources.

The name of the method (shortcut) to invoke do this is dispose (unmanaged
resources).

However it has in fact (as long as this is not overridden to do other
things) only slightly to do with the dispose of an object itself.

It is strongly advised to do in Net nothing about destruction of objects
yourself as this is done by managed code.

Doing the destruction yourselfs can lead to unpredictable results.

Cor

"laputa" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Dear all :
>
> I create a simple code (window application) like following
> Private Sub Button1_Click()
> Dim Obj As Object
> Dim asm As Assembly = Assembly.LoadFrom("TestApp.dll")
> Dim ty As Type = asm.GetType("TestApp.appServ")
> Obj = Activator.CreateInstance(ty)
> Dim Istring As String = Obj.funct()
> MessageBox.Show(Istring)
> Obj.Dispose()
> GC.SuppressFinalize(Obj)
>
> Obj = Nothing
> end sub
>
> while TestApp.dll code follow that
> using System;
> using System.Collections.Generic;
> using System.Text;
>
> namespace TestApp
> {
> public class appServ : IDisposable
> {
> private bool disposed = false;
> public string funct()
> {
> return "aad";
> }
> ~appServ()
> {
>
> // call Dispose with false. Since we're in the
> // destructor call, the managed resources will be
> // disposed of anyways.
> Dispose(false);
> }
>
> protected virtual void Dispose(bool disposing)
> {
> if (!disposed)
> {
> if (disposing)
> {
> // dispose-only, i.e. non-finalizable logic
> }
>
> // shared cleanup logic
> disposed = true;
> }
> }
>
>
>
> public void Dispose()
> {
> Dispose(true);
> GC.SuppressFinalize(this);
> GC.Collect();
> }
>
> }
> }
>
>
>
> Before the button click the "TestApp.dll" allow be overwrite
> but after button click , the file is locked until the main program exit.
> Can anyone tell me how to solve it ?
>
> Thanks
>
> Michael
>
>
>
>


 
Reply With Quote
 
Michel Posseth [MCP]
Guest
Posts: n/a
 
      18th Jan 2009
Hello Michael



' Assembly you want to load
Dim assemblyFileName As String = "D:\Test\TestApp.dll"
Dim assemblyName As String = "Test"

' Setup the evidence
Dim evidence As New Evidence(AppDomain.CurrentDomain.Evidence)
Dim TestDomain As AppDomain = AppDomain.CreateDomain(
"TestDomain", ' The friendly name of the domain.
evidence, ' Evidence mapped through the security policy to establish a
top-of-stack permission set.
AppDomain.CurrentDomain.BaseDirectory, ' The base directory that the
assembly resolver uses to probe for assemblies.
System.IO.Path.GetFullPath(assemblyFileName), ' The path relative to
the base directory where the assembly resolver should probe for private
assemblies.
True) ' If true, a shadow copy of an assembly is loaded into this
application domain.

Console.WriteLine(TestDomain.Load(assemblyName).FullName)

' You use the assembly

AppDomain.Unload(TestDomain) ' Unload the AppDomain

AppDomain.Unload should unlock the assembly loaded into that appDomain. ,
you load the assembly into your main appDomain. In which case, you will
lock it until you shutdown your application and thus that appDomain is
unloaded .


Lifeng Lu MSFT for VB once showed this verry nice example

<<<<

I think I will do is not to call AppDomain.Load function, but you create an
agent class, which must be a MarshalByRefObject class in your application.
After you create the appDomain, you create an instance of that Agent class
in the appDomain, and in your main appDomain, you can get a marshalled
handler of this agent, and then, you call its function. Only in that
function, which will run inside the sub appDomain will load that seperated
assembly, and uses its functions.



Your code would look like:

Class AgentInAppDomain

Inherits MarshalByRefObject

Sub RunFunctionInAppDomain(...)

Try

dim yourAssembly As Assembly = Assembly.LoadFrom(...)

' do what you need do here...

Catch ex As Exception

Throw new InvalidOperationException(ex.Message)

End Try

End Sub

End Class



In your main function

Dim subDomain As AppDomain = AppDomain.CreateDomain(...)

dim agent as AgentInAppDomain =
subDomain.CreateInstanceAndUnwrap(GetType(AgentInAppDomain).Assembly.GetName().FullName,
GetType(AgentInAppDomain).FullName)

agent.RunFunctionInAppDomain()

....

subDomain.Unload()



You can fill in the details. Remember you should not pass any type defined
in that assembly into your main appDomain, including exceptions, otherwise,
you would load that assembly into your main appDomain.



Lifeng Lu

MS VB

>>>




I hope that above examples give you an idea how you can solve your problem ,
the Locking is not because of the Dispose but because of the appDomain
still beeing loaded wich in its turn locks the Dll



HTH



Michel Posseth [MCP]

http://www.vbdotnetcoder.com/

















"Michel Posseth [MCP]" <(E-Mail Removed)> schreef in bericht
news:%(E-Mail Removed)...
>
> AFAIK
>
>
> dll`s are locked as long as the app domain is loaded , so create seperate
> app domains if you want the dll to be freed
> hth
>
> michel
>
>
> "laputa" <(E-Mail Removed)> schreef in bericht
> news:(E-Mail Removed)...
>> Dear all :
>>
>> I create a simple code (window application) like following
>> Private Sub Button1_Click()
>> Dim Obj As Object
>> Dim asm As Assembly = Assembly.LoadFrom("TestApp.dll")
>> Dim ty As Type = asm.GetType("TestApp.appServ")
>> Obj = Activator.CreateInstance(ty)
>> Dim Istring As String = Obj.funct()
>> MessageBox.Show(Istring)
>> Obj.Dispose()
>> GC.SuppressFinalize(Obj)
>>
>> Obj = Nothing
>> end sub
>>
>> while TestApp.dll code follow that
>> using System;
>> using System.Collections.Generic;
>> using System.Text;
>>
>> namespace TestApp
>> {
>> public class appServ : IDisposable
>> {
>> private bool disposed = false;
>> public string funct()
>> {
>> return "aad";
>> }
>> ~appServ()
>> {
>>
>> // call Dispose with false. Since we're in the
>> // destructor call, the managed resources will be
>> // disposed of anyways.
>> Dispose(false);
>> }
>>
>> protected virtual void Dispose(bool disposing)
>> {
>> if (!disposed)
>> {
>> if (disposing)
>> {
>> // dispose-only, i.e. non-finalizable logic
>> }
>>
>> // shared cleanup logic
>> disposed = true;
>> }
>> }
>>
>>
>>
>> public void Dispose()
>> {
>> Dispose(true);
>> GC.SuppressFinalize(this);
>> GC.Collect();
>> }
>>
>> }
>> }
>>
>>
>>
>> Before the button click the "TestApp.dll" allow be overwrite
>> but after button click , the file is locked until the main program exit.
>> Can anyone tell me how to solve it ?
>>
>> Thanks
>>
>> Michael
>>
>>
>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to dispose of a Pen object Tony Johansson Microsoft C# .NET 0 17th Apr 2011 11:45 AM
object dispose laputa Microsoft Dot NET Framework 2 19th Jan 2009 09:46 AM
Dispose GDI+ object #Hai Microsoft Dot NET Framework 3 9th Jul 2003 09:21 PM
Dispose GDI+ object #Hai Microsoft Dot NET 3 9th Jul 2003 09:21 PM
Dispose GDI+ object #Hai Microsoft C# .NET 3 9th Jul 2003 09:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:40 PM.