B
Beam_Us_Up_Scotty
Hello all,
I am trying to write a "simple" animation using C#, and I've tried many
things but nothing seems to work for me without leaking memory. Here's
a very simple piece of code that uses a timer to set the image of a
label from an ImageList every 100ms or so. This is what was being used
when I inherited this piece of code, and I thought it was OK, until I
changed the size of the label to do animation on a larger piece of
screen real-estate.
-----------------------------------------
public void Timer_Tick(object sender, EventArgs e)
{
if (isAnimating)
{
pic_index = ++pic_index % 18;
this.label1.ImageIndex = pic_index;
}
else
{
this.timer1.Enabled = false;
}
}
-----------------------------------------
The form has a start and stop button, and the label. The imagelist has
18 images in it. I start it and it runs and leaks memory like a sieve!
The "in memory" footprint will shrink if I minimize the app, but we're
running on an embedded PC and there is NO virtual memory available, so
we are running full screen all the time.
I've tried other approaches such as double buffering, and that leaks
about 4k bytes / second at 10 refreshes per second. Here's the "tick"
method from that attempt:
-----------------------------------------
public void Timer_Tick(object sender, EventArgs e)
{
if (isAnimating)
{
pic_index = ++pic_index % 18;
Graphics AnimG = screen.getGraphics(); // gets an offscreen "copy"
screen.erase();
spriteArray[pic_index].draw(AnimG); // draw to the copy
screen.flip(); // blt
AnimG = null;
}
else
{
this.timer1.Enabled = false;
}
}
-----------------------------------------
The imagelist example acts like it's "copying" the image out of the
image list and storing it into some permanent (semi-permanent) data
structure someplace and keeping a strong reference to it. I don't even
have a theory as to what the double-buffering sample is doing, how it
could be leaking 4k bytes per second, any new objects created in the
tick method are destroyed at the end of it! In that method, I have 18
"sprites" where one gets painted to the buffer each tick and the buffer
is blt-d to the screen on a panel. That's about as simple as you can
make it.
The only thing I can figure is I'm using the timer wrong - somehow.
I've copied samples off the 'net, I don't "start" the timer anymore,
just enable or disable it.
Anybody have any clues as to what I've done wrong? I've done
animations for years in Java, and NEVER seen anything like this. I
"stumbled" on the memory leak issue because we ran our embedded PC out
of memory so fast it (the app not the PC) crashed in about 30s.
I'm frustrated and developing a VERY bad taste for C#.
Thanks in advance.
I am trying to write a "simple" animation using C#, and I've tried many
things but nothing seems to work for me without leaking memory. Here's
a very simple piece of code that uses a timer to set the image of a
label from an ImageList every 100ms or so. This is what was being used
when I inherited this piece of code, and I thought it was OK, until I
changed the size of the label to do animation on a larger piece of
screen real-estate.
-----------------------------------------
public void Timer_Tick(object sender, EventArgs e)
{
if (isAnimating)
{
pic_index = ++pic_index % 18;
this.label1.ImageIndex = pic_index;
}
else
{
this.timer1.Enabled = false;
}
}
-----------------------------------------
The form has a start and stop button, and the label. The imagelist has
18 images in it. I start it and it runs and leaks memory like a sieve!
The "in memory" footprint will shrink if I minimize the app, but we're
running on an embedded PC and there is NO virtual memory available, so
we are running full screen all the time.
I've tried other approaches such as double buffering, and that leaks
about 4k bytes / second at 10 refreshes per second. Here's the "tick"
method from that attempt:
-----------------------------------------
public void Timer_Tick(object sender, EventArgs e)
{
if (isAnimating)
{
pic_index = ++pic_index % 18;
Graphics AnimG = screen.getGraphics(); // gets an offscreen "copy"
screen.erase();
spriteArray[pic_index].draw(AnimG); // draw to the copy
screen.flip(); // blt
AnimG = null;
}
else
{
this.timer1.Enabled = false;
}
}
-----------------------------------------
The imagelist example acts like it's "copying" the image out of the
image list and storing it into some permanent (semi-permanent) data
structure someplace and keeping a strong reference to it. I don't even
have a theory as to what the double-buffering sample is doing, how it
could be leaking 4k bytes per second, any new objects created in the
tick method are destroyed at the end of it! In that method, I have 18
"sprites" where one gets painted to the buffer each tick and the buffer
is blt-d to the screen on a panel. That's about as simple as you can
make it.
The only thing I can figure is I'm using the timer wrong - somehow.
I've copied samples off the 'net, I don't "start" the timer anymore,
just enable or disable it.
Anybody have any clues as to what I've done wrong? I've done
animations for years in Java, and NEVER seen anything like this. I
"stumbled" on the memory leak issue because we ran our embedded PC out
of memory so fast it (the app not the PC) crashed in about 30s.
I'm frustrated and developing a VERY bad taste for C#.
Thanks in advance.