Wednesday, September 29, 2010

On the memory leak (1)


On the memory leak (1)

For a c / c + + programmers, the memory leak is a common cause for headache. There are already many techniques have been proposed to address this problem, such as Smart Pointer, Garbage Collection, etc.. Smart Pointer relatively mature technology, STL has already included support for Smart Pointer of the class, but it seems not widely used, but it can not solve all problems; Garbage Collection in Java, are already quite mature, but in c / c + + area The development is not smooth, although some early thinking in C + +, also joined the GC support. The real world is like this, as a c / c + + programmer, the memory leak is the pain in your heart forever. Luckily, there are many tools to help us verify the existence of a memory leak, identify the problem code.

The definition of a memory leak

We often say that generally refers to the memory leak heap memory leaks. Program heap memory is allocated from the heap, the size of any (memory block size can be run on the decision), released after use to display memory. Applications generally use malloc, realloc, new and other functions assigned to the block of memory from the heap after use, the program must be responsible for the corresponding call to free or delete the release of the block of memory, otherwise, this memory can not be used again, we that this memory is leaked. The following program demonstrates this small heap memory leak case:

void MyFunction (int nSize)

(

char * p = new char [nSize];

if (! GetStringFrom (p, nSize)) (

MessageBox ("Error");

return;

)

... / / Using the string pointed by p;

delete p;

)

Example One

When the function GetStringFrom () returns zero when the pointer p point to the memory will not be released. This is a common case of memory leak. Procedures at the entrance to allocate memory, the memory in the release of the exit, but the c function can quit at any place, so if there is an exit should be released without the release of memory, a memory leak occurs.

Broadly speaking, memory leaks, not only contains the heap memory leak, also includes the leakage of system resources (resource leak), such as the core state HANDLE, GDI Object, SOCKET, Interface, etc. Basically these objects are allocated by the operating system consumption of memory, and if these objects will eventually lead to leakage of memory leak. Moreover, consumption of certain objects is the core state memory, these objects will cause severe leakage throughout the operating system unstable. So contrast, system resources leak heap memory leaks than more serious.

GDI Object leak is a common resource leak:

void CMyView:: OnPaint (CDC * pDC)

(

CBitmap bmp;

CBitmap * pOldBmp;

bmp.LoadBitmap (IDB_MYBMP);

pOldBmp = pDC-> SelectObject (& bmp);

...

if (Something ()) (

return;

)

pDC-> SelectObject (pOldBmp);

return;

)




[Next]



Example Two

When the function Something () returns non-zero, the program does not quit before the election back to pOldBmp pDC, this will lead to pOldBmp point to the HBITMAP object leak. If the long run this procedure may cause the entire system Huaping. This problem is easily exposed under Win9x, because the GDI heap than Win9x or NT, Win2k much smaller.

Memory leak occurs by:

Way to place the classification of a memory leak can be divided into four categories:

1. Often made of a memory leak. Memory leak in the code will be repeatedly executed, when each is executed will lead to a memory leak. Such cases two, if Something () function always returns True, then the pOldBmp HBITMAP object always points to a leak.

2. Episodic memory leak. The code memory leak occurs only in certain environments or under the operation will occur. Such cases two, if Something () function only in specific circumstances only to return True, then the pOldBmp HBITMAP object does not always point to the leak. Regular and occasional hair is relative. For a particular environment, and perhaps occasional became often made of. So the test environment and test methods for detecting memory leaks is essential.

Three. A one-time memory leak. Memory leak in the code will be executed once, or because of algorithm flaws, there is always a just cause and a memory leak. For example, in the class constructor to allocate memory in the destructor does not release the memory, but because this class is a Singleton, so the memory leak only happens once. Another example:

char * g_lpszFileName = NULL;

void SetFileName (const char * lpcszFileName)

(

if (g_lpszFileName) (

free (g_lpszFileName);

)

g_lpszFileName = strdup (lpcszFileName);

)

Example Three

