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");
}
Camera.h
#include "stdafx.h"
// OpenCV
#include <cv.h>
#include <highgui.h>
// cvBlob
#include "cvblob.h"
// FirePackage
#include <fgcamera.h>
using namespace cvb;
class Camera{
public:
int cameraID;
double x, y,z, FOVx, FOVy;
IplImage *image, *backgroundImage, *labeledImage;
CvBlobs blobs;
Camera(FGNODEINFO NodeInfo[], int camID, float xin, float yin, float zin);
~Camera();
void QueryFrame();
void ProcessFrame();
void GrabBackground();
void LoadCalibration();
void Show();
private:
// ======= FirePackage ======
CFGCamera FGCamera;
UINT32 Result;
FGNODEINFO MyNodeInfo;
UINT32 NodeCnt;
FGFRAME Frame;
// ======= Camera Configuration ======
// Trigger Settings
UINT32 nOn, nPolarity, nSrc, nMode, nParm, BurstCount, DMAMode;
// Image Settings
UINT32 AutoExposure, Shutter, Gain, Brightness, Gamma;
// Image Format Settings
UINT32 Format, Mode, Resolution, ColorFormat, FrameRate;
// Structures
UINT32 TriggerValue;
UINT32 FormatValue;
UINT32 DFormatValue;
// OpenCV Calibration matrices
CvMat *intrinsics, *distortion;
IplImage *mapx, *mapy;
void SetUpFirePackage();
void SetUpOpenCV();
};
Camera.cpp:
#include "stdafx.h"
#include "Camera.h"
Camera::Camera(FGNODEINFO NodeInfo[], int camID, float xin, float yin, float zin)
{
cameraID = camID;
x = xin;
y = yin;
z = zin;
FOVx = 42.6;
FOVy = 32.5;
MyNodeInfo = NodeInfo[cameraID];
SetUpFirePackage();
SetUpOpenCV();
// Grab the first frame
printf("Waiting for frame...\n");
QueryFrame();
};
//Destructor
Camera::~Camera()
{
// Stop the device
FGCamera.StopDevice();
// Close capture
FGCamera.CloseCapture();
// Disconnect before ExitModule
FGCamera.Disconnect();
// Exit module
FGExitModule();
cvReleaseImage(&image);
};
[...]
};