|
I have a large pool of business objects all referencing one another in various ways.
In the client application I want to do something like
employee.Delete();
Behind the scenes, I want to remove all references to the object allowing it to be garbage collected. I then want to remove the physical representation of the object from persistance. Unless I have missed a scenario, the references to the object can be deleted in two ways
MyParentObject.EmployeeProp = null; or
MyEmployeeList.Remove(employee);
In order to do this, I have to maintain some kind of internal lists of these relationships.
My questions are --
1) Have I missed any other way that an object can be referenced?
2) Isn't there an easier way? Doesn't the GC already have this list somewhere?
Robert Zurer
robert@zurer.com
|
|
|
|
|
Technically, MyEmployeeList.Remove won't remove a reference to the object - it only removes the reference from a list. If a class references that particular object somewhere else, it still won't be GC'd.
One way you can do this - but you must have proper exception handling in place - is to dipose the object (make sure it implements IDisposable somewhere up the class hierarchy and call Dispose() on it). Components and controls are already disposable, as well as many other classes in the .NET BCL.
If another object tries referencing it then, an ObjectDisposedException will be thrown.
Unless you add a collection into which each referencing class must add itself (or, make your object to be referenced a ContextBoundObject and perform the registration yourself since you can intercept / handle even direct method calls and assignments), there is no way to get access to what the CLR knows about your app through managed code.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Thank you again for your help.
No I can't throw and exception. What I want is to null the reference only, so IDisposable is not an option. I suppose I could catch the ObjectDisposedException exception and null the reference there, but I don't like that idea.
What I was looking for was a way to not have to manage these internal referencing lists. But I guess that is not to be.
Robert Zurer
robert@zurer.com
|
|
|
|
|
Catching it was my whole point - it's either that or manage the list of callers that reference it yourself as I also mentioned. Once again - you can't get access to the reference list through managed code. This pretty much defeats the purpose of the CLR managing the memory so you don't have to.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
bz - You are thinking about it too much. If you keep a list of yet more references to an object that you want to be destroyed it will never disappear.
Think un-C++, think dirty, think c#. Leave it null and let it be. That's all you are contracted to do memory management wise in C#.
I suggest that if you have it as part of a collection, you first remove the reference from the collection, and then nullify whatever else points to it (including the temp variable).
It's a pain, but you don't get much control over the GC, and MS would have you believe it's better that way.
If you've just done a big batch remove process, probably worth while calling GC.Collect.
It often takes two passes of the GC for the physical memory to be actually freed up though (first makes it a weak reference, second destroys).
/**********************************
Paul Evans, Dorset, UK.
Personal Homepage "EnjoySoftware" @
http://www.enjoysoftware.co.uk/
**********************************/
|
|
|
|
|
Thanks Paul,
As the song goes, maybe I think too much.
Anyway I wasn't so much concerned with making the GC work as I was maintaining consistency of data across the application. I just need to have a list of references to an object so that I can null them all. The framework I'm trying to develop needs this reference list for validation and updating objects as well.
|
|
|
|
|
Hello
I am using C# to create an mdi application. i have the parent form which has its own toolbar and file menu, and a child form that has it own file menu and toolbar. When l press a command button to show the child form l want the parent form's file menu and toolbar to be replaced by the childs.
I have found out how to replace the toolbar, just by making it invisible on the parent form. But l cannot not replace the parent's file menu with the childs file menu.
Many help in advance
Steve
|
|
|
|
|
I'm not too sure but there are some Merging options you can control. Perhaps an event fires to??? Good luck
leppie::AllocCPArticle("Zee blog"); Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.
|
|
|
|
|
The parent calls MergeMenu on its menu with the child Form's menu. Read the SDK documentation for Form.MergedMenu and read the current MSDN Online series about a document/view application in .NET by Chris Sells: http://msdn.microsoft.com/library/en-us/dnforms/html/winforms10162003.asp[^] (this is part 2 that discusses menu merging about half-way down the page).
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Has anyone used the IUrlHistory Interfaces in C#? I've got working samples in C++, but cannot find any samples in C#. Yeah... You can probably guess my COM and Interop knowledge is not enough to build a class for myself!
Any samples or further reading (beyond MSDN) would be appreciated!
|
|
|
|
|
Dave Conn wrote:
You can probably guess my COM and Interop knowledge is not enough to build a class for myself!
Good sign to learn then! There are several articles here on CP, including a good one[^] by Nick Parker about it.
You don't actually have to wrap this interface yourself, though. You can use a little trick[^] where you create a typelib out of the urlhist.idl file, then use tlbimp.exe from the .NET SDK to create an interop assembly out of it. You can then either implement the class (if you're hosting the MSHTML control yourself or something) or get an instance to that interface in order to call methods on it. Tip: casting an object to an in interface in .NET is the same as QI'ing for it in COM (.NET does it under the covers).
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Hi all,
I wonder if anyone can help with a problem I am having with an AddIn I am
writing for Visual Studio.NET
I am try to add a submenu to the Edit menu with two items in it.
I would assume this is done by creating a CommandBar of type
vsCommandBarTypeMenu and specifying the Edit menu as the parent, such as the
code below:
CommandBar EditMenu = (CommandBar)applicationObject.CommandBars["Edit"];<br />
.......<br />
.........<br />
CommandBar MySubMenu = (CommandBar)applicationObject.Commands.AddCommandBar("My Sub Menu", vsCommandBarType.vsCommandBarTypeMenu, EditMenu, 0);
However, when I try to add the CommandBar, a COMException is thrown with the
message "Class not registered. Looking for object with CLID {.........}".
What am I doing wrong here?
Regards,
Chris
|
|
|
|
|
|
Chris Morrison wrote:
However, when I try to add the CommandBar, a COMException is thrown with the
message "Class not registered. Looking for object with CLID {.........}".
It sounds as if the CLSID is not being found in the registry, thus it is not able to load the COM Server because it doesn't know it's location. Are you using a third-party component that hasn't been registered? If so, pull up a command prompt, navigate to the directory of your COM .dll and type the following:
regsvr32 "[YourComponent.dll]"
Not entirely sure this is your problem, however here are a few links that might be of some help:
COMException Class[^]
HRESULTs and Exceptions[^]
Nick Parker - DeveloperNotes.com
VB gets under people's skin because one can be ignorant of computer architecture and ASM and still
get a lot done. -Don Box
|
|
|
|
|
Nick Parker wrote:
Chris Morrison wrote:
However, when I try to add the CommandBar, a COMException is thrown with the
message "Class not registered. Looking for object with CLID {.........}".
It sounds as if the CLSID is not being found in the regisry, thus it is not able to load the COM Server because it doesn't know it's location. Are you using a third-party component that hasn't been registered? If so, pull up a command prompt, navigate to the directory of your COM .dll and type the following:
regsvr32 "[YourComponent.dll]"
That did not work and caused an error.
I would have thought that any registrations that were required would be performed when I build and debug the Add-In. Also I have added a floating toolbar to the IDE with no problems, and I can add new items directly to the Edit menu with out error, so I can't see why I am having problems adding a sub menu to it.
This is very strange.
Chris
|
|
|
|
|
Try running regasm.exe on your .NET assembly. It is equivalent to regsvr32.exe but is for managed assemblies. Since such assemblies do not export DllRegisterServer or DllUnregisterServer , regsvr32.exe is useless.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Heath Stewart wrote:
Since such assemblies do not export DllRegisterServer or DllUnregisterServer, regsvr32.exe is useless.
I would have suggested that but since it was throwing a COMException I figured it was due to a problem on the other side of the fence.
Oh well, it's the weekend after all.
-Nick Parker
VB gets under people's skin because one can be ignorant of computer architecture and ASM and still
get a lot done. -Don Box
|
|
|
|
|
VS.NET uses the DTE components via COM (even if written in managed code), so if it can't find the registered class (either because it wasn't registered correctly or because he moved the DLL after registering it) it would throw such a COMException .
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Hi:
I am doing a project ,in which I must use journal-hook
But all the help fille and example-code I saw is about
local-hook,what should I do in C# .
thanks
|
|
|
|
|
Continueing this repost trend wont give you an answer. Wtf is journal hooks anyways? How is it done in C++?
leppie::AllocCPArticle("Zee blog"); Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.
|
|
|
|
|
In C#.NET I wrapped WOpenEnum, WCloseEnum and WEnumResource so that I may go through the network domains, servers, and shares much like one would with the Directories class... (Directories.GetDirectories(string path)... you know...)... anyway to the problem:
In the Console (my dev environment) runs like a champ... no errors, the amount of memory used or threads opened doesn't change... really stable... I incorporate this set of files in a Forms environment though and errors start occuring randomly, more often when I'm querying faster... errors such as infinite recursion, null referance is mscrolib...etc... I really want to say it has something to do with GC, but I'm not an expert... but because the console is garbageless (I think) and Forms are constantly getting rid of things the GC doesn't think you need I'm looking that way... I don't know though. I've tried the KeepAlive method and other things but the problem still comes. I'm really not much on low level wrappers, but like I said, in the Console window it's running great... and I peiced this thing together best I can but am open to suggestions and learning experiance. I just can't get it to work worth a darn otherwise outside the console and it's frustrating the heck out of me. If you can help please email me and I'll send you the code... I don't care if it's public or not... a lot of people I'm sure could use something that would help them get network resources easier... I'm just not sure how to post a file if you even can... I'm new here.
Thanks,
Brian J.
If you ever meet somebody who says they know it all, they are obviously ignorant and probably write terrible code.
|
|
|
|
|
One word: Paragraphs!
I think (without reading your post) the problem is, that you cant write to the Console form another thread.
leppie::AllocCPArticle("Zee blog"); Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.
|
|
|
|
|
I'm with leppie on this one - use paragraphs next time. It's very difficult to follow your problem.
Since I can't really distinguish what you're problem is, let me give you a lesson or two on the .NET Framework. The GC only collects objects to which no references exist. If something is referencing an object in memory, it will NOT be GC'd. As far as your wrapper class for the Network API, all the methods you P/Invoked are static, which means they are methods of the Type - not an instance - so it can't be GC'd. If you get back a handle from one of those methods and aren't storing it (hence, not making a reference to it in your instance), then it will be GC'd.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
I'll try to clarify...
I made a set of classes that make interface with the WOpenEnum, WCloseEnum, and WEnumResources API functions simpiler. In that set of classes I also have a public class called Network that has static methods for retrieving the domains, servers, or shared folders in a network using the three API above (GetDomains(), GetServers(string domain), GetSharedFolders(string server). This set of classes also has three public structs (Domain, Server, SharedFolder) that I use for returning the information gathered in the three functions above. If you've worked with those three API before you know there are 8 items of information that come back, but only two that are important to the user, the 'Remote Name' and the 'Comment', this is what I put in those structs.
Now in a seperate project I make a reference to those set of class (which I just call EnumNetwork.dll)... With just a simple console project I can put it in a loop (normally wouldn't do this, nor use goto) as the following:
Start:
Server [] myServers = Network.GetServers("myDomain");
foreach(Server s in myServers)
{
Console.WriteLine(s.Name); //write the name of the server
}
goto Start:
And with this the servers will scroll out the console window forever without a glitch.
Now in a Windows Form I have a ListView object to make things look what users are use too... assume I already have a computer icon set for the second parameter of Item.Add below (the 0):
Server [] myServers = Network.GetServers("myDomain");
this.lstView.BeginUpdate();
this.lstView.Items.Clear();
foreach(Server s in myServers)
{
this.lstView.Items.Add(s.Name, 0);
}
this.lstView.EndUpdate();
What happens with this is that occasionally when closing the window itself you get 'Null Reference in mscrolib', or 'object not set to a reference of an object in unknown module'... and if running this code more that once in a 10 second period, or if there are many servers you get 'Infinite Recursion' and and 'Unhandled exception in sscrolib'...
Now I don't know what you mean by paragraphs unless you were saying I need to type better... in which case sorry for that... otherwise I'm ignorant to the meaning... but what I'm getting at is calling that method in the console window constantly results in no errors... over hours (yes I tested it for that long)... yet calling it in a method on a form results in unpredicatable errors... and try...catch statements don't do anything because the errors are caused in the Application.Run line... like I said, if you need the source code... not a problem... I'll email... thanks for any help, and thanks for the good info on the GC...
Brian
If you ever meet somebody who says they know it all, they are obviously ignorant and probably write terrible code.
|
|
|
|
|
Structs are not reference types - they are values types. If you think that different classes have reference to the same struct, you'd be wrong. They each have a separate copy of the struct because it is, once again, a value type.
This might have something to do with the ListView you're using, too. Since the ListViewItem is representing something that is getting destroyed by the GC (once structs are out of scope and the GC has a slice of time it cleans them out - including the values in the struct). Instead, when adding the s.Name to the list, use s.Name.Clone().ToString() . A copy of the string will be used instead of the string itself - which will get destroyed by the GC and a string is a reference type. This might be your problem.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|