[c/c++]_[intermediate]_[Use smart pointers to release malloc out of Heap space]

Source: Internet
Author: User


Scene:

1. The Wrap class object can be wrapped using auto_ptr, then the object can be disposed automatically after the method ends, so that the free statement or CloseHandle can be omitted when a conditional-judged statement is available.

http://blog.csdn.net/infoworld/article/details/9008911

One of the attributes of 2.c++ is that the class object (an object that is not a return value) automatically calls the destructor at the end of the method, so that the destructor can be placed to release resources.

3. Here is a practical wrap class for a class similar to Auto_ptr, and you can refer to customizing the specific wrap class to your own needs. One of the drawbacks of auto_ptr is that it only supports new objects.

The second is that the array cannot be deleted. Delete []

4. Proper use of wrapobject can effectively reduce memory leaks, because in many C programming, too many if statements need to release objects before they return, making it easy to make mistakes or release multiple times.


File Wrap_object.h

#ifndef __wrap_object#define __wrap_object#include <stdlib.h> #include <windows.h>template<bool ( winapi* wraphandlefunc) (void*) >class wraphandle{public:wraphandle (void* data):d Ata_ (data) {}~wraphandle () {if ( Data_) {wraphandlefunc (data_);d ata_ = NULL;}} Private:void* Data_;}; Class Wrapmalloc{public:wrapmalloc (void* data):d Ata_ (data) {}~wrapmalloc () {free (data_);d ata_ = NULL;} void Reset () {free (data_);d ata_ = NULL;} Private:void* Data_;}; #endif

How to use:

Specify an HTTP server.  Const char* www = "www";  std::string urladdr = Dhoptiondomain::getvalue ("Url:brand");    Const char* Www_start = STRSTR (Urladdr.c_str (), www);  if (!hsession) {return knetworkerror;    } wraphandle<winhttpclosehandle> Session_wh (hsession);  wchar_t* Www_start_unicode = Convertutf8tounicode (Www_start);  Wrapmalloc WWW_WM (Www_start_unicode);   Hconnect = Winhttpconnect (hsession, Www_start_unicode, Internet_default_http_port, 0);    Create an HTTP request handle.    if (!hconnect) {return knetworkerror;  } wraphandle<winhttpclosehandle> Connect_wh (hconnect); Hrequest = Winhttpopenrequest (Hconnect, L "GET", L "/win-update.xml", NULL, Winhttp_no_ REFERER, Winhttp_default_accept_types, Winhttp_f   Lag_refresh);    Send a request.  if (!hrequest) {return knetworkerror; } WRAPHANDLE&LT;WINHTTPCLOsehandle> REQUEST_WH (hrequest);                                     Bresults = WinHttpSendRequest (hrequest, winhttp_no_additional_headers, 0,    Winhttp_no_request_data, 0, 0, 0);    End the request.  if (!bresults) {return knetworkerror;   } bresults = Winhttpreceiveresponse (hrequest, NULL);

Note: If you don't have wrapobject, you have to remember to release the ever-increasing handle object in the IF, which is one of the benefits of improving the quality of the program and is recommended to be used directly in the project.


[c/c++]_[intermediate]_[Use smart pointers to release malloc out of Heap space]

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.