Unmanaged resources

D

Davis

Hi, I'm confused about one issue. If a form PInvokes into
unmanaged dll are any resources allocated as a result of the
PInvoke call itself. I read somewhere that an object which allocates
unmnaged resources needs to be manually disposed.

My current thinking is that a Pinvoke does not allocate an
unmanged resource as a result of the call itself..
The unmanged dll may (in my case does and frees them,i've checked with
CodeSnitch).
So as long as any resources in the unmanged code are freed one is ok.

Is this correct?

Thanks
 
P

Paul G. Tobey [eMVP]

The reason that you need to dispose such resources is, I think, obvious.
The garbage collector doesn't know when you're done with them.

Calls don't allocate memory or operating system handles.

Paul T.
 
G

Guest

Hi, I'm confused about one issue. If a form PInvokes into
unmanaged dll are any resources allocated as a result of the
PInvoke call itself.

A little is allocated for the call itself, but that's it.
I read somewhere that an object which allocates
unmnaged resources needs to be manually disposed.

Any object that allocates unamanaged resources must also free them, as the
GC won't know how to. Typically this is done in the object's finalizer.
It's good practice to implement Dispose on these types of objects, and a
general rule is that if you use an object that implements Dispose, you
should call Dispose when you're done with it.
My current thinking is that a Pinvoke does not allocate an
unmanged resource as a result of the call itself..
Correct.

The unmanged dll may (in my case does and frees them,i've checked with
CodeSnitch).
So as long as any resources in the unmanged code are freed one is ok.

What happens in the Dll stays in the Dll (generally anyway, there are
exceptions, like CeRapiInvoke). If the Dll allocates memory, it should free
it up. Now sometimes that has to happen by an external method call, but
that should be documented.
Is this correct?

Hard to say, as there are exceptions for everything. Generally if you
allocate a resource, you need to deallocate it. If something else allocates
memory, it should clean up after itself and you shouldn't have to care about
it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top