Access violation reading location 0x00000024

what is causing this and how can I fix this? I'm trying to do a PWM class... basically the very same code for opening and closing pins works straight from another .cpp file, to me it looks like something's wrong with GpioController, which is weird, because I can turn on and off a specific pin using the VERY SAME app and almost identical code

class header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#pragma once

using namespace Windows::Devices::Gpio;
using namespace Windows::UI::Xaml;

namespace myGpio
{
	public ref class PWM sealed
	{
	public:
		PWM(int p, int w);
		int Init(int p, int w);
		int Clear();

		property int width;
	private:
		//Timer
		void OnTick(Platform::Object^ sender, Platform::Object^ args);
		Windows::Foundation::TimeSpan interval;
		DispatcherTimer^ timer;

		//PWM
		GpioPin^ pin;
		GpioPinValue value = GpioPinValue::High;
		const int frequency = 256;
	};
}

class source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "pch.h"
#include "PWM.h"

using namespace myGpio;

PWM::PWM(int p, int w)
{
	Init(p, w);
}


int PWM::Init(int p, int w)
{
	SYSTEM_INFO sysinfo;
	GetNativeSystemInfo(&sysinfo);
	if (sysinfo.wProcessorArchitecture != PROCESSOR_ARCHITECTURE_ARM)
		return -1;

	GpioController^ gpio = GpioController::GetDefault();
	if(gpio == nullptr)
		return -2;

	GpioOpenStatus s;
	gpio->TryOpenPin(p, GpioSharingMode::Exclusive, &pin, &s);
	if(s == GpioOpenStatus::PinOpened)
	{
		pin->SetDriveMode(GpioPinDriveMode::Output);
		pin->Write(value);

		if(w > frequency)
			w = frequency;
		else if(w < 1)
			w = 1;

		width = w;

		timer = ref new DispatcherTimer();
		interval.Duration = 10 * width;
		timer->Interval = interval;
		timer->Tick += ref new Windows::Foundation::EventHandler<Object^>(this, &PWM::OnTick);
		timer->Start();

		return 0;
	}
	return -3;
}


screenshot of the error:
https://social.msdn.microsoft.com/Forums/getfile/714723

working code i'm using from another .cpp to open/close pin (no PWM)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
MainPage::MainPage()
{
	InitializeComponent();

	SYSTEM_INFO sysinfo;
	GetNativeSystemInfo(&sysinfo);
	if (sysinfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_ARM)
	{
		gpio = GpioController::GetDefault();
		if (gpio == nullptr)
			MessageDialog("There is no GPIO controller on this device!", "Error").ShowAsync();
	}
	else
		MessageDialog("Current processor architecture differs from ARM!", "Error").ShowAsync();
}

void PIN_Control::MainPage::pin_Checked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
	auto checkbox = reinterpret_cast<CheckBox^>(sender);
	if (gpio != nullptr)
	{
		int ipin = _wtoi(checkbox->Tag->ToString()->Data()) - 1;
		if (pin[ipin] != nullptr)
			pin[ipin]->Write(GpioPinValue::High);
		else
		{
			GpioOpenStatus status;
			gpio->TryOpenPin(ipin + 1, GpioSharingMode::Exclusive, &pin[ipin], &status);
			if (status == GpioOpenStatus::PinOpened)
			{
				pin[ipin]->SetDriveMode(GpioPinDriveMode::Output);
				pin[ipin]->Write(GpioPinValue::High);
			}
		}
	}
	else
	{
		MessageDialog("There is no GPIO controller on this device or the processor architecture differs from ARM!", "Error").ShowAsync();
		checkbox->IsChecked = false;
	}
}
Last edited on
gpio->TryOpenPin(ipin + 1, GpioSharingMode::Exclusive, &pin[ipin], &status);

address of pin[ipin] is taken as arg and in your code
gpio->TryOpenPin( [..] , &pin, &status);

you are using pin. Is pin scalar or vector? Because you might dereference pointer to array (thus location at 0x24 memory).
the second code is an example of what is working

both are created in class header, in first example as

Windows::Devices::Gpio::GpioPin^ pin;

in second example as
Windows::Devices::Gpio::GpioPin^ pin[40];
Topic archived. No new replies allowed.