mercredi 21 février 2018

error for object : Non-aligned pointer being freed (2) in mac OS release mode only

So I have a code based with an overloaded delete operator which looks like this.This code works fine in debug mode but breaks in release mode. I am not sure why this is happening. When I googled this exception it stated that this usually happens when you try to delete a memory that had a custom allocator . (i.e deleting memory allocated by something else with delete and vice versa).

    void operator delete(void* heap) noexcept
    {     
        //Check if this memory is from this zone
        malloc_zone_t* res= malloc_zone_from_ptr(heap);
        if(res != NULL)
        {
              malloc_zone_free(zoned_ptr.load(),heap); //--->Breaks here 
        }
        else
        {
            std::free(heap);
        }           
    }

The new operator roughly looks like this

 malloc_zone_t* zoned_ptr;
 void* operator new(size_t sz)
    {
                void* s = nullptr;
                if(zoned_ptr==nullptr) 
                {
                    zoned_ptr = malloc_create_zone(0,0);
                    malloc_set_zone_name(zoned_ptr,"TestZone");
                    s = malloc_zone_malloc(zoned_ptr,sz);
                }


        else
        {
            s = malloc_zone_malloc(temp,sz);
        }

        assert(s != nullptr);
        if(s==nullptr)
        {
            throw std::bad_alloc();
        }
        return s;
    }

Now my question is why might I be getting a Non-aligned pointer being freed (2) error in the following in release mode

   malloc_zone_free(zoned_ptr.load(),heap); //--->Breaks here 

Aucun commentaire:

Enregistrer un commentaire