开发者

how to release object from thid party dll?

开发者 https://www.devze.com 2022-12-26 03:58 出处:网络
i\'m developing a program to convert RTF to html i\'m using the DLLs found here http://www.codeproject.com/KB/recipes/RtfConverter.aspx?fid=1458864&df=90&mpp=25&noise=3&sort=Position&a

i'm developing a program to convert RTF to html i'm using the DLLs found here http://www.codeproject.com/KB/recipes/RtfConverter.aspx?fid=1458864&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=3427424&fr=1#xx0xx

this dll saves a jpg file from html to a specific folder, when i run the program, it cinverts the rtf for the first time and saves the images to the folder perfectly but when i try to convert it again i hace this error "error a generic error occured in GDI+" i think this dll use SaveImage method and to avoid this you must release the Image object you created but i can't modify the DLL, is there is any way to release the object i've created from this dll? this is my code

R开发者_开发百科tfVisualImageAdapter imageAdapter = new RtfVisualImageAdapter(
    @Application.StartupPath + "\\Program Data\\temp\\{0}{1}",
    System.Drawing.Imaging.ImageFormat.Jpeg);
RtfImageConvertSettings imageConvertSettings = 
    new RtfImageConvertSettings(imageAdapter);
RtfImageConverter imageConverter = new RtfImageConverter(imageConvertSettings);

try
{
    IRtfDocument rtfDocument = RtfInterpreterTool.BuildDoc(
        ConversionText, imageConverter);
    RtfHtmlConverter htmlConverter = new RtfHtmlConverter(rtfDocument);
    htmlConverter.Settings.ConvertVisualHyperlinks = true;
    htmlConverter.Settings.UseNonBreakingSpaces = true;
    this.richTextBoxPrintCtrl2.Text = htmlConverter.Convert();
}
catch (Exception exception)
{
    MessageBox.Show(this, "Error " + exception.Message, this.Text, 
        MessageBoxButtons.OK, MessageBoxIcon.Error);
}


The code is sloppy, it doesn't call the Dispose() method on the bitmap after saving it. That keeps a lock on the file, GDI+ uses a memory-mapped file to avoid putting pressure on the paging file. Important because bitmaps can be quite large. Trying to save to the same file again fails because of the lock. GDI+ exception messages are notoriously sloppy as well.

I think the bug is located in Interpreter\Converter\Image\RtfImageConverter.cs, SaveImage() method. The "convertedImage" bitmap doesn't get disposed. Note that the Graphics object in that same method doesn't get disposed either. Fix it by wrapping them with the using statement.

Run this code through FxCop to catch similar mistakes. And ask yourself if you really want to maintain code like this.


If something implements IDisposable, you can call its Dispose() method. Objects are eligible for garbage collection as soon as they go out of scope so you might also try calling GC.Collect() after there are no more references to the object you want "released."


As Max sez. Or better use the using construct. NEVER call GC.Collect unless you are dead sure by doing so you'll free a few GB or RAM!


Since you have the source code you could examine it and figure out where it keeps a reference and make sure it's released.

If you cannot figure out where to do this you could load the code up in a separate AppDomain, and execute your code there. When you are finished you can unload the AppDomain, and your application will release any objects. Then recreate the AppDomain for the next run.

But I would try to spend some time figuring out the real issue before using AppDomains.

And another thing. Do you get the GDI error when you execute the same file twice, or two different files in succession? It could be that it fails to load the image of the second file and gives you the error.

0

精彩评论

暂无评论...
验证码 换一张
取 消