Can I make a compiler error or warning if someone fails to use the return value of a function.

P

Peter Strøiman

Hi.

I have a class with two similar methods.
One returns an object implementing IDisposible (and it MUST be disposed)
The other doesn't return anything.

When using the class, it is quite easy to mistakenly call the first method
when you intended to use the second. This leads to a ressource leak.

In most modern day programming languages (in my case C#) it is legal to call
a function and disregard the return value.
Is there any attributes I can place on my function that will let the
compiler generate an error (or at least a warning) if you call
the first method and disregard the return value.

Thanks in advance,
Peter Strøiman
 
W

Wiktor Zychla

I have a class with two similar methods.
One returns an object implementing IDisposible (and it MUST be disposed)
The other doesn't return anything.

When using the class, it is quite easy to mistakenly call the first method
when you intended to use the second. This leads to a ressource leak.

In most modern day programming languages (in my case C#) it is legal to
call a function and disregard the return value.
Is there any attributes I can place on my function that will let the
compiler generate an error (or at least a warning) if you call
the first method and disregard the return value.

you could overload the method and add an out parameter. this way the caller
using the overloaded version of your method would have to be aware of the
"returned" value.

void f() { ... };
void f( out IDisposable returnValue ) { ... };

regards,
Wiktor Zychla
 
P

Peter Strøiman

That is true, but the code where my class is made correctly becomes a little
more complex

Now I can write
using ( MyRessource res = MyClass.Call( ... ) )
{
}

Otherwise I'd have to write
MyRessource res;
MyClass.Call( ..., out res );
try
{

}
finally
{
}
 
P

Peter Strøiman

I accidentally posted too early. Put a "Dispose" call in that cinally block
 
J

John Saunders

Peter Strøiman said:
Hi.

I have a class with two similar methods.
One returns an object implementing IDisposible (and it MUST be disposed)

If it MUST be disposed, then it is up to you to ensure that it gets
disposed. This should not be left up to the caller.

Instead of having callers call your method then maybe disposing your object,
how about requiring them to pass a delegate to your method. Your method
would invoke that delegate within a using statement:

public delegate void IDisposableConsumer(IDisposable mustDispose);

public void ConsumeMustDisposeObject(IDisposableConsumer handler)
{
using (IDispose id = GenerateIDisposableInternal())
{
handler(id);
}
}

protected virtual IDisposable GenerateIDisposableInternal()
{
return new ObjectImplementingIDisposable();
}

This puts the responsibilities where they belong. Your class knows how to
generate the objects which must be disposed, but your callers know what to
do with the objects.

John Saunders
 

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