Global static image reference exception


#1

I receive an exception for the code below in the juce_variant class, line 370 (destructor)
I’m trying to create a static global variable, is this wrong?


#include "../JuceLibraryCode/JuceHeader.h"

static const Image& im = ImageFileFormat::loadFrom(BinaryData::no_cica_png, BinaryData::no_cica_pngSize);


//==============================================================================
class TestApplication  : public JUCEApplication
{
public:
    //==============================================================================
    TestApplication() {
    
        std::cout << "Im: " << im.getHeight() << "\n";
        
    }

    const String getApplicationName()       { return ProjectInfo::projectName; }
    const String getApplicationVersion()    { return ProjectInfo::versionString; }
    bool moreThanOneInstanceAllowed()       { return true; }

    //==============================================================================
    void initialise (const String& commandLine)
    {
        // Add your application's initialisation code here..
    }

    void shutdown()
    {
        // Add your application's shutdown code here..
    }

    //==============================================================================
    void systemRequestedQuit()
    {
        // This is called when the app is being asked to quit: you can ignore this
        // request and let the app carry on running, or call quit() to allow the app to close.
        quit();
    }

    void anotherInstanceStarted (const String& commandLine)
    {
        // When another instance of the app is launched while this one is running,
        // this method is invoked, and the commandLine parameter tells you what
        // the other instance's command-line arguments were.
    }
};

#2

can you load an image globally like that. looks like it’s before things get initialised properly.


#3

I ran into the same segfault just a few days ago. Details are here:

http://www.rawmaterialsoftware.com/viewtopic.php?f=2&t=11296

To summarize: I was told not to use statics.


#4

Thanks for the info. That seams exactly the same issue I had, so I guess no static initialization then 8)