Link to home
Start Free TrialLog in
Avatar of kushcu
kushcu

asked on

visual c++ memory limitations?

I'm trying to make a simple 2D game, and the program worked fine so far. I use full screen in 16 bits, with 4 different sprites. (and some 20 ships on screen) I checked RAM usage before, and the program uses 1 MB of memory (always).

Now I want to use double-buffering, and therefore I tried to create a back screen buffer (using win32 api only) with:
  hbmBuffer = CreateCompatibleBitmap(m_hdc, rect.right, rect.bottom);

This function call fails (returns a NULL pointer) due to unavailable memory. This call requires -at the most- 1.2 MB RAM, I have plenty more. I believe it's due to a setting problem related to Visual C++ 6. What should I change?
Avatar of snoegler
snoegler

Can you post the values of 'rect.right' and 'rect.bottom' here? (Maybe also rect.left and rect.top)
Avatar of jkr
And as you are posting that, what does 'GetLastError()' return after the call has failed?
Its not a RAM limitation. Visual C++ Will allow you to allocate as much ram as you have process address space (providing you have the physical ram or pagefile space). I have in one applciation allocated ~1.9GB of memory (in more than one chunk) with no problems.
I'm not sure, but maybe it's trying to allocate video ram.  If that's the case it might explain why it's failing.

Mithander
ASKER CERTIFIED SOLUTION
Avatar of MDarling
MDarling

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Might it be a stack error instead? this is small by default but can easily be increased in project settings...

also, memory allocations must be contigious, so if your ram is fragged, (Nt, xp, 2k) it can fail...  but would have to be badly fragged if you can't get a meg or 2.

Avatar of kushcu

ASKER

rect.left = 0, rect.top = 0, rect.bottom = 694, rect.right = 1016 (these are the maximum client area sizes defined for my monitor by Windows)

hdc is obtained by GetDC(hwnd) and normally works fine. (I draw 20 alienships, 1 humanship and some missiles on screen with it.)

I get the error number by:
  DWORD error = GetLastError();
which returns 8 as a result, defined as not enough memory in MSDN.

Since I'm using native Windows API, no video memory is involved.
I assume that it is not the actual memory that this error code refers to, but to GDI resources running out - are you releasing all GDI resources properly?
Avatar of kushcu

ASKER

OK, I get the error. I've gotta to get some sleep, I called CreateCompatibleBitmap() in between SaveDC() - RestoreDC(). (for some other reason)
Confused...

I get a B and I'm not sure I even answered a question...

Suspect he was calling GetDC() without ReleaseDC() somewhere and he was running low on GDI resources?
Avatar of kushcu

ASKER

you just made me check how I obtained the handle, and suddenly my code starting working. I gave a B because the answer wasn't a direct one. (Honestly, I didn't expect a direct answer. I post as small code fragments as possible to keep the experts interested.)
Just by checking? sounds a bit like quantum physics to me :-)

Well I'm glad it's working and I helped in some way.

Regards,
Mike.
you may have re-located the memory error. check again, or it may come back later...  it does not sound like you fixed it (even if it now works)