SOLVED: Plug'nScript Native Code Issue

Official support for: bluecataudio.com
Post Reply New Topic
RELATED
PRODUCTS

Post

Can anyone enlighten me as to why this simple C++ code (when compiled into a bin file with Xcode on a Mac) crashes PnS upon loading it. If it doesn't crash on the first load it will crash after hitting the reload button once or twice.

I have built a debug version and attached a debugger to PnS and the initialize function completes successfully. Since I don't have a debug build of PnS I can't really tell what's happening after that as Xcode can only display the assembly language.

A couple of clues, perhaps:
- The problem goes away if the line calling to the .resize method is removed, although that method does execute successfully in the debugger.
- The crash always happens in a routine called libsystem_platform.dylib`_platform_strlen

Any ideas anyone?

The Code

Code: Select all

/*
 *   RMS Level Integrator
 */
#include "./dspapi.h"
#include "./cpphelpers.h"
#include <math.h>


DSP_EXPORT uint audioInputsCount=0;

// metadata
DSP_EXPORT string name="RMS Integrator";
DSP_EXPORT string author="Centripidity";
DSP_EXPORT string description="Averages RMS level of each channel.";


// output parameters definition
DSP_EXPORT array<string> outputParametersNames={};

bool initialised = false;

DSP_EXPORT bool initialize() {
  if(!initialised) {
    outputParametersNames.resize(audioInputsCount);
    initialised = true;
  }
  return true;
}
Peter
Last edited by Minisonic on Sat Jul 31, 2021 3:18 am, edited 1 time in total.

Post

When you resize the array, it does not allocate nor initialize the individual strings (in the C version strings are just C strings that have to be allocated manually). You may want to check our samples (some of them allocate strings dynamically). That's why PnS crashes: your strings are basically random pointers to uninitialized memory.

Post

I started out using the level meter example as a template and stripped it back to see what was causing the crash in my code. I’ll revisit it in the light of what you’ve said. Thanks.

Post

The easiest way to handle strings is to use std::string objects (to handle the allocation) and only refer to their pointer using the c_str() method. Just make sure that these do not get reallocated during the lifetime of the script, as PnS expects unmutable strings with constant pointers, once the initialize function has been called.

Post

Problem solved. I had a bool variable called "reset" and, although that doesn't bother Xcode, it seems to confuse PnS and cause a crash. Perhaps it thinks it's a pointer to a reset() function?

I renamed the variable and all seems to work as expected now.

Post

reset is indeed a reserved function so using an exported symbol with the same can cause problems!

Code: Select all

 /** Reset the state of the filter.
  *
  */
  DSP_EXPORT void reset()
  {
  }

Post Reply

Return to “Blue Cat Audio”