Test and Measurement Forum

Access violations when accessing data in fast streaming mode

Post your .Net discussions here

Access violations when accessing data in fast streaming mode

Postby davidpruitt » Fri Jun 03, 2016 4:52 am

I am doing fast streaming on the Pico 2204A. I am using Visual Studio 2015 and C# as my language. When running my app in "Debug" mode, everything works perfectly, however, this is not the case when I change it to "Release" mode.

Whenever I am in "Release" mode, I am getting an access violation. It seems as if I my program is trying to access memory that it is not supposed to access. I have isolated the issue to the code that gets the overview buffers from the Pico scope.

Here is the normal version of my code:

Code: Select all
private unsafe void get_overview_buffers (short **overviewbuffers, short overflow, uint triggeredAt, short triggered, short auto_stop,
   uint nValues)
{
   for (int x = 0; x < nValues; x++)
   {
      ChannelA.Add((_max_voltage * (double)overviewbuffers[0][x]) / PicoScopeLibrary.Imports.MaxValue);
      ChannelB.Add((_max_voltage * (double)overviewbuffers[2][x]) / PicoScopeLibrary.Imports.MaxValue);
   }

   _num_new_data_points_a = Convert.ToInt32(nValues);
   _num_new_data_points_b = Convert.ToInt32(nValues);
}


This code causes an access violation when trying to access the "overviewbuffers" array. I tried isolating a few things throughout my program, and eventually I just reduced the above function to the following code:

Code: Select all
private unsafe void get_overview_buffers (short **overviewbuffers, short overflow, uint triggeredAt, short triggered, short auto_stop,
   uint nValues)
{
   for (int x = 0; x < nValues; x++)
   {
      var j = overviewbuffers[0][x];
   }
}


This code still causes an access violation. I would like to stress the fact that the variable "j" is not used/read from/written to/accessed anywhere else in the program. The only thing happening in this program is that I am reading the overview buffers.

If I remove the line of code where I access the overview buffer, then no access violation occurs. So this code runs safely:

Code: Select all
private unsafe void get_overview_buffers (short **overviewbuffers, short overflow, uint triggeredAt, short triggered, short auto_stop,
   uint nValues)
{
   for (int x = 0; x < nValues; x++)
   {
      //empty
   }
}


As an FYI, this is how I am setting up fast streaming:

Code: Select all
short auto_stop = 0;
short result = PicoScopeLibrary.Imports.ps2000_run_streaming_ns(ScopeHandle, 5, PicoScopeLibrary.Imports.ReportedTimeUnits.MicroSeconds,
   15000, auto_stop, 1, 15000);


And my loop which continuously calls the function to get the overview buffers looks like this:

Code: Select all
while (true)
{
   unsafe
   {
      //Grab values from the oscilloscope
      PicoScopeLibrary.Imports.ps2000_get_streaming_last_values(ScopeHandle, get_overview_buffers);
   }
}


Anyone have any idea what is going on here?
davidpruitt
Advanced User
Advanced User
 
Posts: 21
Joined: Tue May 17, 2016 6:45 pm

Re: Access violations when accessing data in fast streaming

Postby davidpruitt » Fri Jun 03, 2016 2:02 pm

I have been actively trying to debug this, but still no luck. I suspected that maybe the overview buffers were overflowing, and maybe nValues was exceeding the size of the overview buffers, which would cause my loop to also read out of bounds of the allocated array.

I tested this by simply outputting the size of nValues to a file on each call of the get_overview_buffers function. I then checked to see if nValues ever exceeded the size of the overview buffers (15000), and I plotted the size of nValues over time.

Here is the code:

Code: Select all
private unsafe void get_overview_buffers (short **overviewbuffers, short overflow, uint triggeredAt, short triggered, short auto_stop,
   uint nValues)
{
   StreamWriter k = new StreamWriter("debug.txt", true);
   k.WriteLine(nValues.ToString());
   k.Close();

   for (int x = 0; x < nValues; x++)
   {
      var j = overviewbuffers[0][x];
   }
}


