Re: " //Clean Up managed resources " f&*ck



oOn 19 Maj, 13:35, Moty Michaely <Moty...@xxxxxxxxx> wrote:
On May 19, 2:03 pm, "Koliber (js)" <profesor_kinb...@xxxxxxxxxxxxxx>
wrote:



On 19 Maj, 11:43, Moty Michaely <Moty...@xxxxxxxxx> wrote:

On May 19, 12:33 am, "Koliber (js)" <profesor_kinb...@xxxxxxxxxxxxxx>
wrote:

If sql connection is managed resource
why dispose if called from finaliser in pattern above
should leave it untouched?

The finalizer is called by the GC. Implementing a finalizer is the
only way to code things that needs to be done at instance collection
time (such as releasing internal unmanaged resources). Since Dispose
methods is responsible for cleaning resources, the finalizer code
usually calls the Dispose method.
Therefore, you are right - it's there to guarantee object disposal if
not called manually. But, again, the GC is not deterministic. The
finalizar can practically call the finalizer 10 minutes after it
really got pinned off... Therefore, manual disposal is a best practice
to make sure that unmanaged resources will not be held for 10 minutes,
for nothing....

And if sql connection is unmanaged resource
what the hell is managed resource and how
it can be explicite free'd? :(

Quote: "In Microsoft Windows terminology, managed code is computer
instructions - that is, "code" - executed by a CLI-compliant virtual
machine, such as Microsoft's .NET Framework Common Language Runtime,
or other CLI implementations from The Mono Project or the DotGNU
Project."

Taken from:http://en.wikipedia.org/wiki/Managed_code

Therefore, SqlConnection is a managed code that uses unmanaged
resources.

Hope this clears things..

Moty

Sorry but not at all :( - my question I am putting all the time is
not
about managed code (all things you said i do know)
it is about 'managed resource'. What the hell it is? and how is
method
of clearing it? (see dispose pattern in 1st post there are two areas
for cleaning code one for unmanaged resources (clear for me i think -
like
sql connection) and second for the managed resource cleaning i do not
know
what it is.

--
test

Hi,

Okay, about unmanaged resources:
Unmanaged resource is code that is invoked in unmanaged way such as
windows API calls.

For example:
Let's imagine that you need to develop in a managed way (.NET class) a
class that creates a file and closes it when it goes out of scope.
And let's also imagine that there is no managed class that doe's that,
so you can't use File.Create static method of the .NET System.IO File
class.

How would you do that?

To do so we will need to use p/Invoke to import windows API methods
for file manipulations.

// Import the CreateFile method from kernel32.dll
[DllImport("Kernel32.dll")]
static extern IntPtr CreateFile(
string filename,
[MarshalAs(UnmanagedType.U4)]FileAccess fileaccess,
[MarshalAs(UnmanagedType.U4)]FileShare fileshare,
int securityattributes,
[MarshalAs(UnmanagedType.U4)]FileMode
creationdisposition,
int flags,
IntPtr template);

// Create text.txt file in the current directory
IntPtr ptr= CreateFile("text.txt",FileAccess.ReadWrite,
FileShare.ReadWrite,0,FileMode.Create,0, IntPtr.Zero);

After we obtained a file handle, we will have to expose methods for
writing or reading from the file.. Let's say that we did.
How would we close the file handle?

There is an API call for closing the file handle (or otherwise, the
file will stay opened until the os decides to release it, sometimes
forever...)
[DllImport("kernel32", SetLastError=true)]
static extern unsafe bool CloseHandle(IntPtr hObject);

So to close the file we will have to call CloseHandle(ptr);

When will we call the CloseHandle? Probably in the finalizer. But,
it's more than logical to allow the users of your class to be able to
close the file before the GC calls the finalizer? Here comes the
Dispose pattern..

It's true that the use of unmanaged code (or unsafe code) is *rarely*
required in C# but there are some situations that require them:
* Dealing with existing structures on disk
* Advanced COM or Platform Invoke (p/Invoke) scenarios that
involve structures with pointers in them
* Performance-critical code

Hope this clears it :)

Moty

Tnx for answers but you do not talkin abaut what I am askin :( You
still and only talkin about "unmanaged
resource" Can you answer on that:

What is managed resource (there in dispose pattern from 1st post) ?
----------------------------------------------------------------------------------------------------------
How ppl can free such managed resource (wchich i do not understand
what is) ?
-------------------------------------------------------------------------------------------------------------------------

(as i wrote i can think that managed resource could be an every
managed object but as far as
i know such objects are not cleaned by 'commands' - only when
references to it are lost - so
there in didpose pattern it should be some thing diffrent thing - but
what?) It does make me real angry
that nobody writes what it is.

[here now i am goin to eat dinner, be later]




.



Relevant Pages

  • Re: " //Clean Up managed resources " f&*ck
    ... time (such as releasing internal unmanaged resources). ... Since Dispose ... methods is responsible for cleaning resources, the finalizer code ... it is about 'managed resource'. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: " //Clean Up managed resources " f&*ck
    ... The finalizer is called by the GC. ... time (such as releasing internal unmanaged resources). ... Since Dispose ... it is about 'managed resource'. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: " //Clean Up managed resources " f&*ck
    ... So this leaves the difference between a Managed Resource and a Unmanaged resource. ... These resources will not be closed ot cleaned up by the Garbage Collector, unless the class holding on to the resources implements at least a Finalizer. ... Now on the issue of why we need a dispose function *and* a finalizer. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: " //Clean Up managed resources " f&*ck
    ... So this leaves the difference between a Managed Resource and a Unmanaged ... be cleaned up by the Garbage Collector. ... Now on the issue of why we need a dispose function *and* a finalizer. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: " //Clean Up managed resources " f&*ck
    ... So this leaves the difference between a Managed Resource and a Unmanaged ... be cleaned up by the Garbage Collector. ... Now on the issue of why we need a dispose function *and* a finalizer. ...
    (microsoft.public.dotnet.languages.csharp)