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
    知识共享,欢迎转载。
  • 相关阅读:
    概述各种事务隔离级别发生的影响
    linux内核的经典书籍
    sso 登录,网页跳转的实现方式
    初探移动网站的架构和设计
    C# PrintDocument 打印
    .Text分页技术(1)分页的存储过程分析
    SQL2008使用CTE递归查询批量插入500万数据
    自己写的Web服务器
    OMCS 语音视频框架
    ESFramework4.x
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/6616364.html
Copyright © 2011-2022 走看看