Problems using static std::string in shared library

1

i'm trying to use a C++ library for mechanical simulations in a Qt application. (Project Chrono , Git Hub source).
Everything was ok until i try to use some parts of the library and get some errors. the part of the problematic code is:

ChGlobal.h

//
// PROJECT CHRONO - http://projectchrono.org
//
// Copyright (c) 2010-2011 Alessandro Tasora
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file at the top level of the distribution
// and at http://projectchrono.org/license-chrono.txt.
//

#ifndef CHGLOBAL_H
#define CHGLOBAL_H

#include <string>
#include "core/ChApiCE.h"

namespace chrono {

/// Set the path to the Chrono data directory (ATTENTION: not thread safe)
ChApi void SetChronoDataPath(const std::string& path);

/// Obtain the current path to the Chrono data directory (thread safe)
ChApi const std::string& GetChronoDataPath();

/// Obtain the complete path to the specified filename, given relative to the
/// Chrono data directory (thread safe)
ChApi std::string GetChronoDataFile(const std::string& filename);

}  // END_OF_NAMESPACE____

#endif

ChGlobal.cpp

//
// PROJECT CHRONO - http://projectchrono.org
//
// Copyright (c) 2010 Alessandro Tasora
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file at the top level of the distribution
// and at http://projectchrono.org/license-chrono.txt.
//

#include <string.h>

#include "physics/ChGlobal.h"

#if defined(_WIN32) || defined(_WIN64)
#include "Windows.h"
#endif

#if defined(__APPLE__)
#include <libkern/OSAtomic.h>
#endif

namespace chrono {

// -----------------------------------------------------------------------------
// Functions for manipulating the Chrono data directory
// -----------------------------------------------------------------------------

static std::string chrono_data_path("../data/");

// Set the path to the Chrono data directory (ATTENTION: not thread safe)
void SetChronoDataPath(const std::string& path) {
    chrono_data_path = path;
}

// Obtain the current path to the Chrono data directory (thread safe)
const std::string& GetChronoDataPath() {
    return chrono_data_path;
}

// Obtain the complete path to the specified filename, given relative to the
// Chrono data directory (thread safe)
std::string GetChronoDataFile(const std::string& filename) {
    return chrono_data_path + filename;
}

}  // END_OF_NAMESPACE____

My code in qt is:

const std::string& teste= chrono::GetChronoDataPath();
qDebug()<<"DataPath:"<<teste.c_str();

I get a strange result:

_________
Debugging starts
DataPath: ata/
Debugging has finished
__________

I only get a part of the string.

Then if try to set a new path using the code:

 const std::string& newpath("c:/");
 chrono::SetChronoDataPath(newpath);

i get the follwing error:

Exception at 0x74dfc42d, code: 0xe06d7363: C++ exception, flags=0x1 (execution cannot be continued) (first chance) at f:\dd\vctools\crt\crtw32\heap\new.cpp:62

Is it because of chrono_data_path is a static variable? I've tried another approaches but nothing works. I'm not a C++ expert maybe i'm missing something,

Thanks

c++
qt
asked on Stack Overflow May 27, 2015 by Rui Sebastião

2 Answers

0

The simplest solution is to convert the library and statically link it with your application.

If you insist on dynamically linking the library, you can use the thread-safe Q_GLOBAL_STATIC mechanism:

// implementation (.cpp)

class chrono_data_path_t : public std::string {
public:
  chrono_data_path_t() : std::string("../data/") {}
};

Q_GLOBAL_STATIC(chrono_data_path_t, chrono_data_path)

namespace chrono {

void SetChronoDataPath(const std::string& path) {
    *chrono_data_path = path;
}

const std::string& GetChronoDataPath() {
    return *chrono_data_path;
}

std::string GetChronoDataFile(const std::string& filename) {
    return *chrono_data_path + filename;
}

} // namespace chrono
answered on Stack Overflow May 27, 2015 by Kuba hasn't forgotten Monica • edited May 28, 2015 by Kuba hasn't forgotten Monica
0

I figured out the problem, i was mixing a debug version of my program with a release version of the library. Cmake configuration of the library is generating the same name for both build versions.

answered on Stack Overflow May 27, 2015 by Rui Sebastião • edited May 28, 2015 by Rui Sebastião

User contributions licensed under CC BY-SA 3.0