Program crashing with std::string

#1

Hello Forum,
I am trying to use std::string for a variable. I declare it in my header and everything seems to be fine, but when I give it a value in the setup method in the cpp file the program crashes. I am including < iostreams > and I will try to avoid using namespace std if possible. Any idea why this could be happening? Thanks!

#2

No error logs? Some code would be helpful, too!

#3

What you described shouldn’t crash. So as ayruos suggested, it could help to see what code you mean.

And/or what the error messages are. Maybe the error is about what you are trying to assign to the string?

I would point out though that you may want to know that OF itself already does using namespace std - maybe that’s throwing you off?

In any case, e.g. this code runs with no errors for me inside an OF project:

    std::string goo;
    goo = "goo";

    string yep1 = "yep";
    string yep2;

    yep2 = "yup";
    yep2 = goo;
#4

To add to what @Drazinut said, the ofToString() function is often helpful when trying to assign strings from other functions returning variables that are not formatted as strings.

#5

Ok, here is the code from my ofApp.h

#pragma once

#include "ofMain.h"
#include <iostream>

class ofApp : public ofBaseApp{

public:
    void setup();
    void update();
    void draw();

    void keyPressed(int key);
    void keyReleased(int key);
    void mouseMoved(int x, int y );
    void mouseDragged(int x, int y, int button);
    void mousePressed(int x, int y, int button);
    void mouseReleased(int x, int y, int button);
    void mouseEntered(int x, int y);
    void mouseExited(int x, int y);
    void windowResized(int w, int h);
    void dragEvent(ofDragInfo dragInfo);
    void gotMessage(ofMessage msg);
    void exit();

    std::string papaya;



};

And here is my ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){

      papaya = "banana";

}

//--------------------------------------------------------------
void ofApp::update(){

}

//--------------------------------------------------------------
void ofApp::draw(){


}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){

}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){

}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){

}

//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){

}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){

}
//--------------------------------------------------------------
void ofApp::exit(){

}

And this is my qbs file (I put on some flags because I’m trying to come around ofxBlackmagic and some original examples from the SDK, I saw them in the original makefile so I copied them):

import qbs
import qbs.Process
import qbs.File
import qbs.FileInfo
import qbs.TextFile
import "../../../libs/openFrameworksCompiled/project/qtcreator/ofApp.qbs" as ofApp

Project{
    property string of_root: '../../..'

    ofApp {
        name: { return FileInfo.baseName(sourceDirectory) }

        files: [
            "src/main.cpp",
            "src/ofApp.cpp",
            "src/ofApp.h",
        ]

        of.addons: [
            'ofxBlackmagic',
        ]

        // additional flags for the project. the of module sets some
        // flags by default to add the core libraries, search paths...
        // this flags can be augmented through the following properties:
        of.pkgConfigs: []       // list of additional system pkgs to include
        of.includePaths: []     // include search paths
        of.cFlags: [
            '-std=c++11' ,
            '-Wno-multichar',
//            '-I',
            '-fno-rtti',
            '-Wall',
            '-g'
        ]           // flags passed to the c compiler
        of.cxxFlags: [
            '-std=c++0x',
            '-pthread'
        ]         // flags passed to the c++ compiler
        of.linkerFlags: [
                        '-lm',
                        '-ldl',
                        '-lpthread',
                        '-lpng'
        ]      // flags passed to the linker
        of.defines: []          // defines are passed as -D to the compiler
        // and can be checked with #ifdef or #if in the code
        of.frameworks: []       // osx only, additional frameworks to link with the project
        of.staticLibraries: [

        ]  // static libraries
        of.dynamicLibraries: [
            /*'/usr/lib/x86_64-linux-gnu/libpng.so',
            '-ldl',
            '-pthread' */
        ] // dynamic libraries

        // create a console window when the application start
        consoleApplication: false

        // other flags can be set through the cpp module: http://doc.qt.io/qbs/cpp-module.html
        // eg: this will enable ccache when compiling
        //
        // cpp.compilerWrapper: 'ccache'

        Depends{
            name: "cpp"
        }

        // common rules that parse the include search paths, core libraries...
        Depends{
            name: "of"
        }

        // dependency with the OF library
        Depends{
            name: "openFrameworks"
        }
    }

    property bool makeOF: true  // use makfiles to compile the OF library
    // will compile OF only once for all your projects
    // otherwise compiled per project with qbs


    property bool precompileOfMain: false  // precompile ofMain.h
    // faster to recompile when including ofMain.h
    // but might use a lot of space per project

    references: [FileInfo.joinPaths(of_root, "/libs/openFrameworksCompiled/project/qtcreator/openFrameworks.qbs")]
}

Thanks for the help!
#6

I forgot the error from qtcreator on Ubuntu 19.04:

13:58:12: Starting /home/antonio/dev/of_v0.10.1_linux64gcc6_release/apps/myApps/BlackmagicTestOf/bin/BlackmagicTestOf...
13:58:14: The program has unexpectedly finished.
13:58:14: The process was ended forcefully.
13:58:14: /home/antonio/dev/of_v0.10.1_linux64gcc6_release/apps/myApps/BlackmagicTestOf/bin/BlackmagicTestOf crashed.
#7

I’d hazard a guess that the error is coming from either the ofxBlackmagic addon or has something to do with QTCreator, both of which I’ve never used.

As you can see in the screenshot, std::string and your code doesn’t have any issues running for me. (Also, I don’t really think including iostream is required).

#8

The string use looks utterly harmless.

And I don’t see anything in the error indicating there was a problem with it. What is leading you to believe it has something to do with assigning the string a value?

If you comment out the string value assignment, does the program not crash?

#9

Yes, basically I have been modifying ofxBlackmagic and I needed an std::string, and when my program started crashing I went back step by step until I noticed that whenever I assigned a value to it the program would crash. I must have done something while modifying the addon.

#10

That sounds like a buggy side-effect of something else.

What happens if you comment out the string assignment, and then just give ofApp an variable int x, and then assign x = 1 in ofApp:Setup()?

#11

Did you try to clean the whole project build and then rebuild? It often happens that the compiler caches parts of previous builds but then at some point it happens that the whole thing gets messed up.

#12

Thank you all for your help. @roymacdonald, how would you do that? I think the problem is there.

#13

I solved it, thanks!

1 Like