473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

lock and object passing by ref

Hi,

I get the following warning:
"Possibly incorrect assignment to local 'oLockObject' which is the argument
to a using or lock statement. The Dispose call or unlocking will happen on
the original value of the local."

My code is:
using System;
using System.Collections.Generic;
using System.Text;

namespace RefLockExample
{
class Program
{

protected void MyMethod(ref Object oObject)
{
//something here
//oObject will be used, but no "new" object will be assigned to
oObject
}

static void Main(string[] args)
{
Program oProgram= new Program();
Object oLockObject;
oLockObject = new Object();
lock (oLockObject)
{
oProgram.MyMethod(ref oLockObject);

}

}
}
}

Why does this warning occur?
If I understand it right, it should not make much of a difference to leave
out the "ref".
What does actually get passed here?
I understood that object passing in .NET is passing the reference anyways
and not the value.
So, do I pass the reference on an object reference or do I pass a reference
on the object?

in the called method of course I will not assign anything "new" to the
object, but a problem with assigning anything new would be the same if I
would not have used "ref" also.
What can happen if I use this construct (example) instead of leaving out the
"ref"?

Regards,

Nov 28 '05 #1
6 5953
MSDNAndi wrote:
I get the following warning:
"Possibly incorrect assignment to local 'oLockObject' which is the argument
to a using or lock statement. The Dispose call or unlocking will happen on
the original value of the local."
Yup.

<snip>
Why does this warning occur?
Because the value of oLockObject may change within the lock.
If I understand it right, it should not make much of a difference to leave
out the "ref".
You don't understand it right, I'm afraid.
What does actually get passed here?
The variable is being passed by reference.
I understood that object passing in .NET is passing the reference anyways
and not the value.
The value of that variable *is* a reference. By default, parameter
passing is always done by value - but that value is often a reference.
This is *not* the same as passing the parameter *by* reference.
So, do I pass the reference on an object reference or do I pass a reference
on the object?


You're passing the variable oLockObject by reference.

I suggest you read http://www.pobox.com/~skeet/csharp/parameters.html

Jon

Nov 28 '05 #2
Jon,

thanks for your reply.

I think we mean similar things, but I just expressed myself badly.
I understand what value types, immutable types and "true" (mutable)
reference types are.
The problem of my posting was maybe that I did not point out clearly that I
wanted to talk about mutable reference types only where I will not change the
actual object assignment but only modify the object itself.
So with
"I understood that object passing in .NET is passing the reference anyways
and not the value.
....
So, do I pass the reference on an object reference or do I pass a reference
on the object?"
you replied
"
The value of that variable *is* a reference. By default, parameter
passing is always done by value - but that value is often a reference.
This is *not* the same as passing the parameter *by* reference.
....
You're passing the variable oLockObject by reference.
"
Thus in my (inaccurate) way of asking/expressing it you are saying (in this
specific case of a mutable reference type) that I pass an object reference
by reference.
(I know, "by reference" is different from passing the "reference" since the
object I am working on in the called component is not the passed reference,
but the "by reference" object).

Where I am unsure is what the compiler would do if I pass a mutable
reference type by reference... that maybe the compiler would make no
difference between
ref SomeMutableReferenceType
and
SomeMutableReferenceType.
It would not surprise me if the compiler would "optimize" ref
SomeMutableReferenceType to SomeMutableReferenceType.

In your excellent article you e.g. write about passing a value type by
reference and tell the difference between passing a struct IntHolder and a
class IntHolder, but I do not see an example for the difference of passing a
(mutable) reference type by reference.
Thanks anyways,
Nov 28 '05 #3
MSDNAndi wrote:

<snip>
Where I am unsure is what the compiler would do if I pass a mutable
reference type by reference... that maybe the compiler would make no
difference between
ref SomeMutableReferenceType
and
SomeMutableReferenceType.
It would not surprise me if the compiler would "optimize" ref
SomeMutableReferenceType to SomeMutableReferenceType.
It can't do that - the semantics are completely different.
In your excellent article you e.g. write about passing a value type by
reference and tell the difference between passing a struct IntHolder and a
class IntHolder, but I do not see an example for the difference of passing a
(mutable) reference type by reference.


I give an example where a StringBuilder parameter is passed by
reference. The point is that when a parameter is passed by reference,
the value of the variable in the actual parameter can change, which it
can't when the parameter is passed by value. So:

public void MakeMeNull (StringBuilder sb)
{
sb = null;
}

....

StringBuilder x = new StringBuilder();
MakeMeNull(x);

does nothing, but:

public void MakeMeNull2 (ref StringBuilder sb)
{
sb = null;
}

StringBuilder x = new StringBuilder();
MakeMeNull2(ref x);

leaves x with a value of null.

Jon

