Page 1 of 1

After a while malloc returns null

Posted: 2010-03-30T01:21:25-07:00
by berathebrain
Is there any reason why would malloc return null and then even MapBlob function returns null.
This all happens in method OpenPixelCache in cache.c on line 3784.

I was wondering about this code:

Code: Select all

  status=AcquireMagickResource(AreaResource,cache_info->length);
  length=number_pixels*(sizeof(PixelPacket)+sizeof(IndexPacket));
  if ((status != MagickFalse) && (length == (MagickSizeType) ((size_t) length)))
    {
      status=AcquireMagickResource(MemoryResource,cache_info->length);
      if (((cache_info->type == UndefinedCache) && (status != MagickFalse)) ||
          (cache_info->type == MemoryCache))
        {
          AcquirePixelCachePixels(cache_info);
          if (cache_info->pixels == (PixelPacket *) NULL)
            cache_info->pixels=source_info.pixels;
          else
            {
              /*
                Create memory pixel cache.
              */
              if (image->debug != MagickFalse)
                {
                  (void) FormatMagickSize(cache_info->length,format);
                  (void) FormatMagickString(message,MaxTextExtent,
                    "open %s (%s memory, %lux%lu %s)",cache_info->filename,
                    cache_info->mapped != MagickFalse ? "anonymous" : "heap",
                    cache_info->columns,cache_info->rows,format);
                  (void) LogMagickEvent(CacheEvent,GetMagickModule(),"%s",
                    message);
                }
              cache_info->storage_class=image->storage_class;
              cache_info->colorspace=image->colorspace;
              cache_info->type=MemoryCache;
              cache_info->indexes=(IndexPacket *) NULL;
              if (cache_info->active_index_channel != MagickFalse)
                cache_info->indexes=(IndexPacket *) (cache_info->pixels+
                  number_pixels);
              if (source_info.storage_class != UndefinedClass)
                {
                  status|=ClonePixelCache(cache_info,&source_info,exception);
                  RelinquishPixelCachePixels(&source_info);
                }
          return(MagickTrue);
	        }
        }
      RelinquishMagickResource(MemoryResource,cache_info->length);
    }
I have a question regarding this piece of code:

Code: Select all

          return(MagickTrue);
	        }
        }
      RelinquishMagickResource(MemoryResource,cache_info->length);
Namely, if malloc returns null then this executes : cache_info->pixels=source_info.pixels; Should we return MagickTrue in this case also?
So, instead of the code above should it be more like this:

Code: Select all

	        }
          return(MagickTrue);
        }
      RelinquishMagickResource(MemoryResource,cache_info->length);
This is pretty important, because if we don't return MagickTrue the file is being cached on disk and I really want to avoid that.
I have 4GB of RAM, my program constantly consumes only 700MB of memory so I don't understand why would malloc return NULL. I have WindowsXP SP3 with latest updates.
One last thing, I have found a bunch of memory leaks using Trial version of Insure++ when doing resizing of an image. I can not give you the whole source code of this program but I will try and make some small example where this happens.
Thank you for your time and effort.

Cheers.

Re: After a while malloc returns null

Posted: 2010-03-30T02:53:22-07:00
by berathebrain
Here is the program that produces memory leaks:
viewtopic.php?f=3&t=15897

Re: After a while malloc returns null

Posted: 2010-03-30T12:00:21-07:00
by magick
32-bit Windows has limits on how much memory it will give a process. You most likely will have better luck with 64-bit Windows or even better, Linux.

The pixel cache is designed to make every attempt to allocate a pixel cache so in the event that memory is not available it will push the pixels to disk. If you do not want anything written to disk you can set the disk resource limit and when the limit is exceeded an exception is thrown "cache resources exhausted."

See http://www.imagemagick.org/script/resources.php and http://www.imagemagick.org/script/archi ... .php#cache.