Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1044809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:53:14+00:00 2026-05-16T15:53:14+00:00

Why in this code (this is just working code and not fully exception safe)

  • 0

Why in this code (this is just working code and not fully exception safe) I’m getting an assertion error:
HEAP_CORRUPTION_DETECTED …

class Allocator
{

public:

    explicit Allocator()
    {
        std::cout << "Allocator()" << '\n';
    }

    virtual ~Allocator()
    {

        std::cout << "~Allocator()" << '\n';
    }

    template<class T>
    T* Allocate(const std::size_t count)
    {
        return static_cast<T*>(::operator new(count));
    }

    /*[nothrow]*/
    template<class T>
    void Construct(void* where_, const T& what)
    {
        new (where_) T(what);
    }

    template<class T>
    void Destruct(T* where_)
    {
        where_->~T();
    }

    template<class FwdIter>
    void Destruct(FwdIter first, FwdIter last)
    {
        while (first != last)
        {
            this->Destruct(&*first);
            ++first;
        }
    }

    template<class T>
    void Deallocate(T* where_)
    {
        ::operator delete(where_);
    }
};


template<class T>
class ToyImplementation
{
private:

public:
    typedef T value_type;
    T* data_;///move to private
    std::size_t size_;
    std::size_t capacity_;
    explicit ToyImplementation(Allocator& alloc, const std::size_t count):data_(alloc.Allocate<value_type>(count)),size_(0),capacity_(count)
    {
        std::cout << "ToyImplementation()" << '\n';
        //throw std::exception();
    }
    ~ToyImplementation()
    {
        std::cout << "~ToyImplementation()" << '\n';
    }
};


template<class T>
class ToyA
{
private:
    ToyImplementation<T>* implementation_;
    typedef ToyImplementation<T> value_type;
    Allocator alloc_;
public:
    explicit ToyA(const std::size_t count = 0): implementation_(alloc_.Allocate<value_type>(sizeof(value_type)))
    {
        alloc_.Construct(implementation_, value_type(alloc_,count));
        alloc_.Deallocate(implementation_);//<<--------HERE ERROR IS TRIGGERED
        std::cout << "Toy()" << '\n';
    }
    ~ToyA()
    {
        std::cout << "~Toy()" << '\n';
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    ToyA<int> t(10);
    return 0;
}

Seven lines up is a line which causes an error. Line is marked <<——–HERE ERROR IS TRIGGERED
Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:53:14+00:00Added an answer on May 16, 2026 at 3:53 pm

    You corrupted your heap because your allocation is incorrect. Namely, consider this:

    template<class T>
    T* Allocate(const std::size_t count)
    {
        return static_cast<T*>(::operator new(count));
    }
    

    If count is one, you get one byte. (You then try to construct an object which has a size greater than one in that memory…boom.)

    You probably want:

    template<class T>
    T* Allocate(const std::size_t count)
    {
        return static_cast<T*>(::operator new(sizeof(T) * count));
    }
    

    Note your function design is a bit unorthodox. The allocation and deallocation functions should strictly allocate and deallocate (no casts!), like this:

    template<class T>
    void* Allocate(const std::size_t count)
    {
        return ::operator new(sizeof(T) * count);
    }
    
    void Deallocate(void* where_)
    {
        ::operator delete(where_);
    }
    

    And your construction and destruction functions should be the ones that construct and return an object, and destruct an object:

    template<class T>
    T* Construct(void* where_, const T& what)
    {
        return new (where_) T(what);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was just working on some code and caught myself making this error if
I am working on const-correctness of my code and just wondered why this code
here's the sample code (fully working - just copy to an empty html file
Can anybody help with this code I just don't know where I am going
I have this test code that just saves an XML file to a folder.
this code in my plugin used to work just fine: jQuery('#embedded_obj', context).get(0).getVersion(); and the
Given this JavaScript code (which is just a comment referring to a url): //
I just saw this code while studying the wordpress source code (PHP), You can
I just found this code in reflector in the .NET base libraries... if (this._PasswordStrengthRegularExpression
This code works on one of my dev boxes (using JQ v1.3.2.) I just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.