游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1399|回复: 1

DLL中使用GDI+引起Heap错误

[复制链接]

89

主题

822

帖子

847

积分

高级会员

Rank: 4

积分
847
发表于 2006-1-5 11:18:00 | 显示全部楼层 |阅读模式
我在DLL中调用GdiplusStartup引起Heap错误。一开始没注意,但程序老是出现无规律的莫名错误,不过后来发现输出栏只要调用GdiplusStartup就出现Heap错误。

我在MSDN中发现了下面的描述

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.

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 dynamic-link library (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.  

89

主题

822

帖子

847

积分

高级会员

Rank: 4

积分
847
 楼主| 发表于 2006-1-5 12:10:00 | 显示全部楼层

Re:DLL中使用GDI+引起Heap错误

   UINT  num;        // number of image encoders
   UINT  size;       // size, in bytes, of the image encoder array

   ImageCodecInfo* pImageCodecInfo;
   GetImageEncodersSize(&num, &size);
   pImageCodecInfo = (ImageCodecInfo*)(malloc(size));

   GetImageEncoders(num, size, pImageCodecInfo);

看上面的例子,我发现一个很奇怪的事情,微软在获取Encoder信息的时候,不仅返回了数量,就是数组元素的个数,还返回了数组所占的内存大小。

在申请数组的时候不是用
pImageCodecInfo = new ImageCodecInfo[num];
而是用的
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));

而且size并不等于num * sizeof(ImageCodecInfo)

微软给的解释是
Create a buffer large enough to hold the array of ImageCodecInfo

不知道微软搞得什么鬼。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-23 03:47

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表