Skip to content

Instantly share code, notes, and snippets.

@mmomtchev
Last active September 8, 2023 22:15
Show Gist options
  • Save mmomtchev/64156d9391984f7695d8556974b1611b to your computer and use it in GitHub Desktop.
Save mmomtchev/64156d9391984f7695d8556974b1611b to your computer and use it in GitHub Desktop.
msvc-exception-lambda-bug
#include <Magick++.h>
#include <exception>
#include <functional>
#include <string>
typedef std::function<const char *()> MakeErr;
class LambdaException : public std::exception {
MakeErr get_err;
public:
LambdaException(MakeErr err) : get_err(err) {}
virtual ~LambdaException(){};
const char *Error() const { return get_err(); }
};
int main() {
try {
try {
throw Magick::Exception("error", NULL);
} catch (const Magick::Exception &e) {
throw LambdaException([e2 = Magick::Exception(e)]() { return e2.what(); });
}
} catch (const LambdaException &e) {
printf("%s\n", e.Error());
}
}
#include <exception>
#include <functional>
#include <string>
typedef std::function<const char *()> MakeErr;
class LambdaException : public std::exception {
MakeErr get_err;
public:
LambdaException(MakeErr err) : get_err(err) {}
virtual ~LambdaException(){};
const char *Error() const { return get_err(); }
};
int main() {
try {
try {
throw std::exception();
} catch (const std::exception &e) {
throw LambdaException([e]() { return e.what(); });
}
} catch (const LambdaException &e) {
printf("%s\n", e.Error());
}
}
@mmomtchev
Copy link
Author

mmomtchev commented Sep 8, 2023

Second case, compile

cl /MTd /I "deps\ImageMagick\Magick++\lib" /I "deps\ImageMagick" crash.cc deps\ImageMagick-Windows\VisualMagick\lib\*.lib user32.lib advapi32.lib gdi32.lib ole32.lib shell32.lib

mismatch between constructors and destructors:
(every line show the this pointer, what() and nested which is NULL)

create2 0000009219AFFC40 error 0000000000000000
copy 0000009219AFFCE0 error 0000000000000000
copy 0000009219AFFC90 error 0000000000000000
copy 000001BFEE3C7C48 error 0000000000000000
copy 000001BFEE3C97D8 error 0000000000000000
delete 000001BFEE3C7C48 error 0000000000000000
delete 0000009219AFFC40 error 0000000000000000
what 000001BFEE3C97D8 error 0000000000000000
error
delete 000001BFEE3C97D8 error 0000000000000000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment