LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

GetBitmapFromFile out of memory

Solved!
Go to solution

Attached is a program to test GetBitmapFromFile which usually (sometimes consistently) fails with "out of memory" error code -12.

The failure depends on the size of the .JPG file that's being converted.  Small files (under 300K) usually work, but bigger files (>300K) often fail.

The demonstration of the "bug" starts  near line 169 in main.c.

Notice that after GetBitmapFromFile fails, I can malloc 200Mb of memory with no problems.

Often on clean machines the program will run the first time, but fail the next time it runs.

The short program has the user browse for .JPG files then shows them as thumbnails in a table.

After showing the files with the program still running, browse again for the same files and the error is sure to occur.

And yes, I'm discarding the bitmap every time - Compiled with CVI 9.1

Robin Knoke
CVI Developer since 1994
0 Kudos
Message 1 of 14
(4,336 Views)

I can't offer a solution, but I did come across a similar issue which may help to put a different perspective on the situation. I too had a problem with running out of memory unexpectedly when handling large bitmaps. This was with CVI 7.1, so it may well be the case that the newer CVIs do things differently, but as a result of my investigations at the time I found that the CVI libraries seemed to be requesting contiguous kernel memory from the PC (possibly indirectly, via other Windows kernel graphics calls), instead of plain user memory. This type of kernel memory is in much shorter supply of course and leads to exactly the kind of apparantly conflicting results you are seeing.

 

I don't know if CVI now uses Windows gdi+ but it seems that the older gdi based functions had this odd effect - maybe for compatibility with some graphics cards? I never found a practical solution, apart from ensuring the PC had recently been rebooted before running the program - this gave it the highest probability of still having unfragmented memory areas available.

 

JR

0 Kudos
Message 2 of 14
(4,306 Views)

Thanks JR.  I had read your post but it didn't solve my problem. I am hoping someone from NI will take a look at the test program I attached and could perhaps remedy the situation with a new function.  My current work-around is to resize the JPEG's down to <300K prior to displaying them, but often even that doesn't solve the problem.  Also, JPEG resizing takes time and slows the application. I can't expect my client's customers to reboot the computer every time they run the program, so I'm temporarily stuck.

Robin Knoke
CVI Developer since 1994
0 Kudos
Message 3 of 14
(4,292 Views)

Hi Robin,

 

I actually downloaded your zip file and ran the code as-is. I downloaded some images that were in excess of 300KB, and they added fine. I added a file that was 693 kb, then added one that was 342kb, then added the 693 kb image again. No permutation of this seemed to break the program, and the thumbnails were all visible in the Available Pictures box. You could try to run this on another machine and see if it is working there.

Best regards,
Rohan B
0 Kudos
Message 4 of 14
(4,276 Views)

Rohan - Could you try some images that are 1 or 2 Mb? Maybe 7 or 8 of them, then load them all again?  It's an elusive bug but that usually breaks it.  How much physical memory does your system have? Thanks for taking an interest.

Robin Knoke
CVI Developer since 1994
0 Kudos
Message 5 of 14
(4,270 Views)

Hi Robin,

 

I downloaded a 3MB and a 500kB image and could load them 8 times. Then I cleared the box, and reloaded them 12 times (loading both at the same time 6 different times). I have a machine with 2GB RAM, ~600MB available at run time. Have you been able to try this on another machine?

Best regards,
Rohan B
0 Kudos
Message 6 of 14
(4,250 Views)

Hey Robin - 

 

Just curious - what OS are you using?  What are the memory specs on your video card?

 

NickB

National Instruments

0 Kudos
Message 7 of 14
(4,244 Views)

Nick:

This has been an elusive bug, sometimes it doesn't present itself for awhile, then suddenly I stop getting valid bitmap images, sometimes even the first one.  As for the information you requested, my development machine iis running XP Pro on Intel Quad9550 at 2.8Gz.  2GB memory. I recently updated to CVI 9.1, but I don't think that matters. The two video cards are each ASUS Radeon EAH4350, 512Mb and each drives two screens at 1920x1080.  The function fails even though vmmap shows 1Gb physical RAM still available.  Do you suppose the heap is fragmented?

I have also run the application on an older XP pro machine as well as a Shuttle running Win-7 home ed. - All exhibited similar results.  Has anyone else been able to duplicate the problem or is it just weird out here?

The Win-7 machine was 'clean' and loaded 41 pictures that were 2.5Mb each before failing on pictures 42, 43 and 44. Then I added 4 smaller pictures (88Kb) with no problems. The Win-7 seemed to work much better that the other machines tested and with only 757Mb available RAM, and the perf-meter showing 625Mb used, any missing pictures could be a different problem - after all, the test program attempts to malloc/free 200Mb as part of it's testing.  

Note, DebugPrintf( ) is called if the GetBitmapFromFile function fails, but of course doesn't show in release mode.

Robin Knoke
CVI Developer since 1994
0 Kudos
Message 8 of 14
(4,219 Views)

Hey Robin -

 

That's interesting that you see the issue on both Windows 7 and Windows XP.  What you are seeing is *somewhat* expected on Windows XP, but surprising on Windows 7.  The Win32 api that we use internally for bitmaps actually allocates memory directly from the video card in Windows XP, and so you are limited to contiguous memory available on the memory card.  However, with the introduction of the DWM (desktop window manager) in Vista, this allocation no longer takes place on the video card memory, but instead in user mode virtual memory (as long as DWM is enabled of course).  

 

Is there any chance you could post a small example that recreates what you're seeing?  I've tried to reproduce what you're seeing as Rohan did, but was unable to see the same error at memory levels comparable to what you're describing.

 

Thanks -

 

NickB

National Instruments

0 Kudos
Message 9 of 14
(4,205 Views)
Solution
Accepted by topic author WhiteSalmon

Nick:

Ok, it's starting to make sense!  The Windows-7 machine did seem to work a lot better and I had to really push it to get it to fail - and like I mentioned, it only had 767Mb and the way I was banging on it, it probably just ran out of memory.  This makes me feel a lot better since my client is going to be running my app on Win-7.

Now that I understand that the memory is allocated from the video card in XP, everything is startting to fit the pattern I have seen.  With that in mind, I'll do some testing and get back to you.

Is it possible to get GetBitmapFromFile modified to always use main memory?  Is the souce avail?

Robin Knoke
CVI Developer since 1994
0 Kudos
Message 10 of 14
(4,198 Views)