zoukankan      html  css  js  c++  java
  • gdiplus exception

    <span style="font-size:14px;">#include <windows.h>
    #include <gdiplus.h>
    #include <stdio.h>
    using namespace Gdiplus;
    #pragma comment(lib, "gdiplus.lib")
    
    int main()
    {
    	GdiplusStartupInput gdiplusStartupInput;
    	ULONG_PTR gdiplusToken;
    	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    	printf("%d
    ", gdiplusStartupInput.GdiplusVersion);
    
    	Image image(L"ing.png");
    	printf("The width of the image is %u.
    ", image.GetWidth());
    	printf("The height of the image is %u.
    ", image.GetHeight());
    
    	GdiplusShutdown(gdiplusToken);
    	return 0;
    }
    </span>


    一个使用Gdiplus的Hello World程序,居然都出现了错误。输出居然没有问题=)。

    因为这学期到下学期打算深入学习调试技术,一心想着不丢弃所遇到的任何一个bug。

    F5跟踪一下:


    在Image类的析构函数中出现一个Access violation 错误。

    析构函数一般都是用来释放空间的,为什么会出错呢?

    自认为很重要的一点:在main函数return语句没执行之前调用的Image类的析构函数。

    回想下看MSDN GdiplusStartup、GdiplusShutdown里的一句话:

    Remarks

    You must call GdiplusStartup before you create any GDI+ objects, and you must delete all of your GDI+ objects (or have them go out of scope) before you call GdiplusShutdown.

    也就是说我们在调用GdiplusShutdown以后再去delete某个对象,造成了这个Access violation错误。

    解决办法:

    <span style="font-size:14px;">#include <windows.h>
    #include <gdiplus.h>
    #include <stdio.h>
    using namespace Gdiplus;
    #pragma comment(lib, "gdiplus.lib")
    
    int main()
    {
    	GdiplusStartupInput gdiplusStartupInput;
    	ULONG_PTR gdiplusToken;
    	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    	{
    		Image image(L"ing.png");
    		printf("The width of the image is %u.
    ", image.GetWidth());
    		printf("The height of the image is %u.
    ", image.GetHeight());
    	}
    	GdiplusShutdown(gdiplusToken);
    	return 0;
    }
    </span>
    把gdi+处理函数放在一个{}

    或者

    <span style="font-size:14px;">#include <windows.h>
    #include <gdiplus.h>
    #include <stdio.h>
    using namespace Gdiplus;
    
    INT main()
    {
       GdiplusStartupInput gdiplusStartupInput;
       ULONG_PTR gdiplusToken;
       GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    
       Image *image = new Image(L"ing.png");
       printf("The width of the image is %u.
    ", image->GetWidth());
       printf("The height of the image is %u.
    ", image->GetHeight()); 
    
       delete image;
       GdiplusShutdown(gdiplusToken);
       return 0;
    }
    
    </span>






    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    有感于《CommunityServer 2007 beta》版本的发布
    企业内部办公自动化之---预定系统
    企业办公自动化之三--内部网的部分实现
    推荐一个超人气的类似MSN弹出的控件
    如何创建类型FreeTextBox的编辑器
    输出EXCEL文件的通用函数,很实用
    ASP导出Excel数据的四种方法
    常用的一些工具总结
    ASP2DLL工具
    分層排序的SQL
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/6616364.html
Copyright © 2011-2022 走看看