0

I have dynamically created an Sensor object with new. However, when I try to use the object, it looks like the Sensor's members are uninitialized. I have verified that the creation and initialization of the object is successful. I don't believe this problem is due to the global object not being static but, changing it doesn't resolve the issue either.

class SensorModule
{

public:
  SPI_BUS* spi;
  ADX123*  accel;
  SENSOR*  sensor2;
  ...

public:
  SensorModule();
  ~SensorModule();
public:
  sendCommand();
  ...

}
SensorModule::SensorModule()
{
  spi = new SPI_BUS(1,2,3);
  accel = new ADX123(spi,4);
  sensor2 = new SENSOR(spi,5);

  ...// more initialization
} 
void SensorModule::sendCommand(void){
  accel->accelCommand(); // When I debug and stop here, I see that my "this" 
                         // accel pointer are uninitialized
                         // however, the SensorModule object pointer is                      
                         // valid in the heap
}

main.cpp

SensorModule* pModule = NULL;
int main(void)
{
  pModule = new SensorModule();

  pModule->sendCommand();
  ...
  for(;;)
  {}
}
user3014816
  • 63
  • 2
  • 5

1 Answers1

0

your code seems fine so far, although you have to be very careful to also call delete. In C++ you should usually use unique_ptr to make sure that the elements are deallocated.

Do you debug in debug or release mode? Release mode sometimes messes up some debug symbols. Do you change accel in the course of the constructor or with another method? There has to be something because accel is definitely initialized here. You might also step through the different lines of your constructor to check this.

IceFire
  • 4,016
  • 2
  • 31
  • 51
  • Nothing is fine there. – SergeyA Feb 19 '16 at 18:04
  • 2
    Of course, there are things to improve. But an accel pointer that is not initialized is nothing that comes from the code that he has posted. – IceFire Feb 19 '16 at 18:05
  • *"Release mode sometimes messes up some debug symbols"* - release code does not (usually) have any debug info in it! – Ed Heal Feb 19 '16 at 18:30
  • A little bit of information can usually be found. At least this is something that I experience when debugging with Visual Studio. It is very limited, though, and highly dependant on what is optimized away or not – IceFire Feb 19 '16 at 18:33