释放不是 GDI+ 图像类的成员
dispose is not a member of GDI+ Image class
所以我有一个程序,可以从文件夹中提取随机图像并从中创建拼贴画,并将其设置为Windows壁纸。 这似乎工作正常。 所以我想我会放一个睡眠计时器,让它自动更新,而不必每半小时运行一次。 我这样做了,效果很好,但是我遇到了内存泄漏的问题,在我开始循环之前没有注意到。 我正在尝试释放GDI+对象,但我不断收到错误,即释放不是GDIplus的成员::图像
我正在将图片加载到 Image 对象中,然后调整其大小并将其放入图像数组中,然后我想处理第一个图像。 然后,我想在处理完数组中的图像后处理该数组。
这是使用VS2005的旧副本完成的。
#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
#include <string>
#include <iostream>
#include <vector>
#include <dirent.h>
#include <time.h>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include "cwp05rnd.h"
using namespace Gdiplus;
using namespace std;
#pragma comment (lib,"Gdiplus.lib")
#pragma comment (lib, "user32.lib")
int main()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
HDC hdc;
Graphics graphics(hdc);
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
CLSID jpegClsid;
GetEncoderClsid(L"image/jpeg", &jpegClsid);
SetCurrentDirectoryA("E:\Photos");
ofstream outfile;
outfile.open ("outimgs.txt");
ofstream outfile2;
outfile2.open("imgpos.txt");
srand(time(NULL));
init_genrand(time(NULL));
vector<string> dirlist;
DIR *d;
struct dirent *dir;
int i=0;
d=opendir(".");
if (d)
{
while ((dir=readdir(d)) != NULL)
{
i++;
dirlist.push_back(dir->d_name);
}
closedir(d);
}
Image wp(L"E:\Dropbox\Photos\wallpaper.jpg");
Graphics* wpimage = Graphics::FromImage(&wp);
int r;
int rvsize=100;
int rv[100]={0};
string img;
std::wstring wimg;
const wchar_t* rimg;
double cwidth;
double cheight;
double ratio;
int nheight;
int counter=0;
int full = 0;
int tries = 0;
int hfull = 0;
int imgnum =0;
int last=0;
Image* newpic[10];
while ( tries <10)
{
redo:
tries++;
int newrv=0;
while (newrv ==0)
{
r=genrand_int32()%i;
for (int k=0; k < rvsize; k++)
{
if (rv[k] > 0 && rv[k]==r )
{
break;
}
if (rv[k]==0 && r < i)
{
newrv =1;
rv[k]=r;
last=k;
break;
}
if (rv[k] ==0)
{
break;
}
}
}
img = dirlist[r];
if (img[0]=='.')
{
newrv=0;
goto redo;
}
wimg = std::wstring(img.begin(),img.end());
rimg = wimg.c_str();
Image pic(rimg);
cwidth = pic.GetWidth();
cheight = pic.GetHeight();
if (cheight ==0)
{
outfile2 << "error" << img << endl;
rv[last]=0;
system("pause");
goto redo;
}
ratio = cwidth/cheight;
nheight = nwidth/ratio;
pic.RotateFlip(Rotate180FlipNone);
pic.RotateFlip(Rotate180FlipNone);
newpic[imgnum] = pic.GetThumbnailImage(nwidth,nheight,NULL,NULL);
delete pic[0];
imgnum = imgnum + 1;
}
然后,根据各种随机值,NewPic中的图像上有一长段翻转和旋转。
wpimage->DrawImage(newpic[k],(j*nwidth),(((k+1)*whitespace)+htot),nwidth,nh[k]);
wp.Save(L"C:\Temp\wallpaper\nwallpaper.jpg", &jpegClsid, NULL);
delete newpic;
setWall();
delete wpimage;
delete wp;
return 0;
}
当我尝试删除 Image 对象时,出现错误,指出它无法删除不是指针的对象,或者无法从 GDIplus::Image 转换为 void*
任何建议将不胜感激。
我注意到你有Image pic(rimg);
但你正在做delete pic[0];
pic
不是指针。不是动态分配什么的...也不是数组(或者也许是......但直觉上我认为不是。
*加*哦,是的,如果您已经解决了这个问题,建议您关闭问题或至少提及它......
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 助记符和指向成员语法的指针
- 用于访问容器<T>数据成员的正确 API
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 嵌套在类中时无法设置成员数据
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 将函数类成员映射到类本身内部
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 释放不是 GDI+ 图像类的成员
- 接收sensor_msgs::将图像作为ros消息中的数据成员