This question already has answers here:
C++ display stack trace on exception
(16 answers)
Closed 8 years ago.
I'm wondering is there any predefined macro or something in C++ that could possible to trace back where the destructor is triggered?
It could be something like this:
class myClass{
myClass();
~myClass();
};
myClass::~myClass(){
printf("Object destroyed in %s.\n", __TRACEBACKMACRO__);
}
int main(){
myClass tempClass;
return 0;
}
It should output something like this:
Object destroyed in main().
It's better to output the scope and namespace information as well.
Additional information:
FUNCTION or func macro seems only work in functions not in structs and classes. reference. Any macros that work in struct and class?
You could use the backtrace library though the avalability depends on the platform and compiler:
with gcc compiler (Linux or MacOS X) to display the stacktrace in C++:
include "execinfo.h" and use backtrace -> backtrace_symbols -> __cxa_demangle
with Windows:
include StackWalker.h and use StackWalker class
Have a look at this article http://oroboro.com/stack-trace-on-crash/ for ulterior details.
Alternatively you could use Boost.Call_stack: http://melintea.github.io/Boost-Call_stack/index.html
Related
Preface and the problem
I'm currently studying C++ programming language and game programming.
At the moment, I'm working on a simple game engine just to practice 'consistency' and architecture of the API, and due to this reason the idea of mimicing C# 'Program' class appeared.
C# Entry point:
class Program
{
static void Main(string[] args)
{
// Do stuff.
}
}
C++ analogue required:
class Program
{
public:
static void Main()
{
// Do stuff. 'args' analogue can be ignored, if necessary.
}
};
Is it possible to somehow, using linker options, redefine entry point to be a static class method?
Related experience and my theories on this topic
The main reason, why I think, this should be possible is described in the following piece of code (that was successfully compiled using mingw-w64).
#include <iostream>
class Main
{
public:
static void Foo() { std::cout << "Main::Foo\n"; }
};
void localFoo() { std::cout << "localFoo\n"; }
void callFunc(void(*funcToCall)())
{
funcToCall();
}
int main()
{
callFunc(localFoo);
callFunc(Main::Foo); // Proves that Main::Foo has the same interface as localFoo.
return 0;
}
(Refers to Win32 API) I abstracted Win32 API into classes and used window procedure as a static member of class. It was absolutely correct to Win32 WNDCLASS and I could even use static members of my class inside this procedure.
Conslusion I made: static fields and methods technically have no differences between global variables and functions, and, since that, they can replace some code, that dates back to C (default entry point, for example).
Notes
Both MinGW and MSVC (Visual Studio or cmd) solutions are acceptable.
The author of the post is extremely grateful for any information provided :3
Is it possible to somehow, using linker options, redefine entry point to be a static class method?
No. Not if you want to use the C++ runtime library, at any rate. main (or WinMain) is called by the runtime library once it has completed initialising itself, and that call is hard-coded in the runtime library itself.
The MSVC linker lets you specify an alternative entry point with the /ENTRY switch (see here), but if you do that you will bypass the runtime library initialisation code and that will break things.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I've searched many answers but none of them can solve my problem, I'm new to c++, this issue is quite wired to me. Below is a simplified extraction of my code.
TestHeader.h:
#ifndef NAMESPACE_TESTHEADER_H_
#define NAMESPACE_TESTHEADER__H_
namespace Namespace {
class TestHeader {
public:
TestHeader(const std::string& str) : anyString_(str) { }
virtual std::string methodOne(const std::string& param) const;
virtual ~TestHeader() { anyString_.clear(); }
protected:
std::string anyString_;
};
}
#endif //NAMESPACE_TESTHEADER__H_
TestHeader.cpp:
#include "TestHeader.h"
using namespace std;
namespace Namespace {
TestHeader::TestHeader(const std::string& str):anyString_(str) { <do something>; }
std::string TestHeader::methodOne(const std::string& param) const
{
return <A string>;
}
TestHeader::~TestHeader() {
anyString_.clear();
}
}
What I did was simply call this line in any other .cpp in my package:
#include "TestHeader.h"
TestHeader testHeader("whatever");
The build failed by throwing
error: undefined reference to 'vtable for Namespace::TestHeader'
the vtable symbol may be undefined because the class is missing its key function
The most weird thing is: if I comment out virtual std::string methodOne(const std::string& str) const; in header and its implementation in .cpp, OR, if I comment out : anyString_(str) after constructor and anyString_.clear(); in destructor together in header only, the build will succeed.
Firstly You should not define the constructor and destructor twice. It shouldn't be compiling as mentioned by Curious in comments
Second I assume that you want don't the class to be abstract as there is no Runtime polymorphism implemented which is the basic use of Virtual functions.
If you don't want the class TestHeader to be abstract remove the virtual keyword which is referring to Virtual Table.C++ compiler inserts Virtual Table for every class having virtual function or class inherited from the class that has virtual functions.
Better study the use of Virtual keyword and then write the code.
Here are quick links for the same
Link 1
Link 2
Also, I think you need to revisit few concepts from Destructor virtual ~TestHeader() { anyString_.clear(); } does not make any sense. In fact, there is no base class which in turn denies the use of Virtual Destructor which is used in case of Inheritance
Firstly, include #include <string> at the top of your header file. I am guessing the error is because you have not linked the object file produced after compiling TestHeader.cpp with the source file that contains the declaration and initialization for the variable named testHeader
Compile these with the following command and you should see a linker error that complains saying that you have multiple definitions for the constructor
g++ -std=c++14 TestHeader.cpp yourfile.cpp
After you see those errors, remove the multiple definitions, either put all your definitions in the cpp file or only put them in one place and then recompile and link with the above command. The linker error should be gone.
This question already has answers here:
Undef a typedef in C++?
(3 answers)
Closed 7 years ago.
To avoid confusion and possible errors, is it possible to hide or undefine a typedef.
I do a lot of c++ and java at the same time. In java, the boolean type is boolean, and in c++ it's bool. The problem is that somewhere in the windows c++ libraries, there is a : typedef unsigned char boolean; That means that, in my c++ code, I mistype the bool type as boolean, it will compile and it could cause unexpected error, because it's an unsigned char instead of a true bool.
So what can I do to hide or undefine the boolean typedef in c++?
Yes it's possible with a precompiler directive like the example below:
typedef int foo;
#define foo not_to_be_used
int main() {
foo a = 1; // error: unknown type name 'not_to_be_used'
}
Basically, this way you cancel the typedef. The code above will issue an error if foo is used below the pre-compiler definition.
Live Demo
This question already has answers here:
How to declare a global variable in C++
(5 answers)
Closed 7 years ago.
EDIT 2:
Solved! Use the code below and it worked!
irrklang::ISoundEngine* engine = irrklang::createIrrKlangDevice();
Just place the code above at the top of the code. (Maybe the next line of include or namespace)
I'm using irrKlang to play audio but I had a problem:
#include <irrKlang.h>
void playSound() {
engine->play2D("src/Click.wav");
}
int main() {
irrklang::ISoundEngine* engine = irrklang::createIrrKlangDevice();
playSound();
engine->drop();
return 0;
}
When I run this code, it show that 'engine' (that in the void) was not declared in this scope.
I test this at int main but it work. The problem is that it only worked at main but not at void.
Anything I can use to fix this error? Or is it a bug?
Thanks in advance.
That is expected. irrklang::ISoundEngine* engine is defined in main function but not in playSound().
A straightforward solution would be to pass engine as an argument
void playSound(irrklang::ISoundEngine* engine) {
engine->play2D("src/Click.wav");
}
and in main call it like this
playSound(engine);
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's this C++ syntax that puts a brace-surrounded block where an expression is expected?
I've just come across this strange C/C++ syntax:
#include <stdio.h>
int main() {
printf("%s",
({
static char b__[129];
b__[0] = 55;
b__[1] = 55;
b__[2] = 0;
b__;
})
);
}
This compiles and runs fine using both gcc and g++ (4.5.2). This is the first time I see something like this, and I wonder what exactly this syntax means. I've tried to Google it, but I have no idea what this construct is called.
They're called statement expressions, it's a GNU extension. In your example the result of the expression is b__.