Link to home
Start Free TrialLog in
Avatar of steve-west

asked on

EOufOfResources within a thread

I'm creating a thread.

As part of it's execution, it needs to perform an image file conversion - PNG to BMP.
The following code is called within a synchronised method:

function PNGToBMP(Source : String; Destination : String = '') : boolean;

  Bitmap: TBitmap;
  PNG: TPNGObject;
  DestinationJ : string;
  PNG := TPNGObject.Create;
  Bitmap := TBitmap.Create;
  Result := false;
  {In case something goes wrong, free both PNG and Bitmap}
      Bitmap.Assign(PNG);    //Convert data into bitmap
      if Destination = '' then
        Destination := PathRemoveExtension(Source) + '.bmp';

      Result := true;
      Result := false;  

// ---------------------------------------

I'm getting random E:OutOfResources exceptions created and trapped within the except block of this function. One image may be converted successfully, but later on it may not. The SAME image.

Can someone explain to me the circumstances around the EOutOfResources., I'm beginning to think it's a trap-all exception.

Thank you

Avatar of SteveBay
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Normally I would say You've got image in wrong format.
But not in this case.
Are calling it repeatedly?
Avatar of steve-west


Thanks guys, for both of these responses.


Yes, I have read that topic. I couldn't see how that explained my situation - especially the randomness of it. Sometimes the function is only getting called once per thread - it may work, at other times the exception occurs.

Normally I would have put this down to poor synchronisation - but the function is being called from a synchronised method and therefore running in the main thread. There are only perhaps as few as 10 threads running concurrently.


The images are in the correct format. Sometimes the image is converted correctly, at other times the exception occurs -  as mentioned, for the same PNG image.

Windows has a lot of handles to allocate. This is a simple application that could in no way consume all of these handle, so that's why I don't think this is an accurately reported exception.
I've asked if You call this procedure once or repeatedly?
Maybe You should put more try except blocks to detect exact place of exception.
What is in fact TPNGObject? maybe it's not thread safe?
Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The function could possibly be called once or 1,000 times per thread.

<< guess also that TPNGObject is not threadsafe

But the function is called from a synchronized method, so the object's threadsafe properties shouldn't make any difference.

TPNGObject is a popular component used to handle PNG images. ( )
well, maybe, it just sounds like lost unreleased handles are causing this problem

Thanks for responding, sorry I haven't got back to you sooner.

A Resource leak is what I first expected, but this (I'm fairly sure about) is not what is happening. It'll perhaps be best if explain what the application does.

I've developed a CRM application, part of which generates emails and letters, each to be sent out to the customer.

This application is responsible for processing "queued" correspondence items. Correspondence items are letters or email generated by call centre operators. Each item is placed into a correspondence queue for processing/authorisation etc - you don't want Kev in  Customer Services emailing his mate about last night's episode of Shameless.

Any, this application sits there.The application opens each item - be it a letter or email - and processes it accordingly. All this is done within a thread - I create 10 threads to process the processing queue. Each one either prints a letter or sends an email.

It's the emailing part where the issue lies. The email process insists that all images are in BMP format - the original emails are authored in RTF so any embedded images have to be converted as appropriate.

 In testing, the conversion of the first, say for example, 1000 images complete successfully. When I run the application after this and feeding another 1000 emails into it, the application keels over after about 10. It's random, therefore cannot be an OutofResources memory leak.
well, then i run out of ideas . . .

do it run properly, if you don't use threading?

meikl ;-)
Unfortunately, it's very difficult to simulate the error/conditions outside of the application. The function mentioned above is used elsewhere in the system without error.