12-19-2024 03:18 PM
got told that arrays and strings are objects and they do not get deleted at the end of the program. So I did this
what happened, the vi crashed straight away. Without the 2 delete lines I can run the vi as long as I want, the error comes when I close the vi. Maybe somehow then it disconnects from the dll and data is gone before the vi ended gracefully.
Or maybe, because I compiled for debugging? That as a side note, I try c_str now.
For the future reader, just override the dll in the library
and run vi again
12-19-2024 05:43 PM
ok, still not happy
need to initialise to 129 in Labview and set minimum string length to 129, otherwise it crashes. But the error persists on closing. Error string is truncated to 128 characters for sure, so in principle it works
12-20-2024 01:51 AM
@Steffen01 wrote:
ok, still not happy
need to initialise to 129 in Labview and set minimum string length to 129, otherwise it crashes. But the error persists on closing. Error string is truncated to 128 characters for sure, so in principle it works
Yes, for sure you will get an assertion here:
Please read documentation thoroughly and feel difference:
#include <iostream>
using namespace std;
int main()
{
string errorMSG(512, '-');
char toLVerror_string[129] = { 0 };
// This code below is OK
// refer to https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strncpy-s-strncpy-s-l-wcsncpy-s-wcsncpy-s-l-mbsncpy-s-mbsncpy-s-l?view=msvc-170
strncpy_s(toLVerror_string, 129, errorMSG.c_str(), 128);
printf("Destination OK: %s", toLVerror_string);
// also check example https://en.cppreference.com/w/c/string/byte/strncpy
/*
* The strncpy_s function try to copy the first D characters of strSource to strDest,
* where D is the lesser of count and the length of strSource.
* If those D characters will fit within strDest (whose size is given as numberOfElements)
* and still leave room for a null terminator, then those characters are copied and a terminating null is appended;
*/
// But this code is not OK
// refer to https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strcpy-s-wcscpy-s-mbscpy-s?view=msvc-170
strcpy_s(toLVerror_string, 128, errorMSG.c_str());
printf("Destination too small: %s", toLVerror_string);
// and example https://en.cppreference.com/w/c/string/byte/strcpy
/*
* The strcpy_s function copies the contents in the address of src,
* including the terminating null character, to the location that's specified by dest.
* !!! The destination string must be large enough to hold the source string and its terminating null character
*/
}