various problems with ttf fonts / problem with deque / compilation question

Hello,

first post, so hello everybody !

I am new in openFrameworks but I have some programming experience

I have a couple of questions/problems, any help would be appreciated…

firstly:

I load some fonts in setup, I have correctly copied all the files in the data folder and I do get the proprer results in my screen when I run the program, but in the console I get several “unable to allocate font…” errors…

secondly:

I have this class:

class textObjectCollection {

deque data;

public:

void draw (); // draw every element in deque
void add (textObject object); // add an element to tail
void remove (); // remove an element from start
void autoRemove (int n); // remove an element from start when size exceeds n
void test(); // for testing purposes… to be deleted afterwards
};

a textObject is a simple struct with strings and ints.

void add is a simple

void textObjectCollection::add (textObject object) {
data.push_back(object);
}

now with the following in my keypressed:

if (key==13 {
textObject a (…);

textData1.add(a);
}

I get:

Program received signal: “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
warning: Could not find object file “/Users/theo/Documents/CODE/__OPENFRAMEWORKS/SANDBOX/COMPILE_LIBRARIES/buildGlutFramework/libForeground.a(macx_foreground.o)” - no debug information available for “/Users/mcast/Code/GLUT-ToPost/macx_foreground.m”.

warning: Could not find object file “/Developer/usr/lib/gcc/i686-apple-darwin9/4.0.1/libgcc.a(_eprintf.o)” - no debug information available for “/var/tmp/gcc/gcc-5493~1/src/gcc/libgcc2.c”.

why ??

lastly…

I mainly work with my mac, would it be easy to make some windows binaries in the future ?? Would I have to change a lot in my code ??? Can I make versions for vista/xp/win7 from the same machine ??

*** all that I mentioned happened in a mac os X 10.6 machine running XCode 3

thx and greetings to everybody !

regarding the second question,

I tried a simple

deque data;

and then

data.pushback( some value)

and I got the same error… may I assume there is something going wrong with my textObject class ?? it seems simple and ok to me…

anybody ??

Hey there,

I’m not exactly sure why you’re getting a crash on when you add to your deque.

If you’re just looking to use an array of objects, i’ve had good luck using the vector class

Here’s a simple skeleton

  
  
//data structure to have in the array  
typedef struct{  
    int somefield;  
    string someotherfield;  
} textObject;  
  

then to use it:

  
  
vector<textObject> objects; //this should be in the testApp.h  
  
//to add objects  
for(int i = 0; i < 30; i++){  
   textObject t;  
   t.somefield = i*10;  
   t.someotherfield = "foo";  
   objects.push_back( t );  
}  
  
//to modify an object  
for(int i = 0; i < objects.size(); i++){  
   textObject& t = objects[i]; //the & returns a reference to the original, otherwise it would create a copy  
   t.somefield += 5;  
}  
  

if you keep experiencing the same error, it may have something to do with how you are managing your textObjectCollection object.

hope this approach helps work it out,

best -

If is comment out the destructor ~textObject then my code work…

what have I done wrong ???

class textObject {

public:

string * text; // to hold the text
int * x; // to hold x coordinate
int * y; // to hold y coordinate
string * date; // to hold the date
// fontProperties * fontProp;

textObject (string, int, int, string);
// ~textObject () ;

void move (float posx, float posy);
void appendToFile (string filename);
int textSize ();

};

textObject::textObject (string str, int posx, int posy, string dt ) {

text = new string;
x = new int;
y = new int;
date = new string;

* text = str;
* x = posx;
* y = posy;
* date = dt;
}

/*
textObject::~textObject () {
delete text;
delete x;
delete y;
delete date;
}
*/

You have not implemented the copy-cnstructor, without that, the compiler tries to build one. But this copy-constructor will fail with you dynamically allocated object.

If you do something like deque.push_back(an_object) and an_object is not a pointer, the object gets copied.

Instead of creating the properties of your object on the heap, i would store them on the stack and create the textObjects on the heap and add only the pointers to them to the deque.

Why are you creating the properties on the heap anyway?

this code should work too, and does not involve any pointers at all:

  
  
class textObject {  
     
public:     
     
   string  text; // to hold the text  
   int  x; // to hold x coordinate  
   int  y; // to hold y coordinate  
   string  date; // to hold the date  
   // fontProperties * fontProp;  
     
   textObject (string, int, int, string);  
   ~textObject () ;  
     
   void move (float posx, float posy);  
   void appendToFile (string filename);  
   int textSize ();  
     
};  
  
textObject::textObject (string str, int posx, int posy, string dt ) {  
     
   text = str;  
   x = posx;  
   y = posy;  
   date = dt;  
}  
  
  
textObject::~textObject () {  
}  
  
  
// later in your code  
deque<textObject> my_deque;  
my_deque.push_back(my_text_object);  
  

If you are storing any pointers as part of your objects you’ll have to implement the copy-constructor, as only you know who “owns” the object the pointer is pointing to and how to handle the copy.

cheers,
Stephan

yes, whenever you have a destructor, you almost sure need a copy constructor and a = operator:

http://en.wikipedia.org/wiki/Rule-of-three-%28C%2B%2B-programming%29

btw, @MarinosK please, use the code tag when posting code.

@arturo -> sory for not using the code tag…

if I do not use pointers and do that the way mentioned, what will happen when I push_back textObjects from my Collection ?? Will the memory associated with them be deleted ?? How can I delete it manually ??

I implemented that with pointers as I thought that was the only way to delete the memory when I push back an object…

thx for the help !

when you push back an object in a collection of objects not pointers it will make a copy, the original will disappears as soon as the block ends, blocks are marked by { }

usually you want to do this. but sometimes making a lot of copies can be slow, then you can use pointers and manage the memory yourself

when I use no pointers my code breaks

  
  
class textObject {    
       
public:       
       
   string  text; // to hold the text    
   int  x; // to hold x coordinate    
   int  y; // to hold y coordinate    
   string  date; // to hold the date    
   // fontProperties * fontProp;    
       
   textObject (string, int, int, string);    
   ~textObject () ;    
       
   void move (int posx, int posy);    
   void appendToFile (string filename);    
   int textSize ();    
       
};    
  
void move (int posx, int posy) {  
   x = posx;  
   y = posy;  
}  
  
  

void move fails to set x and y for some reason I cannot understand…

wtf ??

it has nothing to do with pointers, move as you are declaring it there is not a function of the class you need to put the name of the class like:

  
  
void textObject::move (int posx, int posy) {  
   x = posx;  
   y = posy;  
}  
  

yes I’ ve done that, sorry I wrote it myself rather than pasted it…

This is the way I have it declared… Could it be that variables are passed by value rather than by reference or similar problem ?? I am not accustomed with these notions yet, sorry…

the problem should be elsewhere, probably you are doing a copy of the object before setting the values, then setting the values in the original and expecting them to change in the copy. this is not java : ) in java when you do:

