OutOfMemoryException

R

Ryan

Hi All,

I know this topic is getting a bit old now by the number of blog
postings I've been reading and information in google groups but I'm
having bitmap trouble. It's a simple survey application which displays a
picture to a user and allows them to take another one if the status of
the item in question has deteriorated. The code runs fine the first time
then fails on the second run through at the point it displays the picture.

If anybody can give me some clues as to where my code is going wrong I
would applicate it. Everything else works apart from this.

Thank you
Ryan


void DisplayCameraAnswer(){
this.CameraImage = new PictureBox();
/* Removed - Code to position PictureBox */
this.Controls.Add(this.CameraImage);
this.CameraData = new MemoryStream();
this.reportWriter.ReadImageData("Image01", ref this.CameraData);
if (CameraData.Length > 0) {
this.CameraData.Position = 0;
// Exception raised here.
this.CameraImage.Image = new Bitmap(this.CameraData);
}
}

void QuestionCleanup(){
if(CameraData != null){
this.CameraData.Close();
this.CameraData = null;
}
if(CameraImage!= null){
this.Controls.Remove(this.CameraImage);
if(this.CameraImage.Image != null){
this.CameraImage.Image.Dispose();
this.CameraImage.Image = null;
}
this.CameraImage.Dispose();
this.CameraImage = null;
}
}

/* *** reportWriter ReadImageData Function *** */
void ReadImageData(string FieldName, ref MemoryStream ImageData){
ImageData.SetLength(0);
FieldIndex = this.rsImageData.GetOrdinal(FieldName);
if (!this.rsImageData.IsDBNull(FieldIndex)) {
// Get the size for the data in the database
DataLength = (Int32)this.rsImageData.GetBytes(FieldIndex, 0,
null, 0, 0);
// Allocate the byte[] buffer
byte[] DataBuff = new byte[DataLength];
// Read the data from the database
this.rsImageData.GetBytes(FieldIndex, 0, DataBuff, 0, DataLength);
// Write it to the memory stream
ImageData.Write(DataBuff, 0, DataBuff.Length);
}
}
 
P

Peter Foot [MVP]

Your code doesn't appear to dispose the existing bitmap before assigning a
new one. Either call your cleanup method before retrieving the image or add
an additional bit before assigning the new image:-
if(this.CameraImage.Image != null){
this.CameraImage.Image.Dispose();
this.CameraImage.Image = null;
}
this.CameraImage.Image = new Bitmap(this.CameraData);

Peter
 
R

Ryan

Hi Peter,

Thanks for getting back to me. Forgive my poor original post, I didn't
want to put all the code up in my example as it is quite large but it
looks like I missed a key piece of code. There are to procedures like this:

void btnNext_OnClick(object sender, EventArgs e) {
// Save Report Data
// Move to the next question
// Clean up previous answer controls
this.QuestionCleanup();
// Display the new controls
switch(AnswerType){
case SingleLineAnswer:
// Display single line answer
break;
case MemoAnswer:
// Display memo answer
break;
case CameraAnswer:
DisplayCameraAnswer();
break;
}
}

void btnPrevious_OnClick(object sender, EventArgs e) {
// Save Report Data
// Move back one question question
// Clean up previous answer controls
this.QuestionCleanup();
// Display the new controls
switch(AnswerType){
case SingleLineAnswer:
// Display single line answer
break;
case MemoAnswer:
// Display memo answer
break;
case CameraAnswer:
DisplayCameraAnswer();
break;
}
}

Which as you can see from my original post causes the CameraImage.Image
to be disposed and set to NULL. The picture box is also removed from the
form and disposed before the new answer is created. I didn't know if I
was being caught out with the way I'm loading the data i.e. not
disposing of the memory stream properly.

Thanks for your help.
Ryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top