nValues never exceeded the size of the overview buffers (15000). The largest it got was about 13000. Here is the plot:

test_fig.png
test_fig.png (4.13 KiB) Viewed 4027 times


On the iteration in which the program crashed, nValues was 6. Just 6. So it doesn't seem like a buffer overflow is the cause of this issue. Any ideas?
davidpruitt
Advanced User
Advanced User
 
Posts: 21
Joined: Tue May 17, 2016 6:45 pm

Re: Access violations when accessing data in fast streaming

Postby Hitesh » Mon Jun 06, 2016 11:01 am

Hi David,

Was nValues equal to 0 when you tried to access to the overview buffers?

Please find below the callback function code from our C# streaming example:

Code: Select all
/****************************************************************************
* StreamingCallback
* used by data streaming collection calls, on receipt of data.
* used to set global flags etc checked by user routines
****************************************************************************/
unsafe void StreamingCallback(short** overviewBuffers,
                                          short overFlow,
                                          uint triggeredAt,
                                          short triggered,
                                          short auto_stop,
                                          uint nValues)
{
            // used for streaming
            _autoStop = auto_stop != 0;

            // flags to show if & where a trigger has occurred
            _trig = triggered;
            _trigAt = triggeredAt;
            _nValues = nValues;

            if (nValues > 0 && !_appBufferFull)
            {
                try
                {
                    for (int i = (int)_totalSampleCount; i < nValues + _totalSampleCount; i++)
                    {
                        for (int channel = 0; channel < _channelCount; channel++)
                        {
                            if (Convert.ToBoolean(_channelSettings[channel].enabled))
                            {
                                _appBuffer[channel][i] = overviewBuffers[channel * 2][i - _totalSampleCount]; //Only copying max data from buffers
                            }
                        }
                    }
                }
                catch (Exception) // If trying to place data
                {
                    _appBufferFull = true;
                    Console.WriteLine("Appbuffer full collection cancelled");
                }
            }

            _totalSampleCount += nValues;
}

From the Programmer's Guide:

Your callback function should do nothing more than copy the data to another buffer
within your application. To maintain the best application performance, the function
should return as quickly as possible without attempting to process or display the data.

I would suggest copying the data for each channel into a larger corresponding array, placing each chunk of data at the next position in the large array as you continue to collect data.

You can then process the data elsewhere in your application.

Regards,
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 2063
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: Access violations when accessing data in fast streaming

Postby davidpruitt » Mon Jun 06, 2016 2:45 pm

Hitesh,

Thank you for your reply. That is exactly what I am doing. My original code was simply copying data from the overview buffers to my own arrays. Having clocked my program, it is more than fast enough to read from the overview buffers without any overflows happening. Since I am sampling at a rate of 1 sample = 10 us, with overview buffers of 15,000 samples in size, I should theoretically be safe if I can grab data every 150 ms from the overview buffers. I have clocked my program at running at least 100 frames/second, meaning that it checks the overview buffers every 10 ms and grabs data from them. As stated previously, nValues has never exceeded 15,000.

Also, to answer your question, nValues was not 0 when the program crashed. The value of nValues was 6 on the specific instance that I mentioned in my previous post. It has been other values on other runs of the program. It has even been 0 on certain calls to the get_overview_buffers function, but it has never been 0 on the call that crashes the program. Obviously that wouldn't make sense, since the for loop in which the crash happens only executes if nValues is greater than 0.

One thing I have noticed: the total number of values read from the Pico scope (summed over all calls to get_overview_buffers function) is greater than or equal to 3915000. This is true in every case so far! The total number of values has either been 3915000 or 3915001.

So 3915000 total samples read from the scope = 7830000 bytes (each sample is 2 bytes since it is a short) = 7.4673 MB of data. Is there a buffer on the hardware or in the driver program that can't exceed 7.4673 MB? If so, why is that buffer storing every single sample ever streamed? Is it not using a ring buffer?

Apparently, once my program has streamed 3915000 values from the scope, and I try to read in the latest value, that's when everything crashes.
davidpruitt
Advanced User
Advanced User
 
