Memory attention release when thread is created

Source: Internet
Author: User

Situation: Recently learned the old code, found the application memory, and then the pointer to the thread callback function, but never found the place to release. So write below the small demo to learn under.


#include "stdafx.h" #include <iostream> #include <atlstr.h> #include <iostream> using namespace std; DWORD WINAPI Testthreadproc (lpvoid lpparameter) {if (NULL! = lpparameter) {delete [] lpparameter;//If you comment out this sentence, in the Task Manager
		You will see that the memory is constantly rising.
	Lpparameter = NULL;
	} cout << "Test thread End:" << Endl;
return 0;	

	} int _tmain (int argc, _tchar* argv[]) {DWORD dwthreadid;

		for (int i = 0; i < ++i) {int* pInt = new int[1000 * 10000];
			if (NULL = = pInt) {cout << "Allocate Memory Failed:" << Endl;
		Break

		} HANDLE hthread = CreateThread (NULL, 1024x768 * 1024x768 *, Testthreadproc, pInt, 0, &dwthreadid);

			if (NULL = = hthread) {cout << "Create Thread Failed ..." << Endl;
				Note the thread creation failure also frees the memory to release if (NULL! = pInt) {delete [] pInt;
			PInt = NULL;
		}} else {cout << "Create Thread successed ..." << Endl; } Sleep (1000);
Easy to Observe} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.