HeapAlloc
-
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...
-
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...
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
-
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
Hi Luc, Is this the C# board now? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
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
-
Hi Luc, Is this the C# board now? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Hi Luc, Is this the C# board now? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
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
-
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
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.
-
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.
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
-
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
;P
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Sorry if this bothered you. Since there was some C++ involved in the problem so I posted this question.
No problem :) I was just giving Luc a hard time. Cheers, Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
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
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.
-
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.
This is purely a guess on my part, but perhaps you might consider using
VirtualAlloc
instead ofHeapAlloc
? Maybe theVirtualAlloc
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