vector导致内存不断增长

火星大能猫 2013-03-05 05:09:46
C++新手,
线程循环判断打印机队列信息
10毫秒是方便测试使用.
结果发现内存不断上升,何解?

{
//文档名
CString documentName;
DWORD dStatus;
//序号
int dindex;
};
//获取打印队列详细信息 返回vector
vector<CPrintDealSrv::PRINTER_JOB_INFO>* CPrintDealSrv::GetPrintJobList(void)
{
vector<PRINTER_JOB_INFO>* vlist = new vector<PRINTER_JOB_INFO>;

CString strPrinterName = _T("Microsoft XPS Document Writer");
HANDLE hPrintHandle;
LPTSTR lpPrinerName = strPrinterName.GetBuffer();
strPrinterName.ReleaseBuffer();


JOB_INFO_2 *pJobs;
int cJobs;
DWORD dwPrinterStatus;

PRINTER_JOB_INFO jobSimpleInfo;
jobSimpleInfo.documentName = pJobs[i].pDocument;
jobSimpleInfo.dindex = (i+1);
jobSimpleInfo.dStatus = pJobs[i].Status;
vlist->push_back(jobSimpleInfo);


return vlist;
}
//线程方法
UINT CPrintDealSrv::thStartPrintSrv(LPVOID lparm)
{

CPrintDealSrv * pPrintDeal = (CPrintDealSrv*)lparm;
vector<PRINTER_JOB_INFO>* vlist;
while(true)
{

vlist = pPrintDeal->GetPrintJobList();
Sleep(10);
delete vlist;
}
}
...全文
374 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Thinco 2013-03-06
  • 打赏
  • 举报
回复

    //获取成功
    else{
        if (cJobs > 0)
        {
            //队列中有待打印文档
            for (int i = 0;i < cJobs; i ++)
            {
                PRINTER_JOB_INFO jobSimpleInfo;
                jobSimpleInfo.documentName = pJobs[i].pDocument;
                jobSimpleInfo.dindex  = (i+1);
                jobSimpleInfo.dStatus = pJobs[i].Status;
                vlistPrintJobInfo->push_back(jobSimpleInfo);
            }
        }
        //释放内存
        free pJobs;
    }
Thinco 2013-03-06
  • 打赏
  • 举报
回复
引用 11 楼 Thinco 的回复:
GetPrintJobs里面2次malloc,只有1次free
传递出去之后,在delete vector时候没有释放吧
Thinco 2013-03-06
  • 打赏
  • 举报
回复
GetPrintJobs里面2次malloc,只有1次free
fangchao918628 2013-03-06
  • 打赏
  • 举报
回复
成功后没看见你释放 pJobStorage = (JOB_INFO_2 *)malloc(cByteNeeded);这个申请的内存
火星大能猫 2013-03-06
  • 打赏
  • 举报
回复


//获取打印队列详细信息	
vector<CPrintDealSrv::PRINTER_JOB_INFO>* CPrintDealSrv::GetPrintJobList(void)
{
	vlistPrintJobInfo = new vector<PRINTER_JOB_INFO>;
	//打印机名称
	CString strPrinterName = _T("Microsoft XPS Document Writer");
	HANDLE hPrintHandle;
	LPTSTR lpPrinerName = strPrinterName.GetBuffer();
	strPrinterName.ReleaseBuffer();
	
	if(OpenPrinter(lpPrinerName,&hPrintHandle,NULL))
	{
		unsigned char buf[8192];
		DWORD dwSize;
		if(GetPrinter(hPrintHandle, 2, buf, sizeof(buf),&dwSize))
		{
			PRINTER_INFO_2* pInfo;
			pInfo = (PRINTER_INFO_2*)buf;
			if(pInfo->Status == PRINTER_STATUS_PAUSED)//暂停
				AfxMessageBox(L"##打印机暂停");
			else if(pInfo->Status == PRINTER_STATUS_PENDING_DELETION)//等待删除
				AfxMessageBox(L"##打印机删除中");
			if(pInfo->Attributes& PRINTER_ATTRIBUTE_WORK_OFFLINE)
				AfxMessageBox(L"##打印机离线!");
			else
				;//在线
		}

	}
	JOB_INFO_2 *pJobs;
	int cJobs;
	DWORD dwPrinterStatus;
	if(!GetPrintJobs(hPrintHandle,&pJobs,&cJobs,&dwPrinterStatus))
	{
		ClosePrinter(hPrintHandle);
		return vlistPrintJobInfo;
	}
	//获取成功
	else{
		if (cJobs > 0)
		{
			//队列中有待打印文档
			for (int i = 0;i < cJobs; i ++)
			{
				PRINTER_JOB_INFO jobSimpleInfo;
				jobSimpleInfo.documentName = pJobs[i].pDocument;
				jobSimpleInfo.dindex  = (i+1);
				jobSimpleInfo.dStatus = pJobs[i].Status;
				vlistPrintJobInfo->push_back(jobSimpleInfo);
			}
		}
	}

	return vlistPrintJobInfo;
}


