Memory access violation ! where to start?

Hi everyone. I have a juce project that is working on Mac . I can create an au plugin in Mac and it works when i load it into reaper. I try to do it in PC, I added visual studio 2019 as an exporter and tried to compile the code. First i got some errors about type definitions.
There is a file “WaveTable h/cpp” in code. There are two classes in this file. One is the WaveTable and the other one is WaveTapleMap .In WaveTableMap ;

void    makeSawtooth(int tsize = 4096, float subdivide=0.9f);
void    makeSquare(int tsize = 4096, float subdivide=0.9f);
void    makeTriangle(int tsize = 4096, float subdivide=0.9f);
void    makeSine(int tsize = 128, float subdivide=0.9f);

i have this methods. In each method i have (in WaveTable.cpp) a similar routine.For example in makeSawtooth code is like this;

int tableLen = tsize;    
float * freal = new float [tableLen];
float * fimg  = new float [tableLen];

for (int i = 0; i < tableLen; i++) {
    fimg[i] = 0.0;
}

freal[0] = freal[tableLen >> 1] = 0.0;

for (int i = 1; i < (tableLen >> 1); i++) {
    freal[i] = 1.0 / i;               // sawtooth spectrum
    freal[tableLen - i] = -freal[i];  // mirror
}

// Create the wavetable.
WaveTable * wt = new WaveTable();
wt->load(freal, fimg, tableLen, subdivide);

// Add the table
_wtmap.insert(WTMap::value_type("Sawtooth", wt));

delete [] freal;
delete [] fimg;

}

But in makeSine method, definition is different.

int tableLen = tsize;  
float freal[tableLen];// vs2019 is complaining about this defination. 
float fimg[tableLen]; // /*C++ expression must have a constant value.the value of variable  cannot be used as a constant*/

When i replace these two with " float * freal = new float [tableLen];
float * fimg = new float [tableLen];" and with “delete fimg,delete freal” at the end of the code, it is able to compile it. But when i try to run it in host it crashes. I am getting an memory acces violation error. I know that definition with word “new” is the correct way to define these variables but since it is working on Mac , Is there any way to define these in a different way ? or anything to do that might help me?

the error: WaveTable* reference returns a nullpointer

i know it is almost impossible to define the problem with this info , but i really need suggestions, If there is a trustworthy way to debug the code with host and if there is a special way that i need to follow to solve access violation problems, please let me know.

Attach a debugger to your host and load the plug-in. I am quite sure you’ll find a couple of posts here describing the procedure, or google :slight_smile:

1 Like

And build with the Address Sanitizer. That is a mac feature (not sure if it exists on Windows as well), but it will find mistakes, that just happened to go undetected on Mac, and might fix your problems on windows as well…

1 Like

On Windows use Application Verifier.

1 Like

The best practice in C++ is to avoid explicit calls to new and delete. In the code you posted, freal and fimg should be std::vectors rather than heap-allocated C arrays. The ‘wt’ instance of WaveTable could probably be a std::unique_ptr.

2 Likes

Thank you all for all these advice :pray: I will try all of them one by one.