[Good Habit of programming] releases allocated resources in reverse order.

Source: Internet
Author: User

Software functions seem to be dominated by resource management. Each module has a certain amount of resource allocation and release operations. When a module allocates the required resources, there is no exception to the allocation order of each resource. In the queue_init () function in figure 1, the resources are allocated in rows 58, 68, 72, 77, and 81. When a queue is no longer needed, you need to call the queue_fini () function to release related resources. It is important that, when releasing resources, queue_fini () needs to release the allocated resources in reverse order.

Example. c
00053: int queue_init (queue_t ** _ pp_queue, int _ size)
00054 :{
00055: pthread_mutexattr_t attr;
00056: queue_t * queue;
00057:
00058: queue = (queue_t *) malloc (sizeof (queue_t ));
00059: if (0 = queue ){
00060: return-1;
00061 :}
00062: * _ pp_queue = queue;
00063:
00064: memset (queue, 0, sizeof (* queue ));
00065: queue-> size _ = _ size;
00066:
00067: pthread_mutexattr_init (& attr );
00068: if (0! = Pthread_mutex_init (& queue-> mutex _, & attr )){
00069: goto error1;
00070 :}
00071:
00072: queue-> messages _ = (void **) malloc (queue-> size _ * sizeof (void *));
00073: if (0 = queue-> messages _){
00074: goto error1;
00075 :}
00076:
00077: if (0! = Sem_init (& queue-> sem_put _, 0, queue-> size _)){
00078: goto error2;
00079 :}
00080:
00081: if (0! = Sem_init (& queue-> sem_get _, 0, 0 )){
00082: goto error3;
00083 :}
00084:
00085: pthread_mutexattr_destroy (& attr );
00086:
00087: return 0;
00088:
00089: error3:
00090: sem_destroy (& queue-> sem_put _);
00091: error2:
00092: free (queue-> messages _);
00093: error1:
00094: pthread_mutexattr_destroy (& attr );
00095: free (queue );
00096: return-1;
00097 :}
00098:
00099: int queue_fini (queue_t ** _ pp_queue, int _ size)
00100 :{
00101: queue_t * queue = * _ pp_queue;
00102:
00103: if (0 = queue ){
00104: return-1;
00105 :}
00106:
00107: sem_destroy (& queue-> sem_get _);
00108: sem_destroy (& queue-> sem_put _);
00109: free (queue-> messages _);
00110: pthread_mutex_destroy (& queue-> mutex _);
00111: free (queue );
00112:} Figure 1

In fact, in many cases, the order of resource release is not important at all. The order of 107 and 108 rows in 1 is not very important. However, the order of rows 109 and 111 is very important, and the release operation of rows 111 can only be performed after all other resources are released. However, releasing resources in reverse order can be used as a habit to avoid problems caused by incorrect resource release sequence. In addition, such problems can easily cause program crashes.


This article from "to Jane Li cloud" blog, please be sure to keep this source http://yunli.blog.51cto.com/831344/252369

Related Article

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.