LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

what are valid values for a bitmapid ?

Hello all,

Question: what are valid values for a bitmapid ?


I am using CVI 6
I am building a programme with many calls to GetCtrlDisplayBitmap(panelid, controlid, 0, &bitmapid);
This call is part of some preparation that also includes several memory allocations (malloc).
So the organize the cleanup, I initialize variables to a non-valid value, and check these variables during cleanup. The original erroneous code is shown below.
=========================================================
int status;
int bitmapid = -1;
unsigned char *allbits = NULL, *dbit=NULL;
... ...
... ...
status = GetCtrlDisplayBitmap(panelid, controlid, 0, &bitmapid);
if (status < 0) goto CleanUpMemory;
status = AllocBitmapData (bitmapid, NULL, &dbit, NULL);
if (status < 0) goto CleanUpMemory;
allbits = malloc(allsize);
if (allbits == NULL) goto CleanUpMemory;
... ...
... ...
CleanUpMemory:
if (dbit != NULL) free (dbit);
if (allbits != NULL) free (allbits);
if (bitmapid <= 0) DiscardBitmap (bitmapid);
=========================================================

When I noticed a memory leak, I searched for the cause.
I found large negative values for the bitmapid e.g. -366280272
So I repaired this code with
int bitmapid = 0;
if (bitmapid != 0) DiscardBitmap(bitmapid);
But this is only valid if 1) a valid bitmapid is never zero, and 2) GetCtrlDisplayBitmap does not change this value to a random number when an error occurs.

So my question is: are there values for a bitmapid that never occurs, so that I can use this as a check if freeing memory is needed ?

Regards, Jos
0 Kudos
Message 1 of 4
(3,401 Views)
Sorry about an important typo in the source above. The source code - that got rejected - is of course:
if (bitmapid >= 0) DiscardBitmap (bitmapid);


Regards, Jos
Message 2 of 4
(3,397 Views)
I think in practice the bitmap ID will always be a large negative number, however checking against zero is fine and I would say the correct thing to do. Zero will never be a valid bitmap ID.

Regards,

-alex
Message 3 of 4
(3,387 Views)
Jos,

Large negative values are normal for a bitmap id, if you are using signed integers. It will be more meaningful if you store the id in an unsigned integer, and also if you view it as a hexadecimal number.

In any case, you're safe in assuming that a valid bitmap id cannot be 0. So you should feel free to initialize your variable to 0, and then discard it if it is anything other than 0.

Luis
NI
0 Kudos
Message 4 of 4
(3,386 Views)