-
CreateCompatibleBitmap failed
Hello,
I has a problem with CreateCompatibleBitmap.
the usage in short:
------------
CBitmap bm;
CClientDC dc(NULL);
bm.CreateCompatibleBitmap(&dc,4096,4096);
-----------
If I understand it right, it needs 4096*4096*3= approx. 50MB of video memory.
Is this correct?
For my current project it is used once in the app. I has to restart the app many times. No problem half of the day.
But then I get the error 8: "insufficient memory". It can only resolved if I restart the PC.
Is the problem in my app?
Does win XP grabs memory and do not release it?
Is it really the video memory?
Can I check the free video memory?
Thx for any helps!
Ralf
-
Re: CreateCompatibleBitmap failed
Quote:
Originally Posted by
Ralf Schneider
Is the problem in my app?
Does win XP grabs memory and do not release it?
Is it really the video memory?
Can I check the free video memory?
Do any of your other applications show a problem? I bet they don't, so the problem is with your application.
Also, posting those 3 lines of code tells us absolutely nothing except the parameters you're using to call the function. How would those 3 lines look if your application was working correctly? There would be no difference.
Unless we have your entire application, run it, and see the behaviour, then no one will be able to tell you exactly what you're doing wrong. More then likely you are allocating resources and never releasing them.
Regards,
Paul McKenzie
-
Re: CreateCompatibleBitmap failed
Hello Paul,
do win XP not release all the memory used by my app, when I terminate it?
Ralf
-
Re: CreateCompatibleBitmap failed
Of course it does.
Quote:
But then I get the error 8: "insufficient memory". It can only resolved if I restart the PC.
Did you try it on some other XP system? It might be a video driver problem.
-
Re: CreateCompatibleBitmap failed
Possibly your program leaks resources. Check this with Task Manager. See whether Handle Count, GDI Objects and Memory Usage are constantly growing when the program is running. To see all these counters, use View - Select Columns dialog in the Task Manager.
-
Re: CreateCompatibleBitmap failed
Hello Alex,
how can I see all these counters in the task manager on win xp?
I do not find it!:o
Please help.
-
Re: CreateCompatibleBitmap failed
Quote:
Originally Posted by
Ralf Schneider
Hello Alex,
how can I see all these counters in the task manager on win xp?
I do not find it!:o
Please help.
View -> Select Columns
Regards,
Paul McKenzie
-
Re: CreateCompatibleBitmap failed
Hello,
the view menu of win xp has not such an item....
-
Re: CreateCompatibleBitmap failed
Quote:
Originally Posted by
Alex F
Possibly your program leaks resources. Check this with Task Manager. See whether Handle Count, GDI Objects and Memory Usage are constantly growing when the program is running. To see all these counters, use View - Select Columns dialog in the Task Manager.
Hello Alex,
you mean, that these resources will not be released automtically when the app is terminated???
What could it be that interfered with resources used by CreateCompatibleBitmap?
Ralf
-
Re: CreateCompatibleBitmap failed
Quote:
Originally Posted by
Ralf Schneider
Hello Alex,
you mean, that these resources will not be released automtically when the app is terminated???
Fix your application, then you don't need to worry about this.
Regards,
Paul McKenzie
-
Re: CreateCompatibleBitmap failed
Quote:
Originally Posted by
Ralf Schneider
Hello,
the view menu of win xp has not such an item....
I have no idea what you mean by "view menu of win xp" but View menu of Task Manager in Win XP does have this item. :cool:
-
Re: CreateCompatibleBitmap failed
Of course, all resources are released when application is terminated. But if your application allocates some resources (like GDI objects) all the time without releasing them, finally some of these allocations fails. This is called resource leak. So, learn your application behavior with Task Manager.
For example, you see that your application consumes 50 GDI objects. 1 hour later - 70 objects, several hours later - 200 objects etc. This is resource leak.
The same applies to the memory, handles, threads etc. - actually, all types of resources.
-
Re: CreateCompatibleBitmap failed
Hello Alex,
ok, I will check it.
But the Task Manager View-menu has only these items:
(translated from German)
- Update
- Speed for Update
- CPU-history
- Show kernal-times
?????
thx.
Ralf
-
Re: CreateCompatibleBitmap failed
Quote:
Originally Posted by
Ralf Schneider
Hello Alex,
ok, I will check it.
But the Task Manager View-menu has only these items:
(translated from German)
- Update
- Speed for Update
- CPU-history
- Show kernal-times
?????
thx.
Ralf
My version of XP Task Manager certainly does have the "Select Columns..." option in the View Menu. If you don't believe me, do a google search and you will find that it does exist.
Regards,
Paul McKenzie
-
Re: CreateCompatibleBitmap failed
Hello Paul,
of course, I do believe you.
But that do not help me....
-
Re: CreateCompatibleBitmap failed
Quote:
Originally Posted by
Ralf Schneider
Hello Paul,
of course, I do believe you.
But that do not help me....
Perhaps you should just go on the assumption that that's the problem and check that you're releasing all your GDI objects when you're done with them.
-
Re: CreateCompatibleBitmap failed
Quote:
Originally Posted by Ralf Schneider
I has to restart the app many times. No problem half of the day.
Quote:
Originally Posted by Alex F
Possibly your program leaks resources. Check this with Task Manager.
Even if the app leaks any, restarting the app unconditionally releases the ones. There must be something else.
-
Re: CreateCompatibleBitmap failed
Quote:
Originally Posted by
Ralf Schneider
Hello Paul,
of course, I do believe you.
But that do not help me....
Processes tab -> Options -> Select Columns
Do you now see this option?
Regards,
Paul McKenzie
-
Re: CreateCompatibleBitmap failed
more likely than not, your compatible bitmap will be 4bytes per pixel instead of the 3 you're assuming. Thus resulting in a 67Mb bitmap.
Countrary to your first post, this memory is not usually allocated in videomemory but in regular systemmemory.
It is entirely possible that while you have enough free memory still available, you no longer have a continguous block of that size available, causing the function to fail.
If you must allocate large chunks of memory it is recommended you either allocate these as early in your program as possible to minimize effects of memory fragmentation.
Alternatively, you could also reserve a large chunk of memory up front and do your own managing of the block of memory.
If you only need to make a bitmap once, and it's size will never change. Make it as early as you possibly can, and try not to destroy/recreate it.
Remember also that the bitmap is allocated by the OS. If you have previously allocated a lot of your own memory via new/malloc, that memory is still reserved for use by your applications heap and isn't available for the OS even if it has a sufficiently large enough chunk of free memory.
Alternatively build your app as a 64bit app, large memory block exhaustion by fragmentation is almost nonexistant in Win64. (or you're making some pretty funky programs :p)
-
Re: CreateCompatibleBitmap failed
Well, you need to find some program with such functionality, or install another Windows version. Maybe Home edition doesn't support this, and Professional does. You can install another Windows version as virtual computer, for example, using free VirtualBox.
I would ask in different forums about programming tools, that can provide such information about running process. For example, try Process Explorer: http://technet.microsoft.com/en-us/s...rnals/bb896653 I am not sure that it shows all required counters, anyway, you need a tool like this.
-
Re: CreateCompatibleBitmap failed
Hello,
thx for all the hints.
The following I forgot to say:
after failure of
------------
CBitmap bm;
CClientDC dc(NULL);
bm.CreateCompatibleBitmap(&dc,4096,4096);
-----------
I use 2048 instead of 4096. I never had a problem with this.
But this size is too small. But it helps me not to reboot always.
May this information helps to find the cause of my problem....
Ralf
-
Re: CreateCompatibleBitmap failed
Okay, why don't you try to create it only once and reuse during your paints?
-
Re: CreateCompatibleBitmap failed
Hello Igor, and @all
I create it only once in the app.
But not at the beginning.
I create it at the time I need it. Then do my drawings and delete the bitmap.
Then the released memory can be used for other stuff.
I do not know, if it is a good idea to allocate the memory for the bitmap at the beginning. In this case I reserve memory that can not used for other things until it is really needed for the bitmap drawings.
I think, the basic thing for me at the moment is to know, what cause the CreateCompatibleBitmap-function to fail with 4096 (and not with 2048).
Could this be handles, that are not available. Or it is video memory or is it heap memory or fragmented memory.
Any idea to get the answer?
I think, handles are not the problem?
thx.
Ralf
-
Re: CreateCompatibleBitmap failed
update:
I think, CreateCompatibleBitmap uses video memory:
I uses Process explorer (linked above) to check the memory consumption.
And there is no changes in the amount of "physical memory - working set" for and after the CreateCompatibleBitmap call.
What can I do?
Ralf
-
Re: CreateCompatibleBitmap failed
Quote:
Originally Posted by
Ralf Schneider
Hello Igor, and @all
I create it only once in the app.
But not at the beginning.
I guess I will have to be the one to say this:
We don't know what your app is really doing. You just saying what it does is not enough information.
There have been many posts by persons who claim "I'm doing this and doing that", and when it comes time to actually see the code, they are not doing what they say they're doing, or at the very least, they're doing things incorrectly.
Sorry to be harsh, but unless we see actual code (not just three lines), we have no way to verify what you're saying is true. We could be ending up on a wild goose chase, when all that really is a problem is a bug in your program you haven't diagnosed.
If the problem is really those 3 lines you are posting, why not create a very simple, Win32 or MFC application that performs those operations correctly without any sign whatsoever of memory/GDI leakage of handles? You don't even need to draw anything -- just create a simple app that calls those functions in a correct, consistent manner.
That is what any programmer would do at this stage. If it is a video card/memory issue, then the simple application would show the very same issue. If it shows the same issue, then post that project here. This is much better then guessing what is the problem.
Regards,
Paul McKenzie
-
Re: CreateCompatibleBitmap failed
Note that I did say that memory for createcompatiblebitmap is not usually allocated in videomemory, there are some specific cases where this IS indeed happening in the videomemory. If your test for memory consumtion is done right, it may indeed indicate you have one of those cases where memory is being reserved on the videocard.
Windows does this in specific cases to allow the video accellerator to do it's magical stuff. That also means that memory allocated in this way must fit into the videocards memory and must also deal with all the nasties associated with it.
Allocating 67Mb on your videocard is a big deal. You already are using some of it for what you actually see on screen. Windows is already using some of it to cache important and frequently used system resources. The glass effect used in Vista/7 among others is a done with a bitmap stored in videomemory.
Thumbnails in the taskbar also temporarily use videomemory. Those things use, and eventuelly fragment to some degree the available memory for additional allocations.
Long story short, if you need a bitmap as large as 4k by 4k in truecolor... You really should be using DIB's instead of DDB's.
Have a look at CreateDIBSection() instead.
-
Re: CreateCompatibleBitmap failed
Hello OReubens,
I tried to use CreateDIBSection also.
But there were also some problems.
AFAI remember a DIBSection can not be selected into the DC (SelectObject works not for DIBs) and then a drawing into the dib is not possible.
See my attempt:
http://forums.codeguru.com/showthread.php?t=523980
Ralf
-
Re: CreateCompatibleBitmap failed
Quote:
Originally Posted by
Ralf Schneider
Hello OReubens,
I tried to use CreateDIBSection also.
But there were also some problems.
AFAI remember a DIBSection can not be selected into the DC (SelectObject works not for DIBs) and then a drawing into the dib is not possible.
See my attempt:
http://forums.codeguru.com/showthread.php?t=523980
Ralf
Even that link demonstrated you're doing things incorrectly.
The best bet is for you to create a simple application as I stated that shows the error. Posting 3 line snippets as you're doing in this thread, or faulty code as you are doing in the thread above, doesn't get closer into finding out exactly what you're doing.
Regards,
Paul McKenzie
-
Re: CreateCompatibleBitmap failed
Quote:
Originally Posted by
Paul McKenzie
Even that link demonstrated you're doing things incorrectly.
Hello Paul,
I know, that you want to help me. Please tell me, are you familiar with the DIBSection stuff?
AFAIK: the link demonstrates that the approach with the DIBSection do not work. Because there is not Bitmap selected into the DC. And this leads to an access violation.
Ralf
-
Re: CreateCompatibleBitmap failed
Quote:
Originally Posted by
Ralf Schneider
AFAIK: the link demonstrates that the approach with the DIBSection do not work. Because there is not Bitmap selected into the DC. And this leads to an access violation.
DIB actually can be selected into DC alright. But its attributes should comply with the manner of drawing/blitting to DC. For example, in case map mode is MM_TEXT, the DIB should be top-down DIB. :)
Besides, the DC should be properly freed. I met a remark that using ReleaseDC instead of DeleteDC can cause an access violation.
-
Re: CreateCompatibleBitmap failed
Quote:
Originally Posted by
Igor Vartanov
DIB actually can be selected into DC alright. But its attributes should comply with the manner of drawing/blitting to DC. For example, in case map mode is MM_TEXT, the DIB should be top-down DIB. :)
Besides, the DC should be properly freed. I met a remark that using ReleaseDC instead of DeleteDC can cause an access violation.
Hello Igor,
can you see that there is something wrong in the code snippet written in the above link (http://forums.codeguru.com/showthread.php?t=523980)?
Ralf
-
Re: CreateCompatibleBitmap failed
Quote:
Originally Posted by
Ralf Schneider
MikeAThon already pointed out what's wrong. No one knows what are those "tXXXX" types are.
Secondly, and again, instead of snippets, how about a small program that shows the error? How hard is it to create a tiny but full program that demonstrates the error? If the problem are those 3 lines you posted, then it can't be that difficult to write a program that uses those 3 lines, right? Shouldn't that be your next move, instead of us having to guess what the issue is?
Regards,
Paul McKenzie