GDI+奇怪的地方

欣赏下面一段程序,你认为能正常运行吗?

#include "stdafx.h"
#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>

using namespace Gdiplus;

int _tmain(int argc, _TCHAR* argv[])
{
	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR gdiplusToken;

	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

	Matrix m;
	m.Translate(2.0, 3.0);

	PointF p(2.0, 3.0);

	m.TransformPoints(&p);

	GdiplusShutdown(gdiplusToken);

	return 0;
}


调试的时候,一直到return都是正常的,就在Matrix析构的时候挂了,百思不得其姐啊。想想算了,还是自己释放吧,干脆就放在GdiplusShutdown前面吧,放在最后也是不行的,这个有点晦涩呀。修改为如下就正常了:


#include "stdafx.h"
#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>

using namespace Gdiplus;

int _tmain(int argc, _TCHAR* argv[])
{
	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR gdiplusToken;

	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

	Matrix *m =  new Matrix();
	m->Translate(2.0, 3.0);

	PointF p(2.0, 3.0);

	m->TransformPoints(&p);

	delete m;

	GdiplusShutdown(gdiplusToken);

	return 0;
}


一开始我使用的是CodeBlocks,它自带GdiPlus。还以为是它的问题呢,到VC2010上 问题依旧。根据大神的指引,微软的确有个说法:

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.
You can call GdiplusStartup on one thread and call GdiplusShutdown on another thread as long as you delete all of your GDI+ objects (or have them go out of scope) before you call GdiplusShutdown.
Do not call GdiplusStartup or GdiplusShutdown in DllMain or in any function that is called by DllMain. If you want to create a DLL that uses GDI+, you should use one of the following techniques to initialize GDI+:
Require your clients to call GdiplusStartup before they call the functions in your DLL and to call GdiplusShutdown when they have finished using your DLL.
Export your own startup function that calls GdiplusStartup and your own shutdown function that calls GdiplusShutdown. Require your clients to call your startup function before they call other functions in your DLL and to call your shutdown function when they have finished using your DLL.
Call GdiplusStartup and GdiplusShutdown in each of your functions that make GDI+ calls.
Warning  For info about how to use dynamic data exchange (DDE) with GDI+, see Special CWinApp Services.


就是说,在GdiplusShutdown务必释放了所有GDI+对象,如果不是指针的话,当然就在GdiplusShutdown它之后了。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欣爸爸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值