mercredi 14 août 2019

What happens when reset() is called by a unique_ptr twice?

I have the following program where I call mc.reset() twice. I thought this would give me an error but this works without errors.

#include <iostream> 
#include <csignal> 
#include <memory>  // for unique pointer
using namespace std; 

std::unique_ptr<int> mc;

void signal_handler( int signal_num ) { 
    cout << "The interrupt signal is (" << signal_num << "). \n"; 

    mc.reset();
    mc.reset();

    // terminate program   
    exit(signal_num);   
} 

int main () {
    mc.reset (new int); 
    *mc = 5;

    // Press Control + c to raise SIGINT
    signal (SIGINT, signal_handler);

    while(true) 
        cout << "Hello GeeksforGeeks..." << endl; 
    return 0;
}

I used valgrind to check for memory leaks and found none:

==5467== 
==5467== HEAP SUMMARY:
==5467==     in use at exit: 0 bytes in 0 blocks
==5467==   total heap usage: 3 allocs, 3 frees, 73,732 bytes allocated
==5467== 
==5467== All heap blocks were freed -- no leaks are possible
==5467== 
==5467== For counts of detected and suppressed errors, rerun with: -v
==5467== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

What is actually happening when reset is called? Why am I able to call it twice without any problems?

Aucun commentaire:

Enregistrer un commentaire