|
I don't think this is the problem, but just in case:
If you check the value on step by step debug the value will always be false, since the window is hidden by visual studio.
|
|
|
|
|
Hi Guys,
I had windows service that will listen to a COM port and somehow after run for hours, memory leak start happening.
So I created 2 simple programs.
First one, send a data through COM2 (Console):
private static byte[] serialBuffer = new byte[1];
static SerialPort sPort = new SerialPort();
static void Main(string[] args)
{
sPort.PortName = "COM2";
sPort.Open();
string data= String.Empty;
int startNumber = 100000;
while (startNumber < 1)
{
data = DateTime.Now.ToString("dd MM yy hh mm ss");
data = data + " " + data + " " + data;
byte[] tmp = HexStringToByteArray(data);
sPort.Write(tmp, 0, tmp.Length);
Thread.Sleep(500);
startNumber--;
}
}
private static byte[] HexStringToByteArray(string s)
{
s = s.Replace(" ", "");
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);
return buffer;
}
Next one is the receiving part and write back to COM1 port (Windows form):
SerialPort _serialPort = new SerialPort();
public Form1()
{
InitializeComponent();
_serialPort.PortName = "COM1";
_serialPort.Open();
_serialPort.DataReceived += new SerialDataReceivedEventHandler(tccuSerialPort_DataReceived);
}
byte[] serialBuffer = new byte[1];
private void tccuSerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int bytesToRead = _serialPort.BytesToRead;
if (bytesToRead > 256)
bytesToRead = 256;
if (bytesToRead > 0)
{
serialBuffer = new byte[bytesToRead];
_serialPort.Read(serialBuffer, 0, bytesToRead);
listBox1.Items.Add("RECEIVED");
_serialPort.Write(new byte[1] { 0x01 }, 0, 1);
}
serialBuffer = null;
}
When I run the programs, the 2nd program's memory start to leak. (receiving program)
I have no idea if anybody encounter this problem before? can provide some advice?
Thanks in advance.
|
|
|
|
|
fukchai2000 wrote: int startNumber = 100000;
while (startNumber < 1)
Don't know if this was a transcription typo but the loop in the first program won't do anything
As for the second program, this:
fukchai2000 wrote: listBox1.Items.Add("RECEIVED");
Looks like it just keeps adding new items that say "Received" into the listbox, without ever clearing it. Depending on how often it's receiving data, the ram taken up by listBox1 could be quite a lot after a while. Unless it's cleared somewhere that isn't shown here.
He said, "Boy I'm just old and lonely,
But thank you for your concern,
Here's wishing you a Happy New Year."
I wished him one back in return.
|
|
|
|
|
Hi,
1.
I have used SerialPort several times and never had any problem with it.
2.
you failed to explain how you come to the conclusion there would be a memory leak.
I have quite a few comments to your code; I'll just give you two of them:
3.
it would be better to make the test messages predictable, e.g. by including an autoincrementing number; that way you can identify the messages upon receipt and verify none are missing.
4.
in DataReceived, you only read a maximum of 256 bytes of data, no matter how much data is actually present. So it is very well possible your reading is lagging more and more as more data is coming in.
|
|
|
|
|
I have almost nothing to add to the previous 2 posts but, I don't think it's a memory leak.
Remember, the GC runs on a low priority thread and it won't kick in unless the system and/or app
starts running low on memory.
Ex: On my home desktop with 8GB of RAM a little image viewer/organizer and other stuff I created for fun
starts at approx 20MB~. I thought I had some sort of memory leak or some sort of circular reference or something because it grows up to 800+ MB of memory used(if used a lot). Then all of a sudden drops right back at approx 30-40 MB.
Bottom line, maybe the GC doesn't kick in because there are plenty of resources available, and so you
might be tempted to think it's a memory leak.
Just a thought.
I bug
|
|
|
|
|
Hi,
I want to pass the ArrayList to a function which is having the VBA.Collection as paramater in some dll.How can it be done.
|
|
|
|
|
I think you can use the MSVBVM60.dll to create the VBA collection (COM interop). Since I have never done it myself, cannot think of a decent solution.
|
|
|
|
|
kjsl2k9 wrote: I want to pass the ArrayList to a function which is having the VBA.Collection as paramater in some dll.How can it be done.
Instantiate the class on the VB6-side and pass it to .NET. The KB[^] holds the details on how to do so.
I are Troll
|
|
|
|
|
Hi,
Im my webbrowser application in the url field if i type google and press ctrl+enter .com should come automatically how to enable this feature.
Thankyou,
krishna
|
|
|
|
|
You need to be more descriptive.
Do you want normal enter press to behave like Ctrl + enter? You will need to write an add-in for the browsers for that AFAIK.
|
|
|
|
|
huh? I think he wants "{CTRL/ENTER}" to behave like ".com{ENTER}"
|
|
|
|
|
Just do what the OP told: type something into the address field of Internet Explorer and hit CTRL Enter. Now the address gets changed to www.something.com. That's a feature of Internet Explorer.
For doing that in his own web browser application, he will have to subscribe to some key events (e.g. key down or key pressed) of the address textbox, and handle the event arguments accordingly.
|
|
|
|
|
How to load(& add,update ,delete,save) a sql database through class . please help me......
|
|
|
|
|
Lots of tutorials & samples right here[^].
It's time for a new signature.
|
|
|
|
|
Whoo hoo! Mine are on there!
Actually I've working on a new one for the last few weeks.
|
|
|
|
|
PIEBALDconsult wrote: a new one
Thanks for the heads up, I look forward to reading it.
It's time for a new signature.
|
|
|
|
|
See here[^].
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
LINQ. NHibernate. Entity Framework. ORM. CodeSmith. Custom code using update/insert/delete/select.... You choose.
|
|
|
|
|
Class is my middle name. I wouldn't know how to do without.
Have you bothered reading something about OO languages (such as C#) and databases (such as SQL Server)?
Every book I've seen on C# has a couple of chapters on database access.
And are you aware there are thousands of articles here at CodeProject, lots of them dealing with databases; and dozens of them probably fit your current knowledge level?
|
|
|
|
|
Luc Pattyn wrote: Every book I've seen on C# has a couple of chapters on database access
... mostly how not to do it.
|
|
|
|
|
But probably good enough to get started, then feel the weaknesses and finally understand the improvements when you see them somewhere, such as on CP.
|
|
|
|
|
Hello everyone,
I am a freshman in C# and stuck with a stupid problem.
class ProjectDoc
{
private System.Windows.Forms.Panel panelProjectDoc;
this.panelProjectDoc = new System.Windows.Forms.Panel();
this.Controls.Add(this.panelProjectDoc);
public Point myfunc(Point dropPoint)
{
int X_snap = 0;
int Y_snap = 0;
return new Point(X_snap, Y_snap);
}
private void Filter_Click(object sender, EventArgs e)
{
BuildBlock buildBlock = new BuildBlock();
this.panelProjectDoc.Controls.Add(buildBlock);
buildBlock.Location = myfunc(new Point(900, 400));
}
}
class BuildBlock : UserControl
{
private void BuildBlock_MouseUp(object sender, MouseEventArgs e)
{
Point p = new Point(0, 0);
Point q = new Point(0, 0);
p = this.Location;
q = (Parent as ProjectDoc).myfunc(p);
this.Location = q;
}
}
Above code gives run time error when mouseUp event occurs.
[ System.NullReferenceException ]
However, if i replace the line :
this.panelProjectDoc.Controls.Add(buildBlock);
with
this.Controls.Add(buildBlock);
it does not give error (however, this way is not what i want )
any idea how to eliminate the error ?
- me
|
|
|
|
|
When it says "Null reference error" it means that you are trying to refer to an object that has not been set. In this case (and assuming there is no code you missed) it would be the Parent property. Since you have used (Parent as ProjectDoc) I can only assume that the parent is not a ProjectDoc derived object.
Firstly, whenever you use as make a habit of checking the result:
MyClass mc = obj as MyClass;
if (mc != null)
{
...
} It will save you a lot of grief later on!
Secondly, put a breakpoint on the offending line, and see what the Parent property actual is. This should tell you what you need to do.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
Thanks for the reply "OriginalGriff".
I knew there was something wrong, and checking for null will also tell the same.
same will the try .. catch .
However, i was looking for the solution to access myfunc in another class.
The reply by "bhiller" solved the problem.
Thanks anyway, I will keep in mind to check for null
|
|
|
|
|
Let us analyze the hierarchy of the elements:
You start with a form (ProjectDoc ). You add a UserControl panelProjectDoc to its control, thus ProjectDoc becomes Parent of panelProjectDoc . Then you add a UserControl buildBlock to panelProjectDoc , thus panelProjectDoc becomes Parent of buildBlock . Consequently, ProjectDoc is the grand parent of buildBlock . You would need the Parent of the Parent at the position where you get the run time error.
|
|
|
|