Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. why can't I use std::shared_ptr & std::unique_ptr like this? just got crash

why can't I use std::shared_ptr & std::unique_ptr like this? just got crash

Scheduled Pinned Locked Moved C / C++ / MFC
csharpvisual-studioquestion
9 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Falconapollo
    wrote on last edited by
    #1

    I'm using Visual Studio 2013. I don't konw why get crash in these following scenarios. What am I missing? Crash in this function:

    extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
    const void * pUserData
    )
    {
    if (!pUserData)
    return FALSE;

        if (!\_CrtIsValidPointer(pHdr(pUserData), sizeof(\_CrtMemBlockHeader), FALSE))
            return FALSE;
    
        return HeapValidate( \_crtheap, 0, pHdr(pUserData) );
    

    }

    Scenario 1:

    std::shared_ptr foo()
    {
    std::shared_ptr p(new int(10));
    size_t cnt = 10;
    while (cnt--)
    p.get()[cnt] = roll_die();

    return p;                   
    

    }

    void func(int* ptr, size_t cnt)
    {
    while (cnt--)
    {
    cout << ptr[cnt] << endl;
    }
    }

    void main()
    {
    std::shared_ptr u_ptr = nullptr;
    u_ptr = foo();
    func(u_ptr.get(), 10);
    }

    Scenario 2:

    std::unique_ptr foo()
    {
    std::unique_ptr p(new int(10));
    size_t cnt = 10;
    while (cnt--)
    p.get()[cnt] = roll_die();

    return p;                   
    

    }

    void func(int* ptr, size_t cnt)
    {
    while (cnt--)
    {
    cout << ptr[cnt] << endl;
    }
    }

    void main()
    {
    std::unique_ptr u_ptr = nullptr;
    u_ptr = foo();
    func(u_ptr.get(), 10);
    }

    CPalliniC L 2 Replies Last reply
    0
    • F Falconapollo

      I'm using Visual Studio 2013. I don't konw why get crash in these following scenarios. What am I missing? Crash in this function:

      extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
      const void * pUserData
      )
      {
      if (!pUserData)
      return FALSE;

          if (!\_CrtIsValidPointer(pHdr(pUserData), sizeof(\_CrtMemBlockHeader), FALSE))
              return FALSE;
      
          return HeapValidate( \_crtheap, 0, pHdr(pUserData) );
      

      }

      Scenario 1:

      std::shared_ptr foo()
      {
      std::shared_ptr p(new int(10));
      size_t cnt = 10;
      while (cnt--)
      p.get()[cnt] = roll_die();

      return p;                   
      

      }

      void func(int* ptr, size_t cnt)
      {
      while (cnt--)
      {
      cout << ptr[cnt] << endl;
      }
      }

      void main()
      {
      std::shared_ptr u_ptr = nullptr;
      u_ptr = foo();
      func(u_ptr.get(), 10);
      }

      Scenario 2:

      std::unique_ptr foo()
      {
      std::unique_ptr p(new int(10));
      size_t cnt = 10;
      while (cnt--)
      p.get()[cnt] = roll_die();

      return p;                   
      

      }

      void func(int* ptr, size_t cnt)
      {
      while (cnt--)
      {
      cout << ptr[cnt] << endl;
      }
      }

      void main()
      {
      std::unique_ptr u_ptr = nullptr;
      u_ptr = foo();
      func(u_ptr.get(), 10);
      }

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Scenario 1 doesn't 'crash' on my system. Scenario 2 should work, see[^], but, for instance, it doesn't on my system. I fixed it this way:

      std::unique_ptr<int> foo()
      {
      std::unique_ptr<int> p(new int(10));
      size_t cnt = 10;
      while (cnt--)
      p.get()[cnt] = roll_die();

      return std::move(p);
      

      }

      Carlo needs more :java::java::java: this morning. You allocated just ONE integer!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :mad: :-D change from

      new int(10);

      to

      new int[10];

      Veni, vidi, vici.

      In testa che avete, signor di Ceprano?

      F 1 Reply Last reply
      0
      • CPalliniC CPallini

        Scenario 1 doesn't 'crash' on my system. Scenario 2 should work, see[^], but, for instance, it doesn't on my system. I fixed it this way:

        std::unique_ptr<int> foo()
        {
        std::unique_ptr<int> p(new int(10));
        size_t cnt = 10;
        while (cnt--)
        p.get()[cnt] = roll_die();

        return std::move(p);
        

        }

        Carlo needs more :java::java::java: this morning. You allocated just ONE integer!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :mad: :-D change from

        new int(10);

        to

        new int[10];

        Veni, vidi, vici.

        F Offline
        F Offline
        Falconapollo
        wrote on last edited by
        #3

        CPallini wrote:

        Scenario 1 doesn't 'crash' on my system.

        I'm using Visual Studio 2013.

        CPalliniC 1 Reply Last reply
        0
        • F Falconapollo

          CPallini wrote:

          Scenario 1 doesn't 'crash' on my system.

          I'm using Visual Studio 2013.

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Please, see my updated answer.

          Veni, vidi, vici.

          In testa che avete, signor di Ceprano?

          L 1 Reply Last reply
          0
          • F Falconapollo

            I'm using Visual Studio 2013. I don't konw why get crash in these following scenarios. What am I missing? Crash in this function:

            extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
            const void * pUserData
            )
            {
            if (!pUserData)
            return FALSE;

                if (!\_CrtIsValidPointer(pHdr(pUserData), sizeof(\_CrtMemBlockHeader), FALSE))
                    return FALSE;
            
                return HeapValidate( \_crtheap, 0, pHdr(pUserData) );
            

            }

            Scenario 1:

            std::shared_ptr foo()
            {
            std::shared_ptr p(new int(10));
            size_t cnt = 10;
            while (cnt--)
            p.get()[cnt] = roll_die();

            return p;                   
            

            }

            void func(int* ptr, size_t cnt)
            {
            while (cnt--)
            {
            cout << ptr[cnt] << endl;
            }
            }

            void main()
            {
            std::shared_ptr u_ptr = nullptr;
            u_ptr = foo();
            func(u_ptr.get(), 10);
            }

            Scenario 2:

            std::unique_ptr foo()
            {
            std::unique_ptr p(new int(10));
            size_t cnt = 10;
            while (cnt--)
            p.get()[cnt] = roll_die();

            return p;                   
            

            }

            void func(int* ptr, size_t cnt)
            {
            while (cnt--)
            {
            cout << ptr[cnt] << endl;
            }
            }

            void main()
            {
            std::unique_ptr u_ptr = nullptr;
            u_ptr = foo();
            func(u_ptr.get(), 10);
            }

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Hi, The std::unique_ptr[^] is unique meaning non-assignable and non-copyable so you will need to use move semantics to pass it around or give up ownership with unique_ptr::release()[^].

            func(u_ptr.release(), 10);

            Best Wishes, -David Delaune

            1 Reply Last reply
            0
            • CPalliniC CPallini

              Please, see my updated answer.

              Veni, vidi, vici.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              It took a while, but I just figured that out too. :(

              CPalliniC 1 Reply Last reply
              0
              • L Lost User

                It took a while, but I just figured that out too. :(

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                Richard MacCutchan wrote:

                t took a while, but I just figured that out too.

                Yes, sometimes it takes a while to figure out we need more and more caffeine. :-D

                Veni, vidi, vici.

                In testa che avete, signor di Ceprano?

                L 1 Reply Last reply
                0
                • CPalliniC CPallini

                  Richard MacCutchan wrote:

                  t took a while, but I just figured that out too.

                  Yes, sometimes it takes a while to figure out we need more and more caffeine. :-D

                  Veni, vidi, vici.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  CPallini wrote:

                  we need more and more caffeine.

                  Speak for yourself, I need more brain.

                  CPalliniC 1 Reply Last reply
                  0
                  • L Lost User

                    CPallini wrote:

                    we need more and more caffeine.

                    Speak for yourself, I need more brain.

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #9

                    Me too. Caffeine is way more practical, though.

                    Veni, vidi, vici.

                    In testa che avete, signor di Ceprano?

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups