Sign in to follow this  

Allocation memory

This topic is 3458 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I have a problem: after creation of the window, I cannot do any allocation in the memory. I do such a way: mMainWnd = CreateWindowEx( style_ex, (LPCTSTR)RegisterClassEx(&wcex), NULL, style, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, mInstance, NULL); char* tt = new char[1000000000]; // in this string I have an exception. hear is the output: First-chance exception at 0x7c812aeb in test_appD.exe: Microsoft C++ exception: std::bad_alloc @ 0x00dcf214. Unhandled exception at 0x7c812aeb in test_appD.exe: Microsoft C++ exception: std::bad_alloc @ 0x00dcf214. The program '[3188] test_appD.exe: Native' has exited with code -529697949 (0xe06d7363). error code is: 8 ERROR_NOT_ENOUGH_MEMORY 8 Not enough storage is available to process this command. This says that have no memory, but I have 2gb of the memory. I can say that if my code will be such: char* tt = new char[1000000000]; // in this string I have an exception. mMainWnd = CreateWindowEx( style_ex, (LPCTSTR)RegisterClassEx(&wcex), NULL, style, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, mInstance, NULL); I have no problems!! Help me please.

Share this post


Link to post
Share on other sites
Allocating 1GB of memory is very likely to fail - you don't need 1GB of free memory, you need 1GB of contiguous address space, the OS will handle swapping pages in and out of RAM as needed. The problem is that calling CreateWindow() probably causes some DLLs to be loaded, and they fragment your address space.

The only sensible things to do would be to allocate a much smaller chunk of memory (No more than a couple of hundred MB), use memory mapped files with a smaller view (if you need to load in massive files), or switch to a 64-bit build (And OS, and CPU).

Share this post


Link to post
Share on other sites
Why in the blazes would ever need to allocate one GIGABYTE of chars?

Anyway, the error occurs because you either don't have one gigabyte of free memory (check your task manager or equivalent), or because you don't have one gigabyte of contiguous free memory. Either case will cause your allocation to fail.

But you almost certainly don't need to allocate a gigabyte of chars. What exactly are you trying to accomplish? Maybe we can propose a better solution.

Share this post


Link to post
Share on other sites
My real problem was, that I couldn't allocate mem in runtime, I have analogous exception. But in this time I allocated about 40 mb of mem in the heap. So I tryied to manually allocate a huge amount of mem, from begining of the program and I see the place where I have such an exception

Share this post


Link to post
Share on other sites
You are not right. I HAVE 1,5 gb of memory and I say that if i write sych code, everything will be ok:


char* tt = new char[1000000000]; // in this string I have an exception.

mMainWnd = CreateWindowEx(
style_ex,
(LPCTSTR)RegisterClassEx(&wcex),
NULL,
style,
rect.left,
rect.top,
rect.right - rect.left,
rect.bottom - rect.top,
NULL,
NULL,
mInstance,
NULL);

Share this post


Link to post
Share on other sites
If you have a sufficient amount of free memory, then you don't have a sufficient amount of free contiguous memory as Evil Steve and myself previously pointed out. As Steve said, loading a window probably loads certain DLL's which fragment your memory.

Share this post


Link to post
Share on other sites
Quote:
Original post by kitkit
You are not right. I HAVE 1,5 gb of memory and I say that if i write sych code, everything will be ok:


char* tt = new char[1000000000]; // in this string I have an exception.

mMainWnd = CreateWindowEx(
style_ex,
(LPCTSTR)RegisterClassEx(&wcex),
NULL,
style,
rect.left,
rect.top,
rect.right - rect.left,
rect.bottom - rect.top,
NULL,
NULL,
mInstance,
NULL);
Yes, because the DLLs can't load at their preferred address because the address is in use. Windows can relocate DLLs to a specific address, which is what will be happening here.

As a test, look at your debug output and see what DLLs are loaded when you call CreateWindow(). Then use GetModuleHandle() on each of those DLLs in turn and print out the return value (Which is the base address for that DLL). You'll see that if you allocate 1GB of memory before calling CreateWindow, the addresses are different because of relocation.

Share this post


Link to post
Share on other sites
Also, just because you have 1.5 GB of memory does not mean that all of that is available for user space applications. A portion of your memory is devoted to kernel space applications.

Share this post


