0

I'm writing a program in C++ using Visual Studio 2012. The program requires two devices to be connected to it: a pressure sensor and driver that controls a motorized platform. The program worked fine when I wrote all the driver code in main. The motors moved and the pressure sensor indicated the value. However, I wanted to simplify the program and apply objects/classes to make it neater and more manageable. I'm using a .lib and a .dll file, and for whatever reason, when I started to use objects, the program didn't like calling the functions from an object.

This is the error I get:

"Unhandled exception at 0x1000AAD2 (Ldcnlib.dll) in motors.exe: 0xC0000005: Access violation writing location 0x00000025."

I'm thinking maybe I have my #includes wrong? But I can't figure it out. I've provided by project folder Motors.rar if anyone cares to look. If you do, the program runs until

if((!driverA.setParameter()) || (!driverB.setParameter()) || (!driverC.setParameter()))

in main. Debugging, I find the problem occurs exactly at

int Driver::setParameter(){ 
   if(!StepSetParam(PicoAddr, Mode, MinSpeed, RunCurrent, HldCurrent, ADLimit, EmAcc))  
      return 0;
}

in driver.cpp.

I took that code out of main, and I get the same problem further down when I try to call .loadTraj. If I try to do the !StepSetParam directly in main, bypassing the class, same error.

(EDIT: It probably goes without saying, but the program won't work at all because you don't have the necessary devices connected to the COM ports, but I hope someone can help just by looking) Code: Main stdafx.h driver.h driver.cpp stepper.h

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 1
    Please show us the code. Try and boil it down to a small, reproducible sample. Posting the project at a hosting site isn't going to work. We need to see where those variables are coming from in the line that crashes. What does the debugger tell you (i.e., which variables have that value)? – Ed S. Jul 17 '12 at 17:04
  • 3
    "Access violation writing location 0x00000025" is almost always a NULL-pointer access – Christian Stieber Jul 17 '12 at 17:05
  • 3
    @ChristianStieber: Not NULL; `0x00000025` :D – Ed S. Jul 17 '12 at 17:06
  • Next time please format your code before posting. – cybertextron Jul 17 '12 at 17:07
  • Access violations are not caused by include orders in my experience. Sounds like your `setParameter` method is fracking with memory it shouldn't be. Check your parameters and variables are properly initialized in the debugger. – AJG85 Jul 17 '12 at 17:11
  • 5
    @EdS.: The raw address isn't NULL, but it's typically caused by accessing a member variable through a null pointer. I'm going to guess that one of the fields named in the call to `StepSetParam` is at offset 0x25 within the `Driver` class (most likely a single-byte variable since it's not 2-byte aligned) and that somehow `Driver::setParameter()` got called on a NULL pointer. – Adam Rosenfield Jul 17 '12 at 17:13
  • Main: http://pastebin.com/jrGZ8caT stdafx: http://pastebin.com/nt2tsbC2 driver.h: http://pastebin.com/mVXtQtbF driver.cpp: http://pastebin.com/4Ed9cZav stepper.h: http://pastebin.com/hndiXfVm The error occurs in driver.cpp, after the driverA.setParameter() call in main. StepSetParam is defined in stepper.h, and all those functions are derived from the .lib/.dll I have included in the project. Sorry about the formatting, I stink. – Robert Joseph Dacunto Jul 17 '12 at 17:19
  • @AdamRosenfield Nope not null, however `StepSetParam` is a c-style function from picomotor library using addresses and bit flags. I'd put money on one of the parameters having a default or garbage value of `0x00000025` though. He may need to zero out some structs before using the APIs or something but he hasn't put relevant code in the question. – AJG85 Jul 17 '12 at 17:28
  • I included the relevant code in the above comment on pastebin. Also edited the question to put the links as well. Also, the StepSetParam worked just fine prior to using classes. When I had all the code in main, it worked fine. I didn't change anything with StepSetParam, just the way I was calling it. – Robert Joseph Dacunto Jul 17 '12 at 17:33
  • 1
    An access violation on a low address is *almost always* trying to use a members variable when `this` is `NULL`. – Bo Persson Jul 17 '12 at 18:14
  • 1
    @AJG85: I'm not sure what you're trying to tell me. If you have a null pointer and call a non-virtual function on it, it won't crash until you actually try to access a member variable, call a virtual function, or access a virtual base class. If you access a member variable at offset 0x25 from a null pointer, it will crash with an access violation on address 0x00000025. – Adam Rosenfield Jul 17 '12 at 20:26
  • Never written or encountered code that exhibits this but did a quick test and you're right. This would mean that the OP is explicitly setting `Driver` pointers to `nullptr` or deleting them and *then* calling method on them? As normally you would get a magic debug value like 0xABABABAB or something ... – AJG85 Jul 17 '12 at 22:03

0 Answers0