Creating a new object destroys an older object with different name in C++
- by Mikael
First question here!
So, I am having some problems with pointers in Visual C++ 2008. I'm writing a program which will control six cameras and do some processing on them so to clean things up I have created a Camera Manager class. This class handles all operations which will be carried out on all the cameras. Below this is a Camera class which interacts with each individual camera driver and does some basic image processing.
Now, the idea is that when the manager is initialised it creates two cameras and adds them to a vector so that I can access them later. The catch here is that when I create the second camera (camera2) the first camera's destructor is called for some reason, which then disconnects the camera.
Normally I'd assume that the problem is somewhere in the Camera class, but in this case everything works perfectly as long as I don't create the camera2 object.
What's gone wrong?
CameraManager.h:
#include "stdafx.h"
#include <vector>
#include "Camera.h"
class CameraManager{
std::vector<Camera> cameras;
public:
CameraManager();
~CameraManager();
void CaptureAll();
void ShowAll();
};
CameraManager.cpp:
#include "stdafx.h"
#include "CameraManager.h"
CameraManager::CameraManager()
{
printf("Camera Manager: Initializing\n");
[...]
Camera *camera1 = new Camera(NodeInfo,1, -44,0,0);
cameras.push_back(*camera1);
// Adding the following two lines causes camera1's destructor to be called. Why?
Camera *camera2 = new Camera(NodeInfo,0, 44,0,0);
cameras.push_back(*camera2);
printf("Camera Manager: Ready\n");
}