简体   繁体   中英

freeing memory issue

I have an issue regarding freeing memory as under:

            string points; // some points sequences

    char* charPoints = (char*)malloc((points.length() +1) * sizeof(char));

            //do something

    free(charPoints);

Even then after freeing memory is leaked when checked with instruments

The only problem I see is when malloc returns NULL because it can't allocate enough contiguous memory. Are you sure that the memory leak is due to the malloc/free?

The pointer you pass to free must be the same one which returned by malloc . If you pass a different pointer it will result in a undefined behavior. Take a copy of the pointer before you do the operation such as incrementing the charPoints and then pass this original pointer to free function to properly release memory.

您的泄漏检测仪器很糟糕。

After freeing the memory update with NULL value shown bellow

free(charPoints); charPoints ="NULL"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM