iOS Adding Custom Files Issue on XCode

I’m having an issue where any time I try to call an oF function (e.g. ofGetWidth()) from a file outside of “ofApp.*” , I get a runtime error. The program will build correctly, but will not run on the simulator. I have the same code running correctly on OSX with openframeworks and can’t seem to pin it down.

Here’s what I think I’m doing wrong: Adding my own files/classes to XCode.

Here are the steps I’ve taken:

1.) Created an empty oF project with the project generator. It built correctly and ran on the simulator correctly.
2.) If I call ofGetWidth()/ofGetHeight() or any other oF method, it works fine. I just print the size of the screen to the middle of the application and run it in the simulator. Works fine.
3.) I create a class called TouchDrawCircle and addd it to XCode. I have tried adding it into “src” and “local_addons”. The code is below, but I am just trying to run a simple draw method.
4.) When I include “TouchDrawCircle.h” in “ofApp.h”, the program builds correctly and then crashes on runtime with the simulator. It always crashes when I’m calling ofGetWidth() from my added TouchDrawCircle.cpp class.

I’m sure I’m making a very silly error, but I can’t figure it out. Any help would be greatly appreciated. Here is the code:

//

//  TouchDrawCircle.hpp

//  touchTest

//

//  Created by Jonathan Honning on 2/13/19.

//



#ifndef TouchDrawCircle_hpp

#define TouchDrawCircle_hpp



#include <stdio.h>

#include "ofMain.h"



class TouchDrawCircle{

    

public:

    TouchDrawCircle();

    void setCoords(int newX, int newY){x = newX; y = newY;}

    void draw();

    

private:

    int radius;

    int x = 0;

    int y = 0;

    

};



#endif /* TouchDrawCircle_hpp */

//

//  TouchDrawCircle.cpp

//  touchTest

//

//  Created by Jonathan Honning on 2/13/19.

//



#include "TouchDrawCircle.hpp"



TouchDrawCircle::TouchDrawCircle()

{

    radius = 5;

    x = ofGetWidth() * 0.5;

    y = ofGetHeight() * 0.5;

}



void TouchDrawCircle::draw()

{

    ofDrawCircle(x,y,radius);

}
//ofApp.h



#pragma once



#include "ofxiOS.h"

#include "TouchDrawCircle.hpp"



class ofApp : public ofxiOSApp{

    

public:

    void setup();

    void update();

    void draw();

    void exit();

    

    void touchDown(ofTouchEventArgs & touch);

    void touchMoved(ofTouchEventArgs & touch);

    void touchUp(ofTouchEventArgs & touch);

    void touchDoubleTap(ofTouchEventArgs & touch);

    void touchCancelled(ofTouchEventArgs & touch);

    

    void lostFocus();

    void gotFocus();

    void gotMemoryWarning();

    void deviceOrientationChanged(int newOrientation);

    

    std::string displayString = "Init";

    bool didLoseFocus = false;

    bool didDoubleTouch = false;

    

    TouchDrawCircle circle;

};
//ofApp.mm
#include "ofApp.h"



//--------------------------------------------------------------

void ofApp::setup(){

    

}



//--------------------------------------------------------------

void ofApp::update(){

    

}



//--------------------------------------------------------------

void ofApp::draw(){

    ofSetColor(0, 0, 0);

    ofDrawBitmapString(displayString, ofGetWidth() * 0.5, ofGetHeight() * 0.5);

    

    circle.draw();

}



//--------------------------------------------------------------

void ofApp::exit(){

    

}



//--------------------------------------------------------------

void ofApp::touchDown(ofTouchEventArgs & touch){

    circle.setCoords(touch.x,touch.y);

    

    if(didDoubleTouch == false)

    {

        displayString = "Touch Down!";

    }

}



//--------------------------------------------------------------

void ofApp::touchMoved(ofTouchEventArgs & touch){

    circle.setCoords(touch.x,touch.y);

    

    displayString = "We're Moving!";

    didDoubleTouch = false;

}



//--------------------------------------------------------------

void ofApp::touchUp(ofTouchEventArgs & touch){

    if(!didDoubleTouch)

    {

        displayString = "Done touching!";

        didDoubleTouch = false;

    }

    

}



//--------------------------------------------------------------

void ofApp::touchDoubleTap(ofTouchEventArgs & touch){

    didDoubleTouch = true;

    displayString = "Touchy touchy!";

}



//--------------------------------------------------------------

void ofApp::touchCancelled(ofTouchEventArgs & touch){

    

}



//--------------------------------------------------------------

void ofApp::lostFocus(){

    didLoseFocus = true;

}



//--------------------------------------------------------------

void ofApp::gotFocus(){

    if(didLoseFocus)

    {

        displayString = "Lost focus but got it back!";

    }

}



//--------------------------------------------------------------

void ofApp::gotMemoryWarning(){

    

}



//--------------------------------------------------------------

void ofApp::deviceOrientationChanged(int newOrientation){

    

}

Did you try cleaning the build folder and then compiling it again? Adding new headers (and a few other things) is making XCode go haywire at the moment when building for iOS.

Ok, so I have cleaned the folder, but the same thing is happening. Something interesting that I’ve noticed is that it only crashes when an oF method (in this case, ofGetWidth()) is called from “ofApp.h”.

The thread of the crashes goes:

0 ofAppiOSWindow::getWindowSize()
1 ofAppiOSWindow::getWidth()
2 ofGetWidth()
3 TouchDrawCircle()::TouchDrawCircle()
4 TouchDrawCircle()::TouchDrawCircle()
5 ofApp::ofApp()
6 ofApp::ofApp()
7 main

So anytime ofGetWidth() gets called in “ofApp.h” or in the constructor for TouchDrawCircle, this error occurs. If I just set the constructor values for x and y to 0, the program actually runs correctly on the simulator:

TouchDrawCircle::TouchDrawCircle()

{

    radius = 5;

   // x = ofGetWidth() * 0.5;
    //y = ofGetHeight() * 0.5;

x = 0;
y = 0;

}

I’m running Mojave 10.14.3 with XCode 10.1 on a late 2014 Mac mini (which is maybe on its last legs)

I can confirm that Xcode crash on ofGetWidth() call from custom class.

FWIW, I run this same exact code on an oF OSX application and it works fine. So maybe this has something to do with linking files and using the simulator?