尝试读取或写入受保护的内存。 Websupergoo ABCPDF7 中的处置异常

发布于 2024-12-01 04:48:27 字数 2486 浏览 1 评论 0原文

我随机得到(在实时环境中,就像每两周一次,使用非常活跃和努力工作的 Windows 服务,它每天调整大约 50000+ 图像的大小)AccessViolationException:尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

堆栈跟踪是: 堆栈:

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

这是调整大小方法的代码:

    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
    }

任何人都可以帮我解决为什么我会收到此错误,或者我是否可以对此做些什么。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年已逝 2024-12-08 04:48:27

您执行任何非线程安全操作吗?从这里很难说,但是 GC 可能会过早地销毁另一个线程尝试使用的东西 - 这可能会导致看起来随机的行为。您可以将整个事情转换为静态方法或使用互斥体或其他同步方法。

我注意到的另一件事:什么是宽度和高度,宽度和高度?我不明白为什么你应该更关心零宽度图像而不是丢弃异常。我从未使用过 NDoc,但将它放在生产代码的堆栈中似乎也很奇怪。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文