If the program at the end of the string does not release g_lpszFileName point, then, even if multiple calls SetFileName (), there will always be a memory, and only one memory leaks.

4. Implicit memory leak. Program kept running during the distribution of memory, but only until the end of the free memory. Strictly speaking, there is no memory leak here, because the final procedures for the release of all the memory. But for a server program, need to run a few days, weeks or even months, without timely release of the final run out of memory can also cause all the memory system. Therefore, we call this type of memory leak as the implicit memory leak. Give an example:

class Connection

(

public:

Connection (SOCKET s);

~ Connection ();

...

private:

SOCKET _socket;

...

);

class ConnectionManager

(

public:

ConnectionManager () (

)

~ ConnectionManager () (

list:: iterator it;

for (it = _connlist.begin (); it! = _connlist.end (); + + it) (

delete (* it);




[Next]



)

_connlist.clear ();

)

void OnClientConnected (SOCKET s) (

Connection * p = new Connection (s);

_connlist.push_back (p);

)

void OnClientDisconnected (Connection * pconn) (

_connlist.remove (pconn);

delete pconn;

)

private:

list _connlist;

);

Example Four

Assuming Client disconnected from the Server side, Server does not call OnClientDisconnected () function, then the connection on behalf of the Connection object that will not be timely removed (in the Server program exits when all the Connection object of analysis in ConnectionManager structure function is deleted). When there is continuous connection is established, disconnected implicit memory leak happened.

Use the program from the user point of view, the memory leak itself does not produce any harm, as a general user, simply do not feel the existence of a memory leak. Real damage is the accumulation of memory leaks, which will eventually consume all system memory to do. From this perspective, a one-time memory leak no hazard, because it does not accumulate, while the implicit danger of memory leak is very large, as compared to regular fat, and occasional memory leak it more difficult to be detected.






Recommended links:



ThinkPad First National USER Conference held in Beijing



Comparison Registry Tools



A MOVE into a digital video camera to camera



car gps



TD COMMERCIAL trial on April 1 the minimum consumption of 118 yuan a complete experience



Catalogs Newsgroup Clients



MP4 to WMP



IM: Decisive Force's crowded



Describes the Properties of the problem MIDP



Simple Trace And Ping Tools



Unicom's First Response To IPhone Parallel: Black-earned Money Through Tax Evasion



ASF TO MPG



Passion in June, feast Of Mach 5 Group buy



MKV To WMV



Infomation Accounting And Finance



Tuesday, September 14, 2010

How far from the focus to the expertise?



"Our success is built on a professional team, keep improving the quality awareness and quality of service and service attitude basis, we must first find a suitable market segments and solutions, and then focused to do so, consistently, so deep to do through. "This is a Microsoft Gold Certified Partner - Shanghai Grape City Information Technology Co., Ltd. (hereinafter referred to as grape City) Vice President Lee said in an interview are the words, which is the grape has always been adhering to the corporate philosophy of the city.

Technology first, focus on the professional achievements

Grape City into China since 1988, has always focused on technology development, focused on production and sales enterprise, specializing in IT technology play a greater value in the customer service role. It is this focus, the professional achievements of the grape City, Shideputao City as a software company, Microsoft's China region through the first CMMI 4 绾?(software Yanfa Capability Maturity Model) Certified Partner.

As a foreign-funded enterprises to enter China since the beginning of grape City, location is very clear focus on the Microsoft platform-based software development, implementation and training services. In the years of technical development and implementation based on the technical strength not only continue to strengthen, the customer is also deepening the understanding of business, customer service experience is enriched. Years of dedicated, accumulated enough resources and strength. Grape City in transition to provide customers with integrated solutions to their technology, services, advantages more fully into play. Grape City in many cases, we can see the advantages of its technology brings unique value to customers.

For example in 2007, in the implementation of a Fortune 500 client MOSS (Enterprise Information Portal) solution of the project, the city successfully grape existing customers in more than 20 different types of systems to the MOSS platform set under the goal of the system integration and to help clients achieve IT efficiency. In this project, more than 20 system integration, both technically and from the coordination of the existing system provider, has a great challenge.

