|
Hey everyone,
I'm still chugging away on my little game. Right now you can connect to a server and play across the internet. The 'game' works, but I'm having some serious issues with memory leaks.
I don't know when I should implement the IDisposable interface. Even when I implement it, it doesn't seem to make any difference at all. It seems the source of my problem are arrays and their values.
For example: I had an array of bullets. Whenever one bullet was spent and no longer being used, I would just simply call: bulletArray[i] = new Bullet(); and replace the old version of the object with a new one. This caused a very unwelcome memory leak that took me hours to track down. I thought I could just overwrite the old object but it turns out the system holds on to it indefinitely.
My next problem. I've recently started using ArrayLists. Whenever something was no longer needed, I would .Remove(ObjectNoLongerNeeded). Wouldn't removing the object also set it up for garbage collection? It doesn't seem to. What do I need to do to get rid of the old object and reclaim the memory?
This is my first C# program and the first time using a OO language. It's wonderful to see my program working, but whenever anyone shoots while connected their program's memory requirement permanently increases. Whenever anyone explodes, memory requirement goes up. I know this has to do with my arrays, but damn, there are so many of them. How can I get around this?
If I have a class called MyClass() that implements IDisposable, what do I need in the Dispose function to actually 'dispose' it? How do you dispose integers, floats, and other primatives?
Thanks,
EvilDingo
|
|
|
|
|
|
Interesting problem. You only need to implement IDisposable if you have either unmanaged resources (i.e. window or other object handles from the OS) or scarce managed resources (i.e. database connections). All other managed classes, including arrays, should be collected by GC periodically.
In fact, implementing IDisposable imposes a small performance penalty when it comes to garbage collection. Not that you should never do it, but only do it when necessary.
I have heard of other situations like this. I don't know if it was a problem with the Framework or simply sloppy programming. You may look into using structures instead of classes, if your classes are very simple and contain mostly primitives. Structures can have constructors, properties and methods. They are like light-weight objects, but are created on the stack instead of the heap.
HTH
|
|
|
|
|
Hi
I know this does not answer your question, but I hope you know about the problems with ArrayLists. If you dont know these problems, these are what they are. Once you have set up an array to hold a certain number of items, it works fine. When you start increasing the size dynamically, speed drops bigtime, especially if you have large arrays or lots of them. The way it works, is if it can store 10 ints, and you add another 1, it will make a new array, and copy all the items into it, and hopefully delete the old one.
|
|
|
|
|
Hey,
I have read this before it is possible to create an instance of a type
by specifying their string literal equivalent.
for example if i need to create an instance of Employee class
generally i will say Employee objEmployee = new Employee();
my question is i just know that the class which i need to instantiate is "Employee" how will i instantiate using the string "Employee"
hope i did not made the simple ques more complex.
Cheers,
Venkatraman Kalyanam
Chennai - India
"Being Excellent is not a skill, it is an attitude"
Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)
|
|
|
|
|
Check out AppDomain.CreateInstance and AppDomain.CreateInstanceFrom
Rocky Moore <><
|
|
|
|
|
You can instantiate a class using a "partial" name... i.e. the type name as a string literal, one way is...
<br />
string path = "MyApp.MyAssembly";
string class = "MyClass";
<br />
object MyObj = Assembly.Load(path).CreateInstance(class);<br />
This is most useful when your class implements a known interface but you want to dynamically load the implementation because you can do the following.
<br />
IFooBar fooBar = Assembly.Load(path).CreateInstance(class) as IFooBar<br />
This stuff is my favourite thing about .net...
HTH
Shaun
-----------------------------------------------------------------------
Shaun Austin: .NET Specialist. Spreading the word of .NET to the world... well the UK... well my tiny corner of it!!
|
|
|
|
|
thanks for your info
Cheers,
Venkatraman Kalyanam
Chennai - India
"Being Excellent is not a skill, it is an attitude"
Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)
|
|
|
|
|
Venkat,
Have a look at CreateInstance and CreateInstanceAndUnwrap method, I think you can also do this using other reflection techniques.
Cheers,
Kannan
|
|
|
|
|
thanks for your info
Cheers,
Venkatraman Kalyanam
Chennai - India
"Being Excellent is not a skill, it is an attitude"
Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)
|
|
|
|
|
You could also use Activator.CreateInstance. This is much simpler that using AppDomain.CreateInstance, especially if the "Employee" class is in the same assembly. It also returns an Object instance instead of an ObjectHandle.
|
|
|
|
|
How do i load an image from resource?? I found 2 ways :
1) Using class Bitmap's constructor as shown below
public Bitmap(
Type type,
string resource
); What should i put for the type parameter?
2) Using class Bitmap's FromResource(...) method as show below
public static Bitmap FromResource(
IntPtr hinstance,
string bitmapName
); How do i get the instance of my application?
Weiye, Chen
When pursuing your dreams, don't forget to enjoy your life...
|
|
|
|
|
I have simply used:
new Bitmap(this.GetType().Assembly.GetManifestResourceStream("MyNameSpace.MyBitmap"));
Rocky Moore <><
|
|
|
|
|
Sorry for another question, but Im stumped on this: Im getting the following exception when I close my application:
An unhandled exception of type 'System.ObjectDisposedException' occurred in system.windows.forms.dll
Additional information: Cannot access a disposed object named "MainForm".
MainForm is the name of...my main form. It sounds like my main form is getting disposed for it actually should, but Im not explicitliy disposing it anywhere, and really dont have any idea where to look. Any ideas, or at least where to start?
Thanks
|
|
|
|
|
No problems with the questions... that's what these boards are for after all!!
Is it possible that you are either trying to access members of your form or objects created from it after the
Application.Run(new Form1());
line in your Main() function.
OR... do you have a timer which you have not stopped? It could be firing an event handler after the object which handles the event has been "disposed of"...
Some things to look for at least!
Shaun
-----------------------------------------------------------------------
Shaun Austin: .NET Specialist. Spreading the word of .NET to the world... well the UK... well my tiny corner of it!!
|
|
|
|
|
When a form is closed, it disposes of itself. You may still have a valid reference to the form and it won't be null/nothing, but it is a useless reference: any access to it results in ObjectDisposedException. Make sure you aren't trying to access properties/methods after it has closed.
|
|
|
|
|
Is there an equivalent for VB6's InputBox function in C#?
Nick Seng (the programmer formerly known as Notorious SMC)
God, I pity me! - Phoncible P. Bone
|
|
|
|
|
Its there in Microsoft.VisualBasic namespace, Interaction module.
InputBox[^]
Cheers,
Kannan
|
|
|
|
|
Thanks!
Nick Seng (the programmer formerly known as Notorious SMC)
God, I pity me! - Phoncible P. Bone
|
|
|
|
|
Hello friends,
I am trying to add a copyright symbol to a Label or TextBox within my About dialog. What I did was copying it from MS Word doc and paste it into Label or TextBox controls. It gave me this wierd char "ã", instead of the Copyright symbol.
Thanks so much
Khang Nguyen
|
|
|
|
|
private void button1_Click(object sender, System.EventArgs e)
{
string str="i'm some text " + Convert.ToChar(169);///169 is copyright symbol
MessageBox.Show(str);
string strString="now i'm text with registered " + Convert.ToChar(174);///174 is registered symbol
MessageBox.Show(strString);
}
just convert to char with the numbers and you will get the symbols , or paste them from character map .
switch(twinsOnWay)
{
case ("twins on the way"):
MessageBox.Show("for mr and mrs dynamic","twins on the way");
break;
|
|
|
|
|
Thank you so much for the tip, Dynamic! Have a great day.
Khang
|
|
|
|
|
Hi!
I created an excel C# COM Add-in with Visual Studio (Other Projets\Extensibility Projets\Shared Add-in).
So, I would like to know how to add programmatically my add-in to COM Add-Ins dialog box (Tools\Add-In) of excel.
I thought that the automatic registration, after building my add-in, adds it automatically to COM Add-ins dialog box but it doesn't seem.
|
|
|
|
|
|
Jan Gray of the Microsoft CLR Performance Team wrote an excellent article
titled "Writing Faster Managed Code: Know What Things Cost"
(http://msdn.microsoft.com/library/?url=/library/en-us/dndotnet/html/fastmanagedcode.asp).
This article has motivated me to seek answers to the following questions.
Any replies from the group are appreciated. The questions probably
(hopefully) have very simple answers.
Between these two foreach loops, is there any difference in performance,
safety or correctness? (All examples are in C#)
1. declare and initialize new type in body of loop:
foreach (T t in tList)
{
A a = new A(t.ID);
//do more work
myCollection.Add(a);
}
2. declare new type outside loop, and 'new' it inside loop:
A a = null;
foreach (T t in tList)
{
a = new A(t.ID);
//do more work
myCollection.Add(a);
}
I would think #2 is preferrable, but I see that most C# examples use the
style of #1. In fact, I often see the following in C# examples:
T MyMethod()
{
T t = new T();
return t;
}
This would be a big problem in C++, but in C# the garbage collector makes it
OK. But does that mean it's a good practice? Why is it so common in
examples?
Finally, I'm curious if there are any performance differences between the
following two loops. The reason I ask is that I'm one of those people who
(prior to reading the above mentioned article) pulled the array.Length
property out of the loop header in the interest of avoiding extra calls to
get the length value. I now understand why that is not optimal. I'm curious
if any processor optimizations or compiler optimizations make choice 1 equal
to or better than choice 2 below:
1. repeat same property calls inside a loop:
for (int i = 0; i < 10; ++i)
{
DoAlgorithm1(i, T.x);
DoAlgorithm2(i, T.x + y);
DoAlgorithm3(i, T.x + y);
}
2. pull property calls outside the loop:
double x = T.x;
double w = x + y;
for (int i = 0; i < 10; ++i)
{
DoAlgorithm1(i, x);
DoAlgorithm2(i, w);
DoAlgorithm3(i, w);
}
All input is appreciated!
|
|
|
|