MyObject objec1;  
MyObject object2 = object1;  
objec1.someproperty = 3;  

object2 will be changed too, in c++ that will make a copy and each object will be different. the same happens when you insert an object in a vector or any other collection the object in the collection is a copy of the original, trying to modify the original won’t work

I am not sure I understand this…

II have put this object in a deque and then I do this :

  
  
  
cout <<  data[i].x << endl;  
data[i].x = SOMEVALUE ;  
cout << data[i].x << endl;  
  
  

and the value does not change at all…

this as well

  
  
  
cout <<  data[i].x << endl;  
data[i].move(xValue,yValue);  
cout << data[i].x << endl;  
  
  

I can make it work with pointers, but I am trying to understand the way it works…

that seems impossible : ) can you post more code?

the project is too bit big…

I post some excerpts from it that are related to the problem :

(note I use data.h/data.cpp for all my custom classes)

from data.h

  
  
class textObject {  
	  
public:	  
	  
	string text; // to hold the text  
	int x; // to hold x coordinate  
	int y; // to hold y coordinate  
	string date; // to hold the date  
	// fontProperties * fontProp;  
	  
	textObject (string, int, int, string);  
	//~textObject ();  
	  
	void move (int posx, int posy);  
	void appendToFile (string filename);  
	int textSize ();  
	  
};  
  
