LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to detect memory leaks

I just installed CVI 5.5, and discovered that the "Dynamic Memory"
dialog is not in the "Run" menu anymore.

Is there another mean to track the dynamic memory usage of my program?

i.e. I may have a loop where memory is allocated but not freeed. How do
I detect it ? (before the system crashes...)


Thanks,

Denis LIZOTTE
EMS Technologies Canada
0 Kudos
Message 1 of 4
(4,048 Views)
Denis,

You can use the function CVIDynamicMemoryInfo to check your dynamic
memory usage. This function was added in CVI 5.5 patch 1. If you're
using CVI 5.5.1 you can use it directly, it's in the utility
library. Otherwise you'll have to use the helper function appended
below.

The function prints the number of memory blocks allocated and the
total amount of allocated memory in CVI's Debug Output Window. It
takes four arguments: an optional message to help you associate the
output with a location in your code, optional values which hold the
number of allocated blocks and memory on output, and an options field
which allows you to specify if you want additional information to be
printed about the memory blocks.

Depending on how much memory you have allocated this function can take
quite a long time to run (a few seconds) though.

Hope this helps,

Peter

/*************************************/

#include
#include
#include


#define SHOW_ALLOCATED_MEMORY 0x80000000

typedef int (CVIFUNC *TDynMemInfo)(char *message,
unsigned int *numAllocBlocks,
unsigned int *numAllocBytes,
unsigned int options);

int MyGetDynamicMemInfo (char *message,
unsigned int *numBlocks,
unsigned int *numBytes,
unsigned int options)
{
HMODULE dll = GetModuleHandle ("cvirte.dll");
TDynMemInfo dmi = (TDynMemInfo)GetProcAddress (dll, "CVIDynamicMemoryInfo");

if (dmi)
return dmi (message, numBlocks, numBytes, SHOW_ALLOCATED_MEMORY);
/* check debug output window for memory information */
/* note: this function may take a few seconds to execute */
/* if many blocks were allocated. */
else
return -1;
}


int main (int argc, char *argv[])
{
unsigned int numAllocatedBlocks, numAllocatedBytes;
void *memory = malloc (1234);

MyGetDynamicMemInfo ("Some Message",
&numAllocatedBlocks,
&numAllocatedBytes,
SHOW_ALLOCATED_MEMORY);

free (memory);

return 0;
}

/*************************************/

Denis Lizotte writes:
> I just installed CVI 5.5, and discovered that the "Dynamic Memory"
> dialog is not in the "Run" menu anymore.
>
> Is there another mean to track the dynamic memory usage of my program?
>
> i.e. I may have a loop where memory is allocated but not freeed. How do
> I detect it ? (before the system crashes...)
>
>
> Thanks,
>
> Denis LIZOTTE
> EMS Technologies Canada
0 Kudos
Message 2 of 4
(4,048 Views)
Hi,

I didn't find the CVIDynamicMemoryInfo in the libs as you tell (and I'm in CVI
5.5p1)
so I implement the function you include and it works well.
But with the otions (SHOW_ALLOCATED_MEMORY), is it possible to tune it just to
have the allocated blocks and bytes and not the complete dump of the allocated
memory ?

Thanks for your help,
Fred.

Peter Ilberg a �crit :

> Denis,
>
> You can use the function CVIDynamicMemoryInfo to check your dynamic
> memory usage. This function was added in CVI 5.5 patch 1. If you're
> using CVI 5.5.1 you can use it directly, it's in the utility
> library. Otherwise you'll have to use the helper function appended
> below.
>
> The function prints the number of memory blocks allocated and the
> total amount of allocated memory in CVI's Debug Output Window. It
> takes four arguments: an optional message to help you associate the
> output with a location in your code, optional values which hold the
> number of allocated blocks and memory on output, and an options field
> which allows you to specify if you want additional information to be
> printed about the memory blocks.
>
> Depending on how much memory you have allocated this function can take
> quite a long time to run (a few seconds) though.
>
> Hope this helps,
>
> Peter
>
> /*************************************/
>
> #include
> #include
> #include
>
> #define SHOW_ALLOCATED_MEMORY 0x80000000
>
> typedef int (CVIFUNC *TDynMemInfo)(char *message,
> unsigned int *numAllocBlocks,
> unsigned int *numAllocBytes,
> unsigned int options);
>
> int MyGetDynamicMemInfo (char *message,
> unsigned int *numBlocks,
> unsigned int *numBytes,
> unsigned int options)
> {
> HMODULE dll = GetModuleHandle ("cvirte.dll");
> TDynMemInfo dmi = (TDynMemInfo)GetProcAddress (dll, "CVIDynamicMemoryInfo");
>
> if (dmi)
> return dmi (message, numBlocks, numBytes, SHOW_ALLOCATED_MEMORY);
> /* check debug output window for memory information */
> /* note: this function may take a few seconds to execute */
> /* if many blocks were allocated. */
> else
> return -1;
> }
>
> int main (int argc, char *argv[])
> {
> unsigned int numAllocatedBlocks, numAllocatedBytes;
> void *memory = malloc (1234);
>
> MyGetDynamicMemInfo ("Some Message",
> &numAllocatedBlocks,
> &numAllocatedBytes,
> SHOW_ALLOCATED_MEMORY);
>
> free (memory);
>
> return 0;
> }
>
0 Kudos
Message 3 of 4
(4,048 Views)
Hello Fr�d�ric,

