![]() |
GNU C++ - wie Speicherfehler abfangen ?
Hallo,
- ich habe eine C++ Klasse im Namensraum (namespace) "qvc". - ich habe eine gleich lautende Klasse QChar, die aber im Framework Qt5 untergebracht ist. - die Qt5 QChar Klasse spreche per ::QChar an : - die Klasse: qvc::QChar * foo = new qvc::QChar(); wird ohne Fehler erzeugt. - im constructor von qvc::QChar erstelle ich eine Referenz zur Qt5 QChar Klasse: mittels: qvc::QChar() { origin_obj = new ::QChar(); } - alles wird ohne Fehler oder Probleme erledigt. - wenn ich jedoch dann im destructor das Objekt "origin_obj" löschen möchte, prüfe ich vorher, das der Wert nicht einen nullpointer entspricht. - wie zu erwarten ist, wird diese Auswertung mit True zurück geliefert und "delete" versucht "origin_obj" zu löschen. - aber genau hier bekomme ich einen Fehler, der aber nicht abgehandelt wird. - ich habe versucht das ganze per try catch abzufangen, aber da passiert rein gar nichts. - die std::wcout << L"... wird nicht ausgegeben. - wie kann man dann Speicherfehler abfangen, die in einer DLL passieren ? - geht das nicht mit exception's ? - wo liegt mein Fehler ?
Code:
namespace "qvc" {
class QChar { private: ::QChar* origin_obj; public: QChar(); ~QChar(); ... }; qvc::QChar::~QChar(void) { #ifdef DEBUG std::wcout << L"cpp: QChar: dtor..." << std::endl; #endif try { if (nullptr != origin_obj) { std::wcout << L"not null" << std::endl; delete origin_obj; } } catch (std::exception &e) { std::wcout << L"Exception: "; std::wcout << e.what() << std::endl; } } } |
AW: GNU C++ - wie Speicherfehler abfangen ?
Ich möchte an dieser Stelle daran erinnern, dass es sich hier um ein Delphi-Forum handelt. Fragen zu Qt können z.B. (nach vorheriger Anmeldung bei Qt) unter
![]() |
AW: GNU C++ - wie Speicherfehler abfangen ?
Wir sehen hier ja nicht, was in deinem Konstruktor abgeht. Die ganze Sache hört sich doch eigentlich so an, als könnte man super ein nachstellbares Minimalbeispiel machen?
delete an sich wirft keine Exception, es sei denn, der Destruktor des Objekts wirft eine. Ich kenne mich mit Qt nicht aus, aber QChar scheint gar keinen zu haben. Damit hätte ich mir erklärt dass dein try..catch nicht greift, wenn das Ding halt dann was wirft, was sich nicht von std::exception ableitet. Zieh das doch mal einer DLL raus, als standalone Anwendung und schau dir die Sache im Debugger an, dann muss man kein printf-Debuggung machen. |
AW: GNU C++ - wie Speicherfehler abfangen ?
Zitat:
Zitat:
Aber am einfachsten ist natürlich, das ganze ohne DLL zu debuggen, das stimmt. Wenn es dann noch das Problem gibt... |
AW: GNU C++ - wie Speicherfehler abfangen ?
- es geht nicht auschließlich um Qt5 an für sich.
- es geht darum, Objekte, die mit new erstellt wurden zu löschen - das ist unabhängig vom Framework und Aufgabe von C++ - einen kleinen Einblick kann man ![]() der try catch Block wird garnicht angetastet. |
AW: GNU C++ - wie Speicherfehler abfangen ?
@paule32.jk , You miss handling QChar
![]() It is not class like all others with constructor and destructor, it is simple 2 bytes type with methods, something like WChar in pascal with record helper, or simply a managed record that have only WChar/Char. No need to delete it, unless you will declare it as referenced (aka dynamic). In other words, if origin_obj ( ![]() |
AW: GNU C++ - wie Speicherfehler abfangen ?
Zitat:
|
AW: GNU C++ - wie Speicherfehler abfangen ?
@Kas Ob.
it seem so. But like I can see, that QChar is a class, with members like "bool isDigit()". And me, when I have understand you, it should possible for C++ to "delete foo;", too. When "foo" is not a Pointer (also when a Pointer to the class TuFoo exists: auto * fifi = new TuFoo(); then you can delete it without problems. I the simmilar same way, you can do: auto * fifi = new int; which give you a "global" fifi reference, that over life the calling stack (it will also accessible outside). So, it should not be a Problem, that I i "free" the allocated memory (under 64-Bit there are 8 Bytes). An other thing is it, if you use class references or integer references in the local scope (without allocating static memory with "new"). So, class TuFoo fifi; will automatically clean/free, if the callee function reach the Epilog. |
AW: GNU C++ - wie Speicherfehler abfangen ?
QChar is 2 bytes structure supplied with methods and default constructor, yes there is a constructor but it is more filling with default value which is 0.
Now, look how you declared it, in your code there is no "*", it is local field and in-place, trying to delete it itself will not be possible. Also see your example posted here against the one in the GitHub
Code:
namespace "qvc" {
class QChar { private: ::QChar* origin_obj; public: QChar(); ~QChar(); ... }; qvc::QChar::~QChar(void) { #ifdef DEBUG std::wcout << L"cpp: QChar: dtor..." << std::endl; #endif try { if (nullptr != origin_obj) { std::wcout << L"not null" << std::endl; delete origin_obj; } } catch (std::exception &e) { std::wcout << L"Exception: "; std::wcout << e.what() << std::endl; } } }
Code:
See the difference ?
namespace qvc {
qvc::QChar::QChar(void ) { origin_obj = new ::QChar( ); } qvc::QChar::QChar(char t) { origin_obj = new ::QChar(t); } qvc::QChar::QChar(uint8_t t) { origin_obj = new ::QChar(t); } qvc::QChar::QChar(uint16_t t) { origin_obj = new ::QChar(t); } qvc::QChar::QChar(uint32_t t) { origin_obj = new ::QChar(t); } qvc::QChar::QChar(wchar_t t) { origin_obj = new ::QChar(t); } qvc::QChar::QChar(short t) { origin_obj = new ::QChar(t); } ... qvc::QChar::~QChar(void) { #ifdef DEBUG std::wcout << L"cpp: QChar: dtor..." << std::endl; #endif try { if (nullptr != origin_obj) { std::wcout << L"not null" << std::endl; delete origin_obj; } } catch (std::exception &e) { std::wcout << L"Exception: "; std::wcout << e.what() << std::endl; } } In all cases, QChar is 2 bytes and this is smaller and lighter than using dynamic one with extra pointer (either 32 or 64 bit, meaning 4 or 8 bytes) with overhead of allocating and deallocating, just slap it where ever you need to use, as static and in place then use it and don't free it, because it does not need to be free. In other words use it as you use int or LONG .... the only difference is it will have initial value, and comes with methods, that is it. |
AW: GNU C++ - wie Speicherfehler abfangen ?
Here found you this
![]() no constructor and no destructor ! |
Alle Zeitangaben in WEZ +1. Es ist jetzt 16:38 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz