Attempted to read or write protected memory. exception in Websupergoo ABCPDF7 on dispose
I get randomly (on the live environment its like once every 2 weeks with a very active and hard working windows service, it resizes about 50000+ images per day) AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
The stacktrace is: Stack:
at WebSupergoo.ABCpdf7.Internal.NDoc._Clear(IntPtr inDoc)
at WebSupergoo.ABCpdf7.Internal.NDoc.Clear(IntPtr inDoc)
at WebSupergoo.ABCpdf7.Doc.Clear()
at WebSupergoo.ImageGlue7.Canvas.Dispose(Boolean disposing)
at WebSupergoo.ImageGlue7.Canvas.Dispose()
at XXXXX.Classes.Imaging.Image.Resize(Int32 width, Int32 height, Boolean addTransparent) in <path>\Image.cs:line 149
at XXXXX.Classes.XXXXX.Object.Import.Media.CreateImageScale(String destDir, Int32 width) in <path>\Media.cs:line 272
at XXXXX.Classes.XXXXX.Object.Import.Media.CreateResizedImages(String threadId) in <path>\Media.cs:line 242
at XXXXX.Classes.XXXXX.Object.Import.Threading.ResizeThread.Run(Object o) in <path>\ResizeThread.cs:line 38
Here is the code for the resize method:
public void Resize(int width, int height, bool addTransparent)
{
using (Canvas tempCanvas = new Canvas())
{
DrawOptions options = new DrawOptions();
if (height == 0)
{
if (width <= Width)
{
options.Limit = new Size(width, 0);
}
else
{
double scale = (double)width / (double)Width;
options.Transform.Magnify(scale, scale, 0, 0);
}
}
else if (width == 0)
{
if (height <= Height)
{
options.Limit = new Size(0, height);
}
else
{
开发者_运维技巧 double scale = (double)height / (double)Height;
options.Transform.Magnify(scale, scale, 0, 0);
}
}
else
{
double scaleX = (double)width / (double)Width;
double scaleY = (double)height / (double)Height;
options.Transform.Magnify(scaleX, scaleY, 0, 0);
}
//add transparency if set.
if (addTransparent)
options.Transparency = true;
tempCanvas.DrawImage(CurrentImage, options);
CurrentImage = tempCanvas.ToImage();
} <<<<---- HERE WE GET THE EXCEPTION ON THE DISPOSE
}
Can anyone help me solve why i get this error or if there is anything I can do about it.
Do you perform any non-thread safe operation? It's hard to say from here but the GC may destroy prematurely something that another thread tries to use - that could result in random-looking behaviour. You could transform the whole thing into static methods or use mutexes or other syncronization methods.
Another things I noticed: What is width and height, Width and Height? I don't see why you should care about zero width image more than dropping an exception. I never used NDoc, but it seems strange too that you have it on the stack in production code.
精彩评论