Posts: 21
Joined: Tue May 17, 2016 6:45 pm

Re: Access violations when accessing data in fast streaming

Postby Hitesh » Tue Jun 07, 2016 11:59 am

Hi David,

Apologies, I missed the for loop checking condition :oops:

Which version of the ps2000.dll are you using and is it 32-bit or 64-bit?

Regards,
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 2063
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: Access violations when accessing data in fast streaming

Postby davidpruitt » Tue Jun 07, 2016 2:32 pm

64-bit. I am working on a smaller version of the app that replicates the problem that I can upload.
davidpruitt
Advanced User
Advanced User
 
Posts: 21
Joined: Tue May 17, 2016 6:45 pm

Re: Access violations when accessing data in fast streaming

Postby Hitesh » Wed Jun 08, 2016 8:27 am

That would be helpful.

If you can indicate the driver version number (you can query this via the ps2000_get_unit_info() function) that would be helpful as well.

Regards,
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 2063
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: Access violations when accessing data in fast streaming

Postby davidpruitt » Mon Jun 13, 2016 11:28 pm

I have finally been able to come up with a version of the program that reliably reproduces the behavior.

First of all, here are some stats on my oscilloscope:


Driver version: 2.1.4.38
USB version: 2.0
Hardware version: 17
Variant info: 2204A
Serial info: DV037/342
Calibrated date: 11Apr16
Error code: 0
Kernel version: 0.0


Next, here are stats on my system:


Intel Core i5-2400 CPU @ 3.10 GHz, 64-bit
8 GB RAM
Visual Studio 2015 Community is the development IDE, with C# as the language
Targeting .NET framework version 4.5.2
Computer is running .NET framework version 4.6.1


I have figured out what causes the behavior. In the project properties in Visual Studio, under the "Build" options, you can select/de-select "Prefer 32-bit" (the default is selected). If it is not selected, then the program crashes. In my test app, it usually crashes after reading about ~2,145,017 values from the scope (on my machine. This may be both machine dependent and app dependent). The crashing behavior is sometimes an exception, and at other times simply a halt of the program. You must be compiling in "Release" mode for the crash to happen. It will not crash if you are building in "Debug" mode in Visual Studio.

Finally, I am fairly confident that I have the 64-bit version of the driver and SDK installed on my system, because that is what I remember downloading from the Pico website, although it is possible that I am mistaken - I could uninstall and reinstall to be sure.

See the attached ZIP file containing a Visual Studio solution that has code which reproduces the error. You can try selecting/de-selecting "Prefer 32-bit" in the project settings.

I also have 2 different versions of streaming in the app, which you can switch between by changing the value of the variable "program_version" between 1 and 2. With a value of 1, the program streams endlessly (and will therefore reproduce the buggy behavior if "Prefer 32-bit" is not selected). With a value of 2, it will stop and re-start streaming every 15,000 samples (the buffer size that I have set).

If the ZIP file for some reason isn't working, I can post the code on Google Drive, Dropbox, or some code repository. Just let me know.
Attachments
PicoScopeTestApp.zip
Visual studio solution containing code that reproduces the buggy behavior
(94.13 KiB) Downloaded 57 times
davidpruitt
Advanced User
Advanced User
 
Posts: 21
Joined: Tue May 17, 2016 6:45 pm

Re: Access violations when accessing data in fast streaming

Postby Martyn » Tue Jun 14, 2016 5:19 am

Can you post the versions, and locations, of all the ps2000.dll and picoipp.dll files that are present on your PC.
Martyn
Technical Specialist
Martyn
Site Admin
Site Admin
 
Posts: 2348
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: Access violations when accessing data in fast streaming

Postby Hitesh » Wed Jun 15, 2016 2:52 pm

Hi David,

As a follow-up to Martyn's post, it could be that with your project setting for 'Any CPU' when the 'Prefer 32-bit ' setting is unticked it is looking for a 64-bit dll.

I've run some tests here with your application using relatively recent drivers (which have had some fixes for streaming related issues) and I am unable to reproduce the issue. If you could please e-mail support@picotech.com, I can make 32-bit and 64-bit versions available for you to test with.