In order to give full play to the role of MOSS platform, organized the city's grape technical consultant also carried out a number of technical breakthroughs, while the power company's technology organization to coordinate efforts to solve MOSS Microsoft technical support for multi-system integration issues, on the other hand the technology Consultants are divided into several groups, coordination of client resources, respectively, a provider of a way to communicate with the existing system to address the issue of integration of various systems. In many system providers to communicate with, the grape is not only the city's technical consultant demonstrated superb communication skills, but also to the customer's understanding, mastery of each system to win customer trust and praise.

Grape City in focus, while also continue to sum up the common customer needs, with targeted product development. It is understood that being a city built entirely on grape MOSS platform workflow product research and development, this product can significantly reduce the user based on the MOSS platform workflow system implementation and development costs. For business users and IT departments can conduct its own business process design, greatly reduce the learning cost and the learning curve.棰勬湡灏嗗湪浠婂勾7銆?鏈堜唤鍦ㄤ腑鍥藉競鍦烘帹鍑恒?

銆??閽荤爺涓嶆噲锛屼笓涓氶摳灏变笓闀?br />
銆??鍖哄埆浜庢湰鍦熺殑缁忛攢鍟嗭紝钁¤悇鍩庨潪甯告敞閲嶅洟闃熴?鎰忚瘑鐨勫缓璁撅紝涓嶄粎鏈変笓涓氱殑鍥㈤槦锛屾洿寮鸿皟鍝佽川鎰忚瘑銆佹湇鍔℃剰璇嗗拰鏈嶅姟鎬佸害锛屼互涓撲笟鐨勫洟闃熷拰涓撲笟鐨勬湇鍔″叏鏂逛綅鍦版弧瓒冲鎴风殑闇?眰銆傚湪澶氬勾鐨勮В鍐虫柟妗堥」鐩疄鏂戒腑锛岃憽钀勫煄鍦ㄥ叾涓撴敞鐨勫悇涓涓氫腑绉疮浜嗗ぇ閲忕殑鎴愬姛妗堜緥锛屽苟褰㈡垚浜嗙ǔ瀹氱殑闀挎湡瀹㈡埛缇や綋锛屽鍏堢伒钁嗛泤銆佺綏姘忚瘖鏂澶囥?娴﹀彂閾惰锛屽鏅數瀛愶紝鑻辫繄鍥介檯锛孶L缇庡崕璁よ瘉绛夌瓑銆?br />
銆??杩欎簺鎴愬姛妗堜緥瀵逛簬钁¤悇鍩庢潵璇村叿鏈夐潪甯告繁杩滅殑鎰忎箟锛屼竴鏂归潰褰㈡垚浜嗚壇濂界殑瀹㈡埛鍙g锛屾湁鍒╀簬钁¤悇鍩庢墿澶т笟鍔★紝鍙︿竴鏂归潰钁¤悇鍩庤繕灏嗚繖浜涙渚嬩腑鐨勭粡楠岀Н绱笅鏉ワ紝鍦ㄦ洿澶氱殑琛屼笟鍜屽鎴蜂腑杩涜鍒嗕韩銆傝繖褰㈡垚浜嗚憽钀勫煄鐨勪紭鍔匡紝鑳藉鏇村揩閫熷噯纭湴瀹氫綅瀹㈡埛闇?眰骞堕?杩囨妧鏈弧瓒冲鎴烽渶姹傘?

銆??姣斿鍦ㄦ煇涓叏鐞冩?璁よ瘉鍏徃鐨勫ぇ涓崕鍖哄疄鏂組OSS绯荤粺鐨勬渚嬩腑锛岃憽钀勫煄涓嶄粎鎴愬姛鍦板疄鐜颁簡瀹㈡埛瀵规枃妗g鐞嗙殑涓ユ牸瑕佹眰锛屾洿灏嗚繖绉嶆枃妗e綊绫荤鐞嗗彉鎴愪簡鑷繁鐨勪紭鍔匡紝鍦ㄥ悗鏉ユ湇鍔″叾浠栧鎴锋椂鍙戞尌浜嗛噸瑕佷綔鐢ㄣ?