// 获取系统打印信息
BOOL CPrintDealSrv::GetPrintJobs(HANDLE hPrinter, JOB_INFO_2 ** ppJobInfo, int* jobCount, DWORD * pStatus)
{
	DWORD cByteNeeded,
		nReturned,
		cByteUsed;
	JOB_INFO_2 *pJobStorage = NULL;
	PRINTER_INFO_2 *pPrinterInfo = NULL;

	//获取缓冲区大小
	if(!GetPrinter(hPrinter, 2, NULL, 0, &cByteNeeded))
	{
		if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)//缓冲区太小
		{
			return FALSE;
		}
	}

	//分配所需的内存
	pPrinterInfo = (PRINTER_INFO_2 *)malloc(cByteNeeded);
	if(!pPrinterInfo)
	{
		return FALSE; //分配内存失败!
	}

	//获取打印机信息
	if(!GetPrinter(hPrinter, 2, (LPBYTE)pPrinterInfo, cByteNeeded, &cByteUsed))
	{
		free(pPrinterInfo);
		pPrinterInfo = NULL;
		ASSERT(FALSE);//没有权限连接打印机
		return FALSE;
	}

	//获取打印工作队列的存储空间
	if (!EnumJobs(hPrinter, 0, pPrinterInfo->cJobs, 2, NULL, 0,
		(LPDWORD)&cByteNeeded, (LPDWORD)&nReturned))
	{
		if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)//缓冲区太小
		{
			free(pPrinterInfo);
			pPrinterInfo = NULL;
			return FALSE;
		}
	}

	pJobStorage = (JOB_INFO_2 *)malloc(cByteNeeded);
	if (!pJobStorage)//分配打印工作空间失败
	{
		free(pPrinterInfo);
		pPrinterInfo = NULL;
		return FALSE;
	}

	ZeroMemory(pJobStorage, cByteNeeded);

	//获取打印工作队列
	if(!EnumJobs(hPrinter,0,pPrinterInfo->cJobs,2,(LPBYTE)pJobStorage,
		cByteNeeded,(LPDWORD)&cByteUsed,(LPDWORD)&nReturned))
	{
		free(pPrinterInfo);
		free(pJobStorage);
		pJobStorage = NULL;
		pPrinterInfo = NULL;
		return FALSE;
	}
		/*
	*  Return the information.
	*/ 
	*jobCount = nReturned;
	*pStatus = pPrinterInfo->Status;
	*ppJobInfo = pJobStorage;
	free(pPrinterInfo);

	return TRUE;
}

Thinco 2013-03-06
  • 打赏
  • 举报
回复
试了一下,似乎问题没有出现在vector上。 检查一下向vector中添加的内容吧,lz贴的代码不完整,看不出来。
引用 5 楼 wukaiping870123 的回复:
引用 1 楼 Thinco 的回复:vlist->clear(),然后再delete clear无效
jimette 2013-03-06
  • 打赏
  • 举报
回复
定义为成员变量 都是 vector<CPoint*> vlist CPoint *point= new CPoint; vlist.push_back(point);
火星大能猫 2013-03-06
  • 打赏
  • 举报
回复
引用 3 楼 sha_jinhao 的回复:
vector<PRINTER_JOB_INFO>* vlist = new vector<PRINTER_JOB_INFO>; 为什么要这样创建啊?
那应该怎么做比较合适?
火星大能猫 2013-03-06
  • 打赏
  • 举报
回复
引用 1 楼 Thinco 的回复:
vlist->clear(),然后再delete
clear无效
火星大能猫 2013-03-06
  • 打赏
  • 举报
回复
可以了.谢谢各位!!!还有个地方忘记关闭了
火星大能猫 2013-03-06
  • 打赏
  • 举报
回复
引用 13 楼 Thinco 的回复:
C/C++ code?1234567891011121314151617 //获取成功 else{ if (cJobs > 0) { //队列中有待打印文档 for (int i = 0;i < cJobs; i ++) { PRINTE……
悲催,问题依旧
MoreWindows 2013-03-05
  • 打赏
  • 举报
回复
每10ms就调用CPrintDealSrv::GetPrintJobList(void) 并且这个函数里还要执行new操作。 vector<PRINTER_JOB_INFO>* vlist = new vector<PRINTER_JOB_INFO>; 效率太低了。 你可以且个全局变量或静态变量呀,这样就不要每次都分配内存了。
jimette 2013-03-05
  • 打赏
  • 举报
回复
vector<PRINTER_JOB_INFO>* vlist = new vector<PRINTER_JOB_INFO>; 为什么要这样创建啊?
昨夜无风 2013-03-05
  • 打赏
  • 举报
回复
pJobs怎么来的?
Thinco 2013-03-05
  • 打赏
  • 举报
回复
vlist->clear(),然后再delete

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