Nov 28 '05 #4
MSDNAndi wrote:
Hi,

I get the following warning:
"Possibly incorrect assignment to local 'oLockObject' which is the argument
to a using or lock statement. The Dispose call or unlocking will happen on
the original value of the local."

My code is:
using System;
using System.Collections.Generic;
using System.Text;

namespace RefLockExample
{
class Program
{

protected void MyMethod(ref Object oObject)
{
//something here
//oObject will be used, but no "new" object will be assigned to
oObject
}


Simple example:

protected void MyMethod(ref Object oObject)
{
oObject = new Object();
}

this means that you're passing back a new object, so the following code:

lock (oLockObject)
{
MyMethod(ref oLockObject);
}

could mean that it locks one object, changes it through the method call,
and thus tries to unlock the new object. This is what the warning is
about, that this isn't going to happen, that it's the original object
(which is now replaced with a new one) that is going to be unlocked.

Of course, as you say, you don't replace the object, but the compiler
doesn't know that.
--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 28 '05 #5
Hi Jon,

yes, but I was not talking about assigning a newly created object or a
"null" value to the object, but only about the case where I modify the
content of the object.

class IntHolder
{
public int intValue;
IntHolder()
{}
}
Difference between
protected void ModifyIntholderByRef(ref IntHolder myIntHolder)
{
myIntHolder.intValue=1;
}

protected void ModifyIntholder(IntHolder myIntHolder)
{
myIntHolder.intValue=2;
}

I am not assigning anything to myIntHolder itself in the called methods.

IntHolder localIntHolder =new IntHolder()
localIntHolder.intValue=0;

with the calls:
ModifyIntHolderByRef(ref localIntHolder);

or:
ModifyIntHolder(localIntHolder);
What would be the difference in this example (again: no assignments to
myIntHolder in the methods, and no "structs")?

Thanks for you patience,
Nov 28 '05 #6
MSDNAndi wrote:
yes, but I was not talking about assigning a newly created object or a
"null" value to the object, but only about the case where I modify the
content of the object.
In that case, you shouldn't pass the parameter by reference - and the
compiler doesn't know that that will be the case.
with the calls:
ModifyIntHolderByRef(ref localIntHolder);

or:
ModifyIntHolder(localIntHolder);
The compiler doesn't know that ModifyIntHolderByRef doesn't really
change the value of the parameter - it's been told to pass something by
reference, so it's going to pass it by reference. Don't forget - other
things could get involved, such as the debugger - to maintain the
correct semantics, if localIntHolder is changed in the debugger at
runtime, that change must get propagated to the call site.
What would be the difference in this example (again: no assignments to
myIntHolder in the methods, and no "structs")?


The difference is in the method signature, which tells the runtime what
to do with the parameters.

If you have no intention of changing the value of a parameter, you
shouldn't declare the parameter as "ref".

Jon

Nov 28 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce Pullen | last post by:
DB2 v7.2 (FP7 - DB2 v7.1.0.68) on AIX 5.2.0.0. We're seeing unexpected single row (then commit) insert locking behaviour. We're seeing Applications that already hold row-level W locks in...
7
by: Sunny | last post by:
Hi, I can not understend completely the lock statement. Actally what is locked: 1. the part of the code between {...} or 2. the object in lock() In the docs is written: for 1: The lock...
0
by: Nashat Wanly | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaskdr/html/askgui06032003.asp Don't Lock Type Objects! Why Lock(typeof(ClassName)) or SyncLock GetType(ClassName) Is Bad Rico...
14
by: Sharon | last post by:
Hi all. I have an ArrayList and sometimes while enumerating through, i get an exception because another thread has added to the ArrayList. To solve this problem, i lock the enumeration, passing...
2
by: Fernando Rodríguez | last post by:
Hi, I've been reading the multithreading tutorial at http://www.yoda.arachsys.com/csharp/threads/locking.shtml (thanks Jon :-) and there's one thing I don't understand. Why do have to pass a...
8
by: nytimescnn | last post by:
I've read some discuession about lock() for thread-safe. I am wondering what will be the differce between below two code segment? Code 1: class A { private static Object padlock = new...
2
by: shenanwei | last post by:
DB2 V8.2 on AIX, type II index is created. I see this from deadlock event monitor. 5) Deadlocked Connection ... Participant no.: 2 Lock wait start time: 09/18/2006 23:04:09.911774 .........
20
by: Kurt | last post by:
Below is a class that can accessed from multiple threads and I want the class to be thread safe. I have a private timer member whose interval can be changed by different threads. Which is the correct...
8
by: not_a_commie | last post by:
I'd like to be able to overload the lock command so that I could log an entry on lock and unlock. Any ideas on how to do this? I think it would be powerful if you could inherit from a few...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.