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. Managed C++/CLI
  4. HeapAlloc

HeapAlloc

Scheduled Pinned Locked Moved Managed C++/CLI
csharptutorial
12 Posts 4 Posters 0 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.
  • M Offline
    M Offline
    miah alom
    wrote on last edited by
    #1

    Hi, I have an application in C# which allocates 200MB of data from the unmanaged heap using the [DllImport("kernel32")] static extern void* HeapAlloc(int hHeap, int flags, int size); I have another fucntion [DllImport("cppwrapper.dll")] private extern static int test(IntPtr DataOut, out string ErrorOut); I want to pass the the pointer returned by HeapAlloc into the test function using the parameter DataOut instead of string as I run into OutofmemoryException randomly(There is a limitation on the managed heap). But I am constantly getting the system.accessviolation exception. The body of test method looks like SOME_API int test(LPSTR* DataOut, LPSTR* ErrorOut) { return _lpsomefunction(DataOut, ErrorOut); } Can someone guide me in the right direction...

    L 1 Reply Last reply
    0
    • M miah alom

      Hi, I have an application in C# which allocates 200MB of data from the unmanaged heap using the [DllImport("kernel32")] static extern void* HeapAlloc(int hHeap, int flags, int size); I have another fucntion [DllImport("cppwrapper.dll")] private extern static int test(IntPtr DataOut, out string ErrorOut); I want to pass the the pointer returned by HeapAlloc into the test function using the parameter DataOut instead of string as I run into OutofmemoryException randomly(There is a limitation on the managed heap). But I am constantly getting the system.accessviolation exception. The body of test method looks like SOME_API int test(LPSTR* DataOut, LPSTR* ErrorOut) { return _lpsomefunction(DataOut, ErrorOut); } Can someone guide me in the right direction...

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, it would be more accurate to use: [DllImport("kernel32")] static extern IntPtr HeapAlloc(IntPtr hHeap, int flags, int size); since both hHeap and the return value are pointers (hence 4B on Win32 and 8B on Win64, exactly what IntPtr gives you). As such you can pass the return value to your test method. But then I am puzzled as to why you would use HeapAlloc at all. You could allocate a byte array of the same size, and use GCHandle class to "pin" it in memory, then get its pointer (which is an IntPtr obviously). That way you get managed memory that will not move, and will live until you Free the GCHandle. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


      M M 3 Replies Last reply
      0
      • L Luc Pattyn

        Hi, it would be more accurate to use: [DllImport("kernel32")] static extern IntPtr HeapAlloc(IntPtr hHeap, int flags, int size); since both hHeap and the return value are pointers (hence 4B on Win32 and 8B on Win64, exactly what IntPtr gives you). As such you can pass the return value to your test method. But then I am puzzled as to why you would use HeapAlloc at all. You could allocate a byte array of the same size, and use GCHandle class to "pin" it in memory, then get its pointer (which is an IntPtr obviously). That way you get managed memory that will not move, and will live until you Free the GCHandle. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        Hi Luc, Is this the C# board now? Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        M L 2 Replies Last reply
        0
        • L Luc Pattyn

          Hi, it would be more accurate to use: [DllImport("kernel32")] static extern IntPtr HeapAlloc(IntPtr hHeap, int flags, int size); since both hHeap and the return value are pointers (hence 4B on Win32 and 8B on Win64, exactly what IntPtr gives you). As such you can pass the return value to your test method. But then I am puzzled as to why you would use HeapAlloc at all. You could allocate a byte array of the same size, and use GCHandle class to "pin" it in memory, then get its pointer (which is an IntPtr obviously). That way you get managed memory that will not move, and will live until you Free the GCHandle. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


          M Offline
          M Offline
          miah alom
          wrote on last edited by
          #4

          Thanks a lot for the reply. I will try and see if it works.

          1 Reply Last reply
          0
          • M Mark Salsbery

            Hi Luc, Is this the C# board now? Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            M Offline
            M Offline
            miah alom
            wrote on last edited by
            #5

            Sorry if this bothered you. Since there was some C++ involved in the problem so I posted this question.

            M 1 Reply Last reply
            0
            • M Mark Salsbery

              Hi Luc, Is this the C# board now? Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Hi Mark, no we are not colonizing the C++/CLI forum :laugh:; I just copied and improved two lines of code from the OP and introduced some .NET stuff that exists for all CLR languages AFAIK. If, however I had known at that time that the identical question was posted in the C# forum, I would have answered it there (identically). As it is, I told him overthere not to duplicate stuff... Please feel free to adjust whatever I may have messed up language wise. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


              M 1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, it would be more accurate to use: [DllImport("kernel32")] static extern IntPtr HeapAlloc(IntPtr hHeap, int flags, int size); since both hHeap and the return value are pointers (hence 4B on Win32 and 8B on Win64, exactly what IntPtr gives you). As such you can pass the return value to your test method. But then I am puzzled as to why you would use HeapAlloc at all. You could allocate a byte array of the same size, and use GCHandle class to "pin" it in memory, then get its pointer (which is an IntPtr obviously). That way you get managed memory that will not move, and will live until you Free the GCHandle. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                M Offline
                M Offline
                miah alom
                wrote on last edited by
                #7

                Sometimes I have to allocate more than 500 MB of data. In such situations I run into OutOfMemoryException. The real problem here is a third party API I am using expects string as an input parameter which can sometimes be as large as 800 MB. That why I am using HeapAlloc. HeapAlloc has worked fine for me so far without any problems.

                L Richard Andrew x64R 2 Replies Last reply
                0
                • M miah alom

                  Sometimes I have to allocate more than 500 MB of data. In such situations I run into OutOfMemoryException. The real problem here is a third party API I am using expects string as an input parameter which can sometimes be as large as 800 MB. That why I am using HeapAlloc. HeapAlloc has worked fine for me so far without any problems.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  OK I hope you don't forget to release that memory eventually... You probably should make a managed object that allocates, pins, provides the pointer, and has a Dispose/finalizer/destructor to make sure it gets freed too. You might want to look at the LP_Pinner class in my TrayIconBuster article. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                  M 1 Reply Last reply
                  0
                  • M miah alom

                    Sorry if this bothered you. Since there was some C++ involved in the problem so I posted this question.

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #9

                    No problem :)  I was just giving Luc a hard time. Cheers, Mark

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      Hi Mark, no we are not colonizing the C++/CLI forum :laugh:; I just copied and improved two lines of code from the OP and introduced some .NET stuff that exists for all CLR languages AFAIK. If, however I had known at that time that the identical question was posted in the C# forum, I would have answered it there (identically). As it is, I told him overthere not to duplicate stuff... Please feel free to adjust whatever I may have messed up language wise. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #10

                      ;P

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      1 Reply Last reply
                      0
                      • L Luc Pattyn

                        OK I hope you don't forget to release that memory eventually... You probably should make a managed object that allocates, pins, provides the pointer, and has a Dispose/finalizer/destructor to make sure it gets freed too. You might want to look at the LP_Pinner class in my TrayIconBuster article. :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                        M Offline
                        M Offline
                        miah alom
                        wrote on last edited by
                        #11

                        I do a HeapFree everytime. Actually I am running this on a Grid environment with 1000's of items and each item is memory intensive and each item sometimes goes beyond 1.5 GB. But after every run the memory comes back to 60 mb or so. I will try whatever you said. Thanks again for all the valuable information.

                        1 Reply Last reply
                        0
                        • M miah alom

                          Sometimes I have to allocate more than 500 MB of data. In such situations I run into OutOfMemoryException. The real problem here is a third party API I am using expects string as an input parameter which can sometimes be as large as 800 MB. That why I am using HeapAlloc. HeapAlloc has worked fine for me so far without any problems.

                          Richard Andrew x64R Offline
                          Richard Andrew x64R Offline
                          Richard Andrew x64
                          wrote on last edited by
                          #12

                          This is purely a guess on my part, but perhaps you might consider using VirtualAlloc instead of HeapAlloc? Maybe the VirtualAlloc will be able to provide all the memory you need without OutOfMemory exceptions.

                          -------------------------------- "All that is necessary for the forces of evil to win in the world is for enough good men to do nothing" -- Edmund Burke

                          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