class textObjectCollection {  
	  
	deque <textObject> data;  
	  
public:  
	  
	  
	void add (textObject object);  
	unsigned size ();// add an element to tail  
	textObject at (unsigned n);  
	void remove (); // remove an element from start  
	void autoRemove (int n); // remove an element from start when size exceeds n  
};  
  
  

from data.cpp

  
  
textObject::textObject (string str, int posx, int posy, string dt ) {  
	  
	text = str;  
	x = posx;  
	y = posy;  
	date = dt;  
}  
  
  
void textObject::move (int posx, int posy) {  
	x = posx;  
	y = posy;  
}  
  
void textObject::appendToFile (string filename) {  
	  
}  
  
int textObject::textSize () {  
	string txt;  
	txt = text;  
	return txt.size();  
}  
  
  
void textObjectCollection::add (textObject object) {  
	data.push_back(object);  
}  
  
unsigned textObjectCollection::size () {  
	return data.size();  
}   
  
textObject textObjectCollection::at (unsigned n) {  
	return data[n];  
}   
  
void textObjectCollection::remove () {  
  
}   
  
void textObjectCollection::autoRemove (int n) {  
	  
}  
  

from testApp.h

  
  
// text collections to be drawed  
	textObjectCollection textData1;  
int mouseIndex;  
  

from testApp.cpp -> mouseDragged

  
  
        cout << mouseIndex << endl;  
	cout << x << endl;  
	textData1.at(mouseIndex).x=x;  
	cout << textData1.at(mouseIndex).x << endl;  
  

from testApp.cpp -> key pressed

  
	if(key==8 && textString.size()>0) { // backspace  
		textString = textString.substr(0, textString.size()-1); // delete one character  
	} else {  
		textString.append (1, (char)key );  
	}  
	  
	// on enter store on a deque!  
	if ( key==13 ) {   
		  
		enterPressed = true;  
		proceed = true;  
		  
		if (cycle==phase1) {  
			time_t timer;  
			timer=time(NULL);  
			textObject a (textString, cursorPos.x, cursorPos.y, asctime(localtime(&timer)));  
			textData1.add(a);  
			textString="";  
		}  
  

so i think the problem is here?

  
textData1.at(mouseIndex).x=x;    
cout << textData1.at(mouseIndex).x << endl;   

beign the at method:

  
textObject textObjectCollection::at (unsigned n) {    
    return data[n];    
}   

so at is returning a copy of the original inside the collection, you can modify it but that’s not going to modify the original.

to be able to access the original inside the collection you can use a reference:

  
textObject & textObjectCollection::at (unsigned n) {    
    return data[n];    
}   

note the & in the return type, that way instead of returning a copy you are actually returning a reference to the object inside the deque. when calling it later if you do it like:

  
textData1.at(mouseIndex).x=x;    
cout << textData1.at(mouseIndex).x << endl;   

as you were doing it will work but be careful doing:

  
textObject texObj = textData1.at(mouseIndex);  

since that will make a new copy in texObj and you’ll end up having the same problem. the correct will be something like:

  
textObject & texObj = textData1.at(mouseIndex);  

where you have a reference to the return of the call to at in texObj

now It makes sense…

I need to clarify in my head when a copy is passed rather than the actual element - then all will be more clear…

THANKS !

I need to clarify in my head when a copy is passed rather than the actual element

always, unless you use a & for passing or returning a reference or a * for a pointer