Link to post
Share on other sites
Quote:
Original post by glaeken
Also, just because you have 1.5 GB of memory does not mean that all of that is available for user space applications. A portion of your memory is devoted to kernel space applications.
Not exactly; a portion of the address space is. Anything above 0x80000000 is reserved for the kernel which is the important thing. You could have 256MB of RAM and allocate a 1GB chunk of memory without any problems (Apart from it'll be slow as hell [smile])

Share this post


Link to post
Share on other sites
You, know, may be you are right, but I returned to old problem when I try to allocate 40mb. At this moment I have about 1.3gb free but, when I try to allocate 40mb I have old error :( . BUT !! when I, at this moment try to allocate four times by ten mb I have no error!!! I know, you should tell me that I have memory fragmentation. But I have 1.3gb free!!!!!!!!!!!! what can you say ?

Share this post


Link to post
Share on other sites
Quote:
Original post by kitkit
You, know, may be you are right, but I returned to old problem when I try to allocate 40mb. At this moment I have about 1.3gb free but, when I try to allocate 40mb I have old error :( . BUT !! when I, at this moment try to allocate four times by ten mb I have no error!!! I know, you should tell me that I have memory fragmentation. But I have 1.3gb free!!!!!!!!!!!! what can you say ?
How do you know you have 1.3GB free? What if you call VirtualAlloc(NULL, 40*1024*1024, MEM_COMMIT | MEM_RESERVE, 0);? That's the lowest level allocation function you can possibly get. If it returns null, then you simply don't have enough free unfragmented memory.

It's also possible that you have heap corruption going on, which could break the CRT allocator and cause allocations to fail.

Share this post


Link to post
Share on other sites
Evil Steve , thanks you! I can say that with this method I have no warnings, leaks or errors. Now my app works correctly.

What you can say now? Why didn't works it before?? Can you say?

Share this post


Link to post
Share on other sites
Quote:
Original post by kitkit
Evil Steve , thanks you! I can say that with this method I have no warnings, leaks or errors. Now my app works correctly.

What you can say now? Why didn't works it before?? Can you say?
That's odd... I'd avoid using VirtualAlloc if possible, it's a bit too low level to be using normally. It looks like heap corruption to me, but I could be wrong. Basically, I don't know [smile]

Share this post


Link to post
Share on other sites
Let's continue our discussion. I have founded that real exception throws on string

_munlock(_HEAP_LOCK);

in file

dbgheap.c


in method

void * __cdecl _nh_malloc_dbg

Can you help me again ??

Share this post


Link to post
Share on other sites
I seem to recall that's the internal function that actually does the memory allocation. If you trace into the memory allocation call, you should find out the reason it fails (E.g. VirtualAlloc fails or something). I'll try doing this myself at my lunch break (2 hours time).

Share this post


Link to post
Share on other sites
May I write "call stack" output to this forum ?


kernel32.dll!7c812aeb()
kernel32.dll!7c812aeb()
msvcr71d.dll!_nh_malloc_dbg(unsigned int nSize=1, int nhFlag=3, int nBlockUse=14466340, const char * szFileName=0x00dcbe9c, int nLine=14466728) Line 267 + 0x7 C
msvcr71d.dll!_CxxThrowException(void * pExceptionObject=0x00dcbd40, const _s__ThrowInfo * pThrowInfo=0x104f6150) + 0x39 C++
msvcp71d.dll!std::_Nomemory() Line 10 C++
test_appD.exe!operator new(unsigned int size=40981387) Line 15 C++
> test_appD.exe!operator new[](unsigned int count=40981387) Line 7 + 0x9 C++

Share this post


Link to post
Share on other sites
You'll need the CRT source code to trace into the CRT functions. I'm not sure if it comes with the express version of Visual Studio, I'm pretty sure it does with the professional (I'll look at lunchtime).

Share this post


Link to post
Share on other sites
So, ...

during call operator new

void *__cdecl operator new(size_t size) _THROW1(_STD bad_alloc)
{ // try to allocate size bytes
void *p;
while ((p = malloc(size)) == 0)
if (_callnewh(size) == 0)
_STD _Nomemory();
return (p);
}

in when there is NO error, function malloc returns not zero value.

when there is error, function malloc return 0 and error code (GetLastError) is 8 - no memory. then method _callnewh - returns 0, and then we call _STD _Nomemory();

WHY ??? :)

Share this post


Link to post
Share on other sites
Quote:
Original post by kitkit
in when there is NO error, function malloc returns not zero value.

when there is error, function malloc return 0 and error code (GetLastError) is 8 - no memory. then method _callnewh - returns 0, and then we call _STD _Nomemory();

WHY ??? :)
You'll need to trace right into the CRT code, down to where it calls the OS functions like VirtualAlloc to find that out. If the code never calls an OS function, then it's not even requesting more memory from the OS for some reason.

Share this post


Link to post
Share on other sites
Ok, the CRT memory allocation functions use HeapAlloc() rather than VirtualAlloc for their allocations. It seems that when trying to allocate a 1000MB chunk of memory after creating a window, the function fails, where VirtualAlloc succeeds. I was able to allocate 500MB of memory without any problems though.

Share this post


Link to post
Share on other sites

This topic is 3458 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this