Page 1 of 1

Error in user guide

Posted: Wed Jun 26, 2019 6:39 pm
by francoisreme
Hi folks.

I'm kind of surprised about these lines in flowstone user guide, and also with provided helper functions.
if we use a new [] we need to use delete[]. Could be serious memory issue, if you leak an array of size N at each function call...

Code: Select all

// Delete previous array and reset pOut entry
if( pOut[x] )
{
delete *((int**)&pOut[x]);
pOut[x] = 0;
}
// Create new array (assume we have declared 'length' – add 1 for size at front)
int* array = new int[length+1];

Re: Error in user guide

Posted: Wed Jun 26, 2019 7:35 pm
by trogluddite
Unfortunately, the code examples in the FlowStone documentation and default toolbox items are often of a pretty poor standard. You're quite right of course, the C++ standard says that new[] -> delete is "undefined behaviour". In most compilers, it does work without leaking memory so long as the array contains only POD data types, as they have no destructors to call - so it's fortunate that the DLL API types are all POD (string arrays contains C-style strings which have to be freed manually)!

I've seen other coders do this for POD arrays, especially if they're regular C coders who haven't used C++ much. The compiler will optimise away destructor calls if they're not required, so there's no clever "hacky" reason to do it - it's really easy to get caught out by it if you change the array to contain objects, and it can get beginners into bad habits!

Re: Error in user guide

Posted: Thu Jun 27, 2019 10:27 am
by francoisreme
Didn't event know that (POD types) !