銆??璇ュ鎴锋槸涓?鍏ㄧ悆鎬х殑闈炶惀鍒╂満鏋勶紝涓撲笟浠庝簨瀹夊叏璁よ瘉鍜屽挩璇笟鍔★紝鍏徃鐨勫ぇ涓崕鍖哄湪棣欐腐銆佸彴婀俱?澶ч檰(鍖椾含銆佷笂娴枫?鑻忓窞銆佸箍宸炪?閲嶅簡绛?鍧囪鏈夊姙鍏銆傝椤圭洰瑕佹眰鍩轰簬MOSS瀹炵幇鏂囨帶绠$悊锛屽疄鐜版枃妗e彉鏇存祦绋嬮泦涓鎺э紝寮傚湴鏂囨。鍚屾绛夊姛鑳姐?杩欎簺瑕佹眰铏界劧澶嶆潅锛屼絾鏄粠鎶?湳涓婃潵璁插浜庤憽钀勫煄鏉ヨ骞朵笉闅撅紝鐪熸鐨勬寫鎴樺湪浜庡鎴疯姹傚湪绯荤粺涓疄鐜板鏂囨。鐨勪笓涓氬垎绫荤鐞嗗拰鍙樻洿娴佺▼鎺у埗銆傝?瀹㈡埛鏂囨。鐨勫垎绫婚潪甯镐笓涓氬拰缁嗚嚧锛屼笉鍚屼簬涓?埇鏂囨。鐨勯?杈戙?鐔熸倝瀹㈡埛鐨勫垎绫绘ā寮忥紝骞跺湪绯荤粺涓疄鐜拌繖涔堢粏鑷村拰搴炴潅鐨勫垎绫昏姳璐逛簡澶ч噺鐨勬椂闂村拰绮惧姏銆傝憽钀勫煄鐨勬妧鏈【闂滑娣卞叆鍒板鎴蜂腑锛屼笉鏂湴杩涜娌熼?锛屼簡瑙e鎴风殑鏂囨。閫昏緫锛屼笉鏂湴璋冩暣瑙e喅鏂规涓殑鍔熻兘锛屾渶缁堝疄鐜颁簡瀹㈡埛甯屾湜鐨勬枃妗f爣绛惧垎绫绘柟寮忋?

銆??鍚庢潵锛岃憽钀勫煄鍙﹀鐨勪竴涓鎴蜂篃鏈夌被浼肩殑鏂囨。绠$悊闇?眰銆傝憽钀勫煄灏嗗叾鑳藉瀹炵幇鐨勫姛鑳藉睍绀虹粰瀹㈡埛鏃讹紝浠ゅ鎴峰ぇ涓鸿禐璧忓苟褰撳嵆涓庤憽钀勫煄绛剧害銆?br />
銆??姝f槸杩欑涓撴敞锛屼笓涓氾紝浣垮緱钁¤悇鍩庝笉鏂湴浠ョ旱娣卞彂灞曠殑鏂瑰紡璧颁笂浜嗚繖浜涢鍩熺殑楂樼锛屾渶缁堟垚涓鸿涓氱殑浣间郊鑰咃紝涓烘洿澶х殑鎴愬姛濂犲畾浜嗗熀纭??







相关链接:



Mkv File?



COMPARISON of Insert and Rs.Addnew



video format for blackberry



About Groovy AND Grails



Kaspersky: vigilance good at disguising the "beauty" release device Trojan



Cool Dog K's 2008 song King Achievement



Premier Screen Capture



BEST E-Commerce



Interview with B & Q (China) Vice President, Human Resources Director Miss Hu Weiyan



"Blue screen" hardware reasons and solutions



Process



File AND Disk Management Wizard



Free download mp4 to 3gp



The BACKBONE of retail business personal



Articles about Audio PLAYERS



Flac to aac