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 8395605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:20:37+00:00 2026-06-09T20:20:37+00:00

The following code: class Something { public: ~Something() { } }; int main() {

  • 0

The following code:

class Something
{
public:
     ~Something()
    {
    }
};

int main()
{
    Something* s = new Something[1]; // raw pointer received from C api
    std::shared_ptr<Something> p = std::shared_ptr<Something>(s);
    std::vector<std::shared_ptr<Something>> v(&p,&p+1);

    return 0;
}

gives the following error in VS Express 2010:

---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!

File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgdel.cpp
Line: 52

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

Remove the destructor from Something and the error disappears, Why do I get this error?

Update:

Later I will have something like:

Something* s = new Something[100];

and individual shared pointers will be passed around to other objects

  • 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-06-09T20:20:38+00:00Added an answer on June 9, 2026 at 8:20 pm
    Something* s = new Something[1]; // raw pointer received from C api
    std::shared_ptr<Something> p = std::shared_ptr<Something>(s); 
    

    is incorrect usage, since

    ~shared_ptr();
    

    Effects:
    — If *this is empty or shares ownership with another shared_ptr instance (use_count() > 1),
    there are no side effects.

    — Otherwise, if *this owns an object p and a deleter d, d(p) is called.

    — Otherwise, *this owns a pointer p, and delete p is called.

    Default deleter is operator delete, but you have Something* s = new Something[1]; allocated by array-new operator, that should be deleted with array-delete operator (delete[]), otherwise it’s undefined behaviour. You should construct shared_ptr with specific deleter, or use something for arrays, for example boost::shared_array.

    For example this code is correct.

    template<typename T>
    void deleter(T* p)
    {
       delete[] p;
    }
    
    Something* s = new Something[1]; // raw pointer received from C api
    std::shared_ptr<Something> p = std::shared_ptr<Something>(s, deleter<Something>);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following code public class Test { public static void main(String[] args){ int
I have the following code: class Test { public static void main(String[] args) {
In the following code: class SomeClass { vector<int> i; vector<bool> b; public: int& geti()
I have the following code: class Base { public: int x,y; Base() { x=10;
The following code does not compile: public class GenericsTest { public static void main(String[]
I have the following code public class Test { Lock lock = new ReentrantLock();
The following is an extract from my code: public class AllIntegerIDs { public AllIntegerIDs()
i have written the following code class Program { static void Main(string[] args) {
I have the following code :- class A : public B { public: _container
I have the following code: class SomeClass {}; class SomeOtherClass { SomeClass& someObj; public:

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.