Quantcast
Channel: LabWindows/CVI topics
Viewing all articles
Browse latest Browse all 5357

DiscardAsyncTimer returning before timer callback is complete

$
0
0

I'm trying to write a program in LabWindows/CVI 2017 using async timers but ran into a problem with the DiscardAsyncTimer() function. From the documentation for DiscardAsyncTimer():

A call to create or discard an asynchronous timer will not complete (will block) until all outstanding asynchronous callbacks return.

 

However, I ran into some memory problems where I was freeing memory used in my async timer thread after calling DiscardAsyncTimer() thinking that the memory was no longer in use. But apparently this is not the case? I have an example program below that very easily recreates my problem: a "general protection fault" due to trying to access freed memory. However, if my understanding of the documentation, and the documentation itself, is correct, this should be impossible since DiscardAsyncTimer() is supposed to block until all callbacks return.

 

My questions:

  1. Am I understanding the documentation correctly?
  2. Am I doing something stupid in this example?
  3. How am I supposed to verify that my async timer threads have completed running before freeing the memory?

 

#include <ansi_c.h>
#include <asynctmr.h>
#include <stdio.h>
#include <utility.h> 
#include <userint.h>


typedef struct {
	int* array;
} MyStruct;

int CVICALLBACK ShuffleValues (int reserved, int timerId, int event, void *callbackData, int eventData1, int eventData2)
{
	if (event == EVENT_TIMER_TICK) {
		printf("start\n");
		MyStruct* mystruct = callbackData;
		
		// Shuffle values
		for(int i = 0;i < 1000;i++) {
			mystruct->array[0] = mystruct->array[1];
			mystruct->array[1] = mystruct->array[2];
			Delay(0.01);
		}
		printf("finished\n");
	}
	return 0;
}

int main ()
{
	// Allocate memory
	MyStruct* mystruct = malloc(sizeof(MyStruct));
	mystruct->array = malloc(10 * sizeof(int));

	// Start Async Timer
	int timer = NewAsyncTimer(0.01, -1, 1, ShuffleValues, mystruct);

	// Wait a while to let the timer thread run a bit
	Delay(0.5);

	// Destroy Async Timer  
	printf("start destroying\n");
	int retval = DiscardAsyncTimer(timer);
	printf("finished destroying: %d\n", retval);

	// Free memory now that the timer thread is no longer running
	free(mystruct->array);
	free(mystruct);

	Delay(1);
	return 0;
}

 


Viewing all articles
Browse latest Browse all 5357

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>