mercredi 26 octobre 2016

in libc++ for mac os string destructor is not called

In libc++ i have found that basic_string destructor does not gets called , once string goes out of the scope the memory is freed by calling delete operator rather than calling its destructor and then calling the delete operator from destructor, why so?????

Can some one explain this??? see the sample program

void * operator new ( size_t len ) throw ( std::bad_alloc )
{
    void * mem = malloc( len );
    if ( (mem == 0) && (len != 0) ) 
        throw std::bad_alloc();
    return mem;
}

void operator delete ( void * ptr ) throw()
{
    if ( ptr != 0 ) 
        free( ptr );
}

int main(int argc, const char * argv[]) 
{
    std::string  mystr("testing very very very big string for string class");
}

Put break point on new and delete and then check the call stack. new operator gets called from basic_string class while the delete gets called from the end of main, while ideally basic_string destructor should have called first and then the delete operator should be called via deallocate call of allocator.

Aucun commentaire:

Enregistrer un commentaire