Memory corruption error while using auto_ptr

Deepak

I have written a sample code to learn working of auto_ptr, but when I am running my sample code I am getting memory corruption.

My sample code is given below..

#include <iostream>
#include <memory>
#include <cv.h>
#include <highgui.h>
using namespace std;
int main()
{
  for (int i = 0; i < 1000; i++)
  {
    IplImage* temp = cvLoadImage("sample.png");
    auto_ptr<IplImage> aptr (temp);
  }
}

Following is the error message that I got from the above program:

*** glibc detected *** ./a.out: double free or corruption (out): 0x00000000008325c0 ***
======= Backtrace: =========
/lib64/libc.so.6[0x36eb276166]
/lib64/libc.so.6[0x36eb278ca3]
./a.out[0x400e9b]
./a.out[0x400df7]
/lib64/libc.so.6(__libc_start_main+0xfd)[0x36eb21ed1d]
./a.out[0x400cf9]
======= Memory map: ========
00400000-00402000 r-xp 00000000 00:19 7879127                            

Can anybody tell the reason for the above error ??

Jonathan Mee

After looking at the comments on karlphillip's answer, I'm going to update this answer to use, cvReleaseImage.

It should be pointed out that you can accomplish this with unique_ptr because it will let you specify a custom deleter:

unique_ptr<IplImage, void(*)(IplImage*)> aptr(cvLoadImage("sample.png"), [](IplImage* temp){cvReleaseImage(&temp);});

This will allow you to use an auto-pointer without changing cvLoadImage and still delete the memory correctly.

EDIT:

unique_ptr is an owning auto-pointer, meaning that it will execute it's deleter to cleanup the allocated memory that it owns: https://en.cppreference.com/w/cpp/memory/unique_ptr

Unlike auto_ptr which always called delete, you can pass a custom deleter to unique_ptr.

When passing a custom deleter in the unique_ptr you need to provide the functor signature to the unique_ptr.

So to break my code down:

  • unique_ptr<IplImage, void(*)(IplImage*)> : This is a unique_ptr which will contain an IplImage*. The deleter returns void and takes an IplImage* as an argument.
  • aptr(cvLoadImage("sample.png"), : This is argument needs to be a manageable pointer which the unique_ptr will take ownership of, or a nullptr. Obviously cvLoadImage is returning the manageable pointer.
  • [](IplImage* temp){cvReleaseImage(&temp);}); : This the custom deleter that I'm passing to unique_ptr. I'm wrapping it in a lambda so I can dereference the managed pointer held by unique_ptr cause according to this cvReleaseImage needs to take a pointer to the reference to be deleted.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Memory error while using memcpy?

From Dev

Heap Corruption while using CreateWindowExW

From Dev

auto_ptr not working as expected - Compile error

From Dev

Error : malloc():memory corruption in Comparison function for sort

From Dev

malloc( ): memory corruption (fast) error C++

From Dev

Memory error while using selenium (Python)

From Dev

Python: Memory Error while using MatplotLib

From Dev

How do I return an auto_ptr of a base class using an auto_ptr of a derived class?

From Dev

How do I return an auto_ptr of a base class using an auto_ptr of a derived class?

From Dev

Memory leak while using shared_ptr

From Dev

Why auto_ptr initialization using the assignment syntax is not allowed

From Dev

Why auto_ptr initialization using the assignment syntax is not allowed

From Dev

C++ Memory Corruption When Using String Literals

From Dev

OUT OF MEMORY ERROR while using multiple gifs in a single activity

From Dev

Get 'cannot allocate memory' error while using mmap() on OSX 10.12

From Dev

Error while executing free() saying "glibc detected double free or corruption"

From Dev

Why is there memory leak while using shared_ptr as a function parameter?

From Dev

out of memory error while using viewpager+imageview even when using cache memory and asynctask

From Dev

C - memory corruption with threads

From Dev

Bypass memory corruption limitations

From Dev

Bison malloc memory corruption

From Dev

Memory Corruption on Allocation

From Dev

Memory corruption cause by free()

From Dev

Memory Corruption on Allocation

From Dev

Local Memory Corruption

From Dev

"using" (or other mechanism) to swap in unique_ptr for auto_ptr in C++11?

From Dev

"using" (or other mechanism) to swap in unique_ptr for auto_ptr in C++11?

From Dev

Critical error while freeing memory

From Dev

memory error while extending lists

Related Related

  1. 1

    Memory error while using memcpy?

  2. 2

    Heap Corruption while using CreateWindowExW

  3. 3

    auto_ptr not working as expected - Compile error

  4. 4

    Error : malloc():memory corruption in Comparison function for sort

  5. 5

    malloc( ): memory corruption (fast) error C++

  6. 6

    Memory error while using selenium (Python)

  7. 7

    Python: Memory Error while using MatplotLib

  8. 8

    How do I return an auto_ptr of a base class using an auto_ptr of a derived class?

  9. 9

    How do I return an auto_ptr of a base class using an auto_ptr of a derived class?

  10. 10

    Memory leak while using shared_ptr

  11. 11

    Why auto_ptr initialization using the assignment syntax is not allowed

  12. 12

    Why auto_ptr initialization using the assignment syntax is not allowed

  13. 13

    C++ Memory Corruption When Using String Literals

  14. 14

    OUT OF MEMORY ERROR while using multiple gifs in a single activity

  15. 15

    Get 'cannot allocate memory' error while using mmap() on OSX 10.12

  16. 16

    Error while executing free() saying "glibc detected double free or corruption"

  17. 17

    Why is there memory leak while using shared_ptr as a function parameter?

  18. 18

    out of memory error while using viewpager+imageview even when using cache memory and asynctask

  19. 19

    C - memory corruption with threads

  20. 20

    Bypass memory corruption limitations

  21. 21

    Bison malloc memory corruption

  22. 22

    Memory Corruption on Allocation

  23. 23

    Memory corruption cause by free()

  24. 24

    Memory Corruption on Allocation

  25. 25

    Local Memory Corruption

  26. 26

    "using" (or other mechanism) to swap in unique_ptr for auto_ptr in C++11?

  27. 27

    "using" (or other mechanism) to swap in unique_ptr for auto_ptr in C++11?

  28. 28

    Critical error while freeing memory

  29. 29

    memory error while extending lists

HotTag

Archive