Regards,
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 2063
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: Access violations when accessing data in fast streaming

Postby davidpruitt » Thu Jun 16, 2016 2:24 pm

Location 1 (where the SDK is installed):

C:\Program Files\Pico Technology\SDK\lib

picoipp.dll version: 1.1.2.52
ps2000.dll version: 2.1.4.38

Location 2 (where the PicoScope software is installed):

C:\Program Files (x86)\Pico Technology\PicoScope6

Pico.IPP.dll version: 1.1.2.51
picoipp.dll version: 1.1.2.51
PS2000.dll version: 2.1.4.37
davidpruitt
Advanced User
Advanced User
 
Posts: 21
Joined: Tue May 17, 2016 6:45 pm

Re: Access violations when accessing data in fast streaming

Postby Hitesh » Fri Jun 17, 2016 7:51 am

Hi David,

Thanks for posting the information.

Those are older versions of the drivers - please e-mail in as requested and I will provide you with a link to an updated driver.

Regards,
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 2063
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: Access violations when accessing data in fast streaming

Postby davidpruitt » Fri Jul 22, 2016 3:12 pm

Hitesh,

Thanks for the new drivers. I will try them out today. I have a few questions (also, sorry about the long amount of time between my last reply and this new reply).

Question 1: We are a neuroscience lab, and could potentially be using these Pico scopes in a lot of places throughout our lab. When installing my program that I have written on our systems, what is the best way to package the Pico Scope dll's? Should I install the Pico SDK on each computer? Should I install the full PicoScope software suite on every computer? Or can I just package the ps2000.dll file with my program?

Question 2: Is the ps2000.dll the only dll that I need? Do I also need to the picoipp.dll file that was referenced earlier? Are there any other dll files necessary?

Question 3: While attempting to install my program on another computer in our lab yesterday, I encountered an interesting issue (that I have yet to fully debug). From the looks of it, it is similar, if not identical, to the issue that I have talked about earlier in this thread - but with a slight nuance.

When installing it, I first installed the PicoScope software suite found on this website (version 6.11.12, the version currently available for download). I then installed my program. After running my program, it would crash after about 20 seconds or so, very consistently, in the exact same manner that my program would crash as I talked about earlier in the thread. I checked the dll versions that came with PicoScope software 6.11.12, and they are:

PS2000.dll: 2.1.6.2
PicoIpp.dll: 1.1.2.55

After seeing it crash so consistently, I uninstalled the PicoScope software, and then I installed a different version of the PicoScope software (the version that was shipped to us on disk when we ordered the Pico 2204A). This version was PicoScope software 6.10.818, and the dll files are the versions that I listed earlier in this thread.

When I installed the older version of the PicoScope software with the aforementioned dll files, my code worked and did not crash.

I have yet to try the new dll files that you emailed to me (ps2000.dll version 2.2.0.12), but I will do so today.
davidpruitt
Advanced User
Advanced User
 
Posts: 21
Joined: Tue May 17, 2016 6:45 pm

Re: Access violations when accessing data in fast streaming

Postby Hitesh » Mon Jul 25, 2016 9:00 am

Hi David,

To answer your questions:

1. The SDK installer also installs the USB kernel drivers required for Windows platforms to recognise our devices. There is a system folder in the SDK root directory containing the files to install but at the present moment, the only files that will be copied across at the time of install will depend on the platform architecture i.e. 64-bit kernel drivers for 64-bit operating systems.

This will be corrected in the next release of the SDK to allow for 32-bit and 64-bit operating systems, so the best thing for now would be to install the SDK then replace the ps2000.dll with the newly-supplied one.

2. The ps2000.dll driver is the only one that you will need - you can choose which drivers and examples to install from the SDK install wizard.

3. Please try the new drivers and if the crash still occurs please post your code or send it to support@picotech.com

Regards,
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 2063
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire


Return to .Net Languages (C#, VB.Net, .)

Who is online

Users browsing this forum: No registered users and 1 guest