LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic Memory Ownership

Hi,

 

I've been having an issue with allocating and freeing memory in an application I'm developing.

 

Read Thread

1. "GlobalAlloc" a block of memory.

2. Read data from a high speed optical card into the allocated memory.

3. Pass the pointer to the memory onto a TSQ

 

Write Thread

4. Read the pointer from the TSQ

5. Write the block of memory to disk.

6. "GlobalFree" the block of memory.

 

If I run this, it appears that the memory isn't being free'd correctly as the used RAM on the PC keeps increasing as the data is read in. If I place a "GlocalFree" within the Read Thread, it appears to free the block ok.

Is there an issue here with which thread owns the pointer, and if so, how can I get round this issue? I have tried Malloc and Free with the same results.

 

Cheers

Stuart

0 Kudos
Message 1 of 3
(2,931 Views)

I am succesfully running a program with a similar architecture using malloc and free (I'm only passing the memory pointer to a PostDeferredCall instead of using a TSQ) and there are no problems of memory not being freed. Is it possible that you have other memory allocation instructions in the program that are allocating increasing chunks of memory?

 

GlobalAlloc can introduce some overhead in the application as described in this page. This page lists all available memory allocation functions: using GlobalAlloc and GlobalFree should safely allocate and dispose of the required memory: you could try accessing the memory pointer after freeing it to detect if it has been succesfully deallocated.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 3
(2,923 Views)

It turns out it wasn't the allocating of memory that was causing the problem but the "fwrite" commands in the write thread. If I dont write to disk, then the RAM allocation stays the same. When I insert the "fwrite" command back in and start logging data, the used RAM quickly increases to about 18Gb (the PC has 96Gb installed!) and this then causes the writing procoess to fall over and the TSQ fill up causes memory issues there too.

 

The application will be logging data at 250Mb/s for potentially 1-2 hours. (1.8Tb?). Even breaking it down into smaller files initially wont improve the RAM usage.

 

Any ideas?

 

Stuart

0 Kudos
Message 3 of 3
(2,916 Views)