|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Dispose... GCprivate DataSet ReadNCFFile(string NCFFileName) { DataSet dsNCFFile = new DataSet(); try { dsNCFFile.ReadXml(NCFFileName); return dsNCFFile.Copy(); } finally { } } private void CallingMethod() { DataSet ds = ReadNCFFile(strFile); } will the dsNCFFile, get disposed by GC soon if return dsNCFFile.Copy(), rather than dsNCFFile. I am assuming here that in the calling method, the "ds" will receive a copy and hence the reference to the DataSet in ReadNCFFile will be lost, so GC will garbage collect it soon??? , or I am totally way of here? Any ideas?? Vijay vj <vijayba***@yahoo.com> wrote:
Show quote > I have the below method The return value of ReadNCFFile is a reference to the object created in > > private DataSet ReadNCFFile(string NCFFileName) > { > DataSet dsNCFFile = new DataSet(); > > try > { > dsNCFFile.ReadXml(NCFFileName); > return dsNCFFile.Copy(); > } > finally > { > > } > } > > private void CallingMethod() > { > DataSet ds = ReadNCFFile(strFile); > } > > will the dsNCFFile, get disposed by GC soon if return dsNCFFile.Copy(), > rather than dsNCFFile. I am assuming here that in the calling method, the > "ds" will receive a copy and hence the reference to the DataSet in > ReadNCFFile will be lost, so GC will garbage collect it soon??? , or I am > totally way of here? the method. Therefore it won't be garbage collected - you've still got a reference to it in CallingMethod. The variable ds is a copy of the *reference*, not a copy of the *object*. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too "vj" <vijayba***@yahoo.com> wrote in message instance Anews:u%23vPuGCNGHA.2752@TK2MSFTNGP14.phx.gbl... >I have the below method > > private DataSet ReadNCFFile(string NCFFileName) > { > DataSet dsNCFFile = new DataSet(); > returns new instance B. A is available for GC> try > { > dsNCFFile.ReadXml(NCFFileName); > return dsNCFFile.Copy(); Show quote > } Instance A will be available for GC after return but you cannot pin down > finally > { > > } > } > > private void CallingMethod() > { > DataSet ds = ReadNCFFile(strFile); > } > > will the dsNCFFile, get disposed by GC soon if return dsNCFFile.Copy(), > rather than dsNCFFile. "soon". > I am assuming here that in the calling method, the "ds" will receive a ds refers to instance B - the one returned by Copy()> copy and hence the reference to the DataSet in ReadNCFFile will be lost, > so GC will garbage collect it soon??? , or I am totally way of here? B is an exact copy of A which leads one to question why DataSet doesn't implement ICloneable and call it Clone()! Even more confusingly DataSet has a Clone() method which does not clone it contrary to all reasonable expectation! It just copies the schema. IMHO this is a horrible MS error. Ultimately what you are doing is a pointless waste of CPU - just return the original instance I missed that reference part... Thanks Jon
oh gosh.. even Clone does not work... very interesting.... so GC will not garbage collect...really bad miss by MS VJ Show quote "Nick Hounsome" <nh***@nickhounsome.me.uk> wrote in message news:IBEJf.19909$Q22.16575@fe1.news.blueyonder.co.uk... > > "vj" <vijayba***@yahoo.com> wrote in message > news:u%23vPuGCNGHA.2752@TK2MSFTNGP14.phx.gbl... >>I have the below method >> >> private DataSet ReadNCFFile(string NCFFileName) >> { >> DataSet dsNCFFile = new DataSet(); > > instance A > >> >> try >> { >> dsNCFFile.ReadXml(NCFFileName); >> return dsNCFFile.Copy(); > > returns new instance B. A is available for GC > >> } >> finally >> { >> >> } >> } >> >> private void CallingMethod() >> { >> DataSet ds = ReadNCFFile(strFile); >> } >> >> will the dsNCFFile, get disposed by GC soon if return dsNCFFile.Copy(), >> rather than dsNCFFile. > > Instance A will be available for GC after return but you cannot pin down > "soon". > >> I am assuming here that in the calling method, the "ds" will receive a >> copy and hence the reference to the DataSet in ReadNCFFile will be lost, >> so GC will garbage collect it soon??? , or I am totally way of here? > > ds refers to instance B - the one returned by Copy() > > B is an exact copy of A which leads one to question why DataSet doesn't > implement ICloneable and call it Clone()! > > Even more confusingly DataSet has a Clone() method which does not clone it > contrary to all reasonable expectation! It just copies the schema. IMHO > this is a horrible MS error. > > Ultimately what you are doing is a pointless waste of CPU - just return > the original instance > > "vj" <vijayba***@yahoo.com> wrote in message GC WILL collect ALL garbage (i.e. instance A).news:un$e7MkNGHA.1760@TK2MSFTNGP10.phx.gbl... >I missed that reference part... Thanks Jon > > oh gosh.. even Clone does not work... very interesting.... so GC will not > garbage collect...really bad miss by MS You can't just come up with your own personal definition of garbage and then complain that the GC doesn't collect it. Also Clone() DOES work exactly as documented. The problem is not that it doesn't work but that almost everywhere else Clone is the implementation of ICloneable and does what it says there whereas here it is NOT implementing ICloneable and does something different. It is not strictly wrong - it is just very confusingly named. Show quote > > VJ > > > "Nick Hounsome" <nh***@nickhounsome.me.uk> wrote in message > news:IBEJf.19909$Q22.16575@fe1.news.blueyonder.co.uk... >> >> "vj" <vijayba***@yahoo.com> wrote in message >> news:u%23vPuGCNGHA.2752@TK2MSFTNGP14.phx.gbl... >>>I have the below method >>> >>> private DataSet ReadNCFFile(string NCFFileName) >>> { >>> DataSet dsNCFFile = new DataSet(); >> >> instance A >> >>> >>> try >>> { >>> dsNCFFile.ReadXml(NCFFileName); >>> return dsNCFFile.Copy(); >> >> returns new instance B. A is available for GC >> >>> } >>> finally >>> { >>> >>> } >>> } >>> >>> private void CallingMethod() >>> { >>> DataSet ds = ReadNCFFile(strFile); >>> } >>> >>> will the dsNCFFile, get disposed by GC soon if return dsNCFFile.Copy(), >>> rather than dsNCFFile. >> >> Instance A will be available for GC after return but you cannot pin down >> "soon". >> >>> I am assuming here that in the calling method, the "ds" will receive a >>> copy and hence the reference to the DataSet in ReadNCFFile will be lost, >>> so GC will garbage collect it soon??? , or I am totally way of here? >> >> ds refers to instance B - the one returned by Copy() >> >> B is an exact copy of A which leads one to question why DataSet doesn't >> implement ICloneable and call it Clone()! >> >> Even more confusingly DataSet has a Clone() method which does not clone >> it contrary to all reasonable expectation! It just copies the schema. >> IMHO this is a horrible MS error. >> >> Ultimately what you are doing is a pointless waste of CPU - just return >> the original instance >> >> > > |
|||||||||||||||||||||||