You could use this function:

void AnotherDynamicMemoryInfo(char *message)
{
int numBlocks, numBytes;

/* or use MyGetDynamicMemInfo */
CVIDynamicMemoryInfo(NULL, &numBlocks, &numBytes, 0);
DebugPrintf("%s\n"
"Number of blocks allocated: %d\n"
"Number of bytes allocated: %d\n",
message, numBlocks, numBytes);
}

Peter

Fr�d�ric MAILLET writes:
> Hi,
>
> I didn't find the CVIDynamicMemoryInfo in the libs as you tell (and I'm in CVI
> 5.5p1)
> so I implement the function you include and it works well.
> But with the otions (SHOW_ALLOCATED_MEMORY), is it possible to tune it just to
> have the allocated blocks and bytes and not the complete dump of the allocated
> memory ?
>
> Thanks for your help,
> Fred.
>
> Peter Ilberg a �crit :
>
> > Denis,
> >
> > You can use the function CVIDynamicMemoryInfo to check your dynamic
> > memory usage. This function was added in CVI 5.5 patch 1. If you're
> > using CVI 5.5.1 you can use it directly, it's in the utility
> > library. Otherwise you'll have to use the helper function appended
> > below.
> >
> > The function prints the number of memory blocks allocated and the
> > total amount of allocated memory in CVI's Debug Output Window. It
> > takes four arguments: an optional message to help you associate the
> > output with a location in your code, optional values which hold the
> > number of allocated blocks and memory on output, and an options field
> > which allows you to specify if you want additional information to be
> > printed about the memory blocks.
> >
> > Depending on how much memory you have allocated this function can take
> > quite a long time to run (a few seconds) though.
> >
> > Hope this helps,
> >
> > Peter
> >
> > /*************************************/
> >
> > #include
> > #include
> > #include
> >
> > #define SHOW_ALLOCATED_MEMORY 0x80000000
> >
> > typedef int (CVIFUNC *TDynMemInfo)(char *message,
> > unsigned int *numAllocBlocks,
> > unsigned int *numAllocBytes,
> > unsigned int options);
> >
> > int MyGetDynamicMemInfo (char *message,
> > unsigned int *numBlocks,
> > unsigned int *numBytes,
> > unsigned int options)
> > {
> > HMODULE dll = GetModuleHandle ("cvirte.dll");
> > TDynMemInfo dmi = (TDynMemInfo)GetProcAddress (dll, "CVIDynamicMemoryInfo");
> >
> > if (dmi)
> > return dmi (message, numBlocks, numBytes, SHOW_ALLOCATED_MEMORY);
> > /* check debug output window for memory information */
> > /* note: this function may take a few seconds to execute */
> > /* if many blocks were allocated. */
> > else
> > return -1;
> > }
> >
> > int main (int argc, char *argv[])
> > {
> > unsigned int numAllocatedBlocks, numAllocatedBytes;
> > void *memory = malloc (1234);
> >
> > MyGetDynamicMemInfo ("Some Message",
> > &numAllocatedBlocks,
> > &numAllocatedBytes,
> > SHOW_ALLOCATED_MEMORY);
> >
> > free (memory);
> >
> > return 0;
> > }
> >
0 Kudos
Message 4 of 4
(4,047 Views)