Hi, I’m tryng to fill a vector with elements of costum class “Circle”, but when I try to execute the constructor of “RaceLight”, which calls the vector’s push_back function, I have a memory corruption error. However, if “RaceLIght” doesn’t inherit from the Juce class “timer”, I don’t have this problem.
Here’s the error and the Source files: , RaceLight and Circle:
Error in `/home/ilaria/TNDSproject/Builds/LinuxMakefile/build/NewProject’: malloc(): memory corruption: 0x0871d528 ***
#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED
#include “JuceHeader.h”
class Circle : public Component
{
public:
Circle(unsigned int);
~Circle();
void paint (Graphics&); void resized(); void SetColour(unsigned int c);
private:
Colour colour; //========================================================================= JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Circle)
};
#endif // CIRCLE_H_INCLUDED
#include “Circle.h”
//==============================================================================
Circle::Circle(unsigned int c):colour(c)
{
// In your constructor, you should add any child components, and
setSize (600, 400);
// initialise any special settings that your component needs.
}
Circle::~Circle()
{
}
void Circle::paint (Graphics& g)
{
/* This demo code just fills the component’s background and
draws some placeholder text to get you started.
You should replace everything in this method with your own drawing code.. */
// clear the background
g.setColour (colour); g.fillEllipse (0,0,getWidth(),getHeight());
}
void Circle::resized()
{
// This method is where you should set the bounds of any child
// components that your component contains…
}
void Circle::SetColour(unsigned int c)
{
colour=Colour(c);
repaint();
}
class RaceLight : public Component,
private Timer
{
public:
//==============================================================================
RaceLight (int N_lights);
~RaceLight();
//============================================================================== //[UserMethods] -- You can add your own custom methods in this section. void light(); //[/UserMethods]
void paint (Graphics& g); void resized();
private:
//[UserVariables] – You can add your own custom variables in this section.
int NumberOfLights;
int TimeOfLight=1000;
vector <ScopedPointer> RedLight;
vector <ScopedPointer> GreenLight;
void timerCallback() override;
//[/UserVariables]
//==============================================================================
//============================================================================== JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RaceLight)
};
//[EndFile] You can add extra defines here…
//[/EndFile]
#endif // JUCE_HEADER_531018019E54512
#include “RaceLight.h”
//[MiscUserDefs] You can add your own user definitions and misc code here…
//[/MiscUserDefs]
//==============================================================================
RaceLight::RaceLight (int N_lights)
: NumberOfLights(N_lights)
{
//[Constructor_pre] You can add your own custom stuff here…
RedLight.reserve(NumberOfLights);
GreenLight.reserve(NumberOfLights);
for(int i=0;i<NumberOfLights;++i){
RedLight.push_back(new Circle(0xffb1082a));
GreenLight.push_back(new Circle(0xff09240a));
addAndMakeVisible(RedLight[i]);
addAndMakeVisible(GreenLight[i]);
}
Thank you very much!