From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

CVI 2013 "FATAL RUN-TIME ERROR: Pointer to free memory passed to library function" when accessing a struct from a struct

Solved!
Go to solution
#include <utility.h>
#include <ansi_c.h>
#include <cvirte.h>

typedef struct StringsStruct
{
  char A[10];
  char AA[10];
  
  char B[10];
  char BB[10];
  
  char C[10];
  char CC[10];
} StringsStructType;

StringsStructType Strings = {0};

char *const SelectedStrings[3] =
{
  Strings.A,
  Strings.B,
  Strings.C
};

int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPSTR lpszCmdLine, int nCmdShow)
{
  if (InitCVIRTE (hInstance, 0, 0) == 0)
    return -1;    /* out of memory */
  
  strcpy( SelectedStrings[1], "TEXT" );
  /*** FATAL RUN-TIME ERROR:   "main.c", line 32, col 11, thread id 0xXXXXXXXX:   Pointer to free memory passed to library function. ***/
  
  Breakpoint();
  
  return 0;
}

Any chance to get this working in CVI 2013?

"&Strings.A[0]" doesn't work either.

-----------------------
/* Nothing past this point should fail if the code is working as intended */
0 Kudos
Message 1 of 4
(4,981 Views)
Solution
Accepted by topic author CVI-User

Hello CVI-User!

 

Thanks for reporting the issue. I have filed bug report # 423491.

 

I haven't had any luck getting rid of the error by changing the definition of the structure, but I was able to get the program running by disabling run-time checking when the structure fields are initialized:

 

strcpy( (char*)(uintptr_t)SelectedStrings[1], "TEXT" );

 Or maybe a more descriptive workaround:

#define UNCHECKED(x) ((void*)(uintptr_t)(x))
strcpy( UNCHECKED(SelectedStrings[1]), "TEXT" );

 Thanks,

 

Peter

0 Kudos
Message 2 of 4
(4,948 Views)

Hi CVI-User,

 

Were you able to use the workaround provided to you by Peter? Is that acceptable to you for now?

Jonathan N.
National Instruments
0 Kudos
Message 3 of 4
(4,873 Views)

Yes, thank you.

This works for me.

-----------------------
/* Nothing past this point should fail if the code is working as intended */
0 Kudos
Message 4 of 4
(4,861 Views)