LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Intermittent issues with malloc

So I'm having some bizarre issues that I can't quite figure out how to deal with. I think there have been a few ways that this has presented, but the one that consistently happens is at the very beginning of my project I have a line that looks like this:

 

	pc.uifname = malloc(strlen(uifname)+1);
	strcpy(pc.uifname, uifname);

 These are the first two lines in load_ui(), which is one of the very first functions I'm running. What's happening is that the malloc call isn't "taking". It's not being skipped, because the execution pauses there if I set a breakpoint, but malloc is only allocating like 5 bytes to the array, rather than 27. Setting a watch expression for strlen(uifname)+1 gives me the correct number, setting int len = strlen(uifname)+1 gives me the right number, but malloc is returning a much smaller value for some reason.

 

And it gets worse. If I insert the line "int len = strlen(uifname)+1" before the malloc call, the malloc call works fine. No change in that line, just assigned a random variable beforehand. However, somewhere way later in the program, I get a GPF (at a call to InsertListItem). Update: After removing the int len = strlen(uifname)+1 line, the malloc call works again, but the GPF is still there.

 

Here's all the code that runs before load_ui() is called:

 

int main (int argc, char *argv[])
{
	//Generate locks, thread safe variables
	CmtNewLock(NULL, OPT_TL_PROCESS_EVENTS_WHILE_WAITING, &lock_pb);
	CmtNewLock(NULL, OPT_TL_PROCESS_EVENTS_WHILE_WAITING, &lock_pp);
	CmtNewLock(NULL, OPT_TL_PROCESS_EVENTS_WHILE_WAITING, &lock_DAQ);
	CmtNewLock(NULL, OPT_TL_PROCESS_EVENTS_WHILE_WAITING, &lock_plot);
	
	InitializeQuitUpdateStatus();
	InitializeQuitIdle();
	InitializeDoubleQuitIdle();
	InitializeStatus();
	idle_thread = NULL;
	update_thread = NULL;

	if (InitCVIRTE (0, argv, 0) == 0)
		return -1;	/* out of memory */
	
	//Main panel
	char *uifname = "Magnetometer Controller.uir"; // So it's easy to change this if we need to.
	
	if(load_ui(uifname)) // This function loads the UI and creates the structs.
		return -1;

 

This has happened before, but usually I start trying to debug it and after some time it goes away. It's happened on multiple machines with the same presentation, so it's not likely to be bad physical memory. I've restarted LabWindows a few times. At the first call in load_ui, the resource tracker was showing 1 block of memory, 5 bytes, allocated, plus the thread locks and thread safe variables.

 

Any ideas on what this could be?

0 Kudos
Message 1 of 6
(3,928 Views)

Hi Paul, that malloc problems are very strange.

I use a similar approach to make changing the uir file name easy that may possibly be an alternative solution: I #define a macro with the name. In your case it would be

#define  UIR     "Magnetometer Controller.uir"

 

UIR macro can then be passed to LoadPanel as you would do with regular filenames. It saves me the need to allocate a (global?) variable to be used wherever I need it to.



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 6
(3,926 Views)

Yeah, that's a good point, I'll probably switch it over to a macro, though honestly I'm not so worried about the global variable. pc is a global struct that I generally use for containing information about the program, so adding a little control about where the filename is isn't so annoying.

 

Still, I don't think the issue is anything with the code itself, rather this happens to be the first malloc call and it's failing because of some underlying issue with memory management. Its intermittent nature really makes it hard to test.

0 Kudos
Message 3 of 6
(3,907 Views)

Paul,

 

You had mentioned this intermittent failure is happening on multiple computers, are all of those machines off of the same image by chance? intermittent memory management issues (but all in the same spot) can be pretty tricky to diagnose, so I just want to make sure it isnt an issue with the image were on.  

 

Regards,

 

Kyle Mozdzyn

Applications Engineering

National Instruments

Regards,

Kyle M.
Applications Engineering
National Instruments
0 Kudos
Message 4 of 6
(3,898 Views)

I'm not sure I understand the meaning of "on the same image". Could you elaborate? I'm using SugarSync to sync the entire workspace, so I'm not like working off of a thumb drive that is the same disc, but I am syncing each file, so all the component files are copied exactly (i.e. I'm not compiling an installer and installing on different machines). Not sure if that's useful information.

0 Kudos
Message 5 of 6
(3,889 Views)

Paul,

 

Often, IT departments and such will have an image which they install onto every computer, making them identical. Think of it as a copy-paste of a harddrive. Its possible that the reason it occurs on multiple computers is because theyre based on the same master image, which may have an issue. 

 

Regards,

 

Kyle Mozdzyn

Applications Engineering

National Instruments

Regards,

Kyle M.
Applications Engineering
National Instruments
0 Kudos
Message 6 of 6
(3,880 Views)