• 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
    知识共享,欢迎转载。
  • 相关阅读:
    什么是模板方法模式?
    在多线程环境下,SimpleDateFormat 是线程安全的吗?
    抽象类是什么?它与接口有什么区别?你为什么要使用过 抽象类?
    依赖注入和工程模式之间有什么不同?
    什么时候使用访问者模式?
    Java 中,受检查异常 和 不受检查异常的区别?
    说出 5 个 JDK 1.8 引入的新特性?
    什么是领域驱动设计?
    列举 IoC 的一些好处?
    什么是 Spring 配置文件?
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/6616364.html
Copyright © 2020-2023  润新知