Skip to content

Instantly share code, notes, and snippets.

@paulfitz
Created March 8, 2011 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulfitz/861309 to your computer and use it in GitHub Desktop.
Save paulfitz/861309 to your computer and use it in GitHub Desktop.
Narrow down a memory allocation problem
#include <yarp/os/all.h>
#include <yarp/sig/all.h>
#include <yarp/dev/all.h>
#include <iostream>
using namespace yarp::os;
using namespace yarp::dev;
using namespace std;
class iCubControl {
public:
bool headInitialised;
Property head;
IControlLimits *head_lim;
IPositionControl *head_pos;
IVelocityControl *head_vel;
IControlMode *head_mode;
IEncoders *head_encs;
yarp::sig::Vector head_encoders;
yarp::sig::Vector head_command;
PolyDriver *head_device;
int head_joints;
double *head_velocities;
double *head_positions;
double *head_min_limit;
double *head_max_limit;
iCubControl() { headInitialised = false; }
void initHead(bool simulation);
void closeHead();
};
void iCubControl::initHead(bool simulation)
{
if(!headInitialised)
{
cout<<"Aquila: initialising head..."<<endl<<endl;
Property head; // ah-hah!
head.put("device", "remote_controlboard");
head.put("local", "/head");
if(simulation) head.put("remote", "/icubSim/head");
else head.put("remote", "/icub/head");
head_device = new PolyDriver;
head_device->open(head);
//<<<------------------------------------------------------------------ IT CRASHES HERE DESPITE OF ALL THE MEMORY BEING DISALLOCATED, THE HEAD_DEVICE IS NULL BEFORE THE EXECUTION ENTERS THIS LINE
/*
if (!head_device->isValid()) cout<<"Aquila: device is not available"<<endl;
if (!head_device->view(head_lim)) cout<<"Aquila: IControlLimits interface is not available"<<endl;
if (!head_device->view(head_pos)) cout<<"Aquila: IPositionControl interface is not available"<<endl;
if (!head_device->view(head_encs)) cout<<"Aquila: IEncoders interface is not available"<<endl;
if (!head_device->view(head_vel)) cout<<"Aquila: IVelocityControl interface is not available"<<endl;
if (!head_device->view(head_mode)) cout<<"Aquila: IControlMode interface is not available"<<endl;
head_pos->getAxes(&head_joints);
head_encoders.resize(head_joints);
head_command.resize(head_joints);
//allocate memory and set velocities and positions to 0
head_velocities = new double[head_joints];
head_positions = new double[head_joints];
head_min_limit = new double[head_joints];
head_max_limit = new double[head_joints];
for(int i=0;i<head_joints;i++)
{
head_positions[i] = 0.0;
head_positions[i] = 0.0;
head_lim->getLimits(i,&head_min_limit[i],&head_max_limit[i]);
}
*/
headInitialised = true;
}
else cout<<"Aquila: head was already initialised..."<<endl<<endl;
}
void iCubControl::closeHead()
{
if(headInitialised)
{
//head_device->close();
delete head_device;
head_device = NULL;
/*
head.clear();
head_encoders.clear();
head_command.clear();
head_joints = 0;
delete[] head_velocities;
delete[] head_positions;
delete[] head_min_limit;
delete[] head_max_limit;
*/
headInitialised = false;
} else cout<<"Aquila: closing call ignored, head was not initialised..."<<endl<<endl;
}
int main() {
Network yarp;
iCubControl ctrl;
for (int i=0; i<100; i++) {
ctrl.initHead(true);
ctrl.closeHead();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment