Questions tagged [destructor]
A special method in object-oriented programming which is invoked when an object is destroyed
3,051
questions
2
votes
0
answers
56
views
In x86-64 assembly, are C++ destructors implented by calling the destructor function before the function epilogue?
When a function ends, the stack is de-allocated using mov rsp, rbp (leave). Any variable or object within that stack frame is now out of scope.
When an object with a destructor goes out of scope, I ...
1
vote
2
answers
62
views
c++ missing construction and destruction of an object
The following code:
#include <iostream>
#include <string>
using namespace std;
void print(string a) { cout << a << endl; }
void print(string a, string b) { cout << a <...
0
votes
0
answers
22
views
C++ on Arduino: Struct constructor calls own Destructor and destroys itself
I want to create a class to manage data using serial communications, I re-wrote and was able to repeat the same issue.
Issue
I have a structure of data that can allocate memory on the constructor and ...
22
votes
3
answers
862
views
Prospective destructors in C++
I have this code and this outputs the following:
link to the following example
https://godbolt.org/z/z8Pn9GsTv
template <typename T>
struct A1 {
A1() {
std::cout << "...
2
votes
1
answer
67
views
`__attribute__((destructor))` not running in some cases?
I am writing a toy malloc(3) implementation, loaded with LD_PRELOAD, as an exercise. I have a function annotated with __attribute__((destructor)) to dump a list of allocations and their status on ...
0
votes
0
answers
25
views
Removing segmentation fault and working with destructor c++ [duplicate]
This is student.hpp (Base class)
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
class student
{
public:
student(const std::string& name, const std::...
1
vote
1
answer
82
views
What should be included in the destructor in C++
Here is two sample classes and the implementation of destructor for class Foo. Why is m_barvec1 implemented as the way it is in the destructor and what does vector<Bar *> m_barvec1 mean?
class ...
-1
votes
1
answer
28
views
How to proper set up a destructor in C++ with Xcode?
there is something that has been bugging me for a while.
I cannot create a destructor using Xcode (with other IDEs like VS2021 that is no issue).
I get the error:
1. Constructor cannot be redeclared
2....
-1
votes
1
answer
59
views
Difference between a destructor in Python vs C++
How do the contracts of a C++ destructor and a Python destructor differ, especially relating to object lifecycle and when resources are reclaimed? I haven't found a comprehensive side-by-side ...
-1
votes
1
answer
60
views
create a base class object use a derived class constructor c++
Is someone can tell why A a = B(); call constructor fisrt and then destructor immediately?
And why the output like this?
C A
C B
D B
D A
test1 A
D A
class A {
public:
A() {
cout<&...
2
votes
2
answers
79
views
Do Ada 83 exceptions include resource cleanup?
Ada 83 was one of the first languages to have exceptions. (I want to say 'the first', but one thing I have learned from looking into the history of technology is that there is almost always an earlier ...
0
votes
1
answer
28
views
Restoring a C++ stream's exception mask for caller
I am writing a C++ function that takes a std::istream as an argument and reads from it to decode an image. When decoding the image, I want the stream to throw exceptions if some error occurs during ...
0
votes
1
answer
30
views
How to define a C++ class destructor with Cython?
cdef cppclass ObjectDef:
string* name_ptr
ObjectDef():
this.name_ptr = new string(b"John")
~ObjectDef(string _name): # <= cython error on "~"
del ...
0
votes
0
answers
61
views
Memory leak when using dynamic array of objects
/** Sorry if I use terminology incorrectly. */
I have a parent class (Transmatrix) which contains a matrix of complex numbers. I have two of child classes (Doutmatrix and Lmatrix).
I want to use an ...
-2
votes
2
answers
52
views
Linked linked destructor raises segmentation fault [closed]
I am trying to delete a linked list using the destructor.
But this code is giving a segmentation fault:
~Node()
{
Node *current = this ;
Node *previous = NULL;
while(current != NULL)
{
...
0
votes
1
answer
48
views
destructor's unexplained behavior while testing deep/shallow copy in C++
class String
{
private:
char* ptr;
public:
String(const String& s1)
{
int len = strlen(s1.ptr);
ptr = new char[len+1];
strcpy(ptr,s1.ptr);
}
...
0
votes
1
answer
28
views
Avoid object destruction when initializing in local scope, C++
I am working on a networking project and I need to initialize an object in a try-catch block. I want the object, which represents a socket, to close the socket upon destruction, but I must avoid ...
-2
votes
3
answers
78
views
when "B c = a" why the output isnt : "cosntructor A\n constructor B\n copy constructor B"?
Output:
when B c = a why the output isnt :
cosntructor A
constructor B
copy constructor B
instead of
cosntructor A
copy constructor B
?
==============================================================...
-1
votes
1
answer
46
views
Destructor of template class causing segmentation fault
This is the short version template class, with the important methods:
template<class T>
class Matrix
{
protected:
int width;
int height;
T ** values;
public:
Matrix(int width, ...
0
votes
0
answers
14
views
I keep getting undefined reference error when trying to access object of another class, anyone can help me out? [duplicate]
This is the error I receive.
I created the Connected Component Class , with it's header
0
votes
0
answers
44
views
Does Dead Store Elimination work on class member variables in the Destructor?
I'm aware that DSE is a problem for crypto developers who primarily use C, particularly with the reliance on memset. However, would the same be true if a class zeroes out its member variables within ...
1
vote
2
answers
152
views
Delphi Class Destructor
I don't understand how a class destructor works! I read grammar semantics and syntax for a class destructor, but I haven't found many complete code examples.
I tried to create a simple code (see below)...
0
votes
1
answer
86
views
Who is responsible for destructing the block scoped static Singleton instance?
I couldn't understand how the program below compiles successfully.
class SomeClass {
public: /** Singleton **/
static SomeClass &instance() {
static SomeClass singleInstance;
...
0
votes
1
answer
52
views
Should we declare the desctructor of a Singleton class as private?
Providing the simple implementation of a Singleton class below. It is possible for anybody to call the destructor as long as s/he has the reference to the singleInstance.
class SomeClass {
public: /** ...
0
votes
1
answer
45
views
Program.exe has triggered a breakpoint in destructor of linked list class
When I run my program, everything works as expected until the destructor of the LList class. On the line that says delete current;, I get the following error:
BlankConsoleLab.exe has triggered a ...
0
votes
1
answer
71
views
Destructor usage in python __del__()
Will the following not cause issue of freeing memory twice? Why is python3 destroying an object
when it has already been destroyed by the programmer?
class Example:
# Initializing
def ...
0
votes
2
answers
67
views
free(): double free detected in tcache 2, in linked List deletion of a node in c++ and how destructor is working in this code
class Node{
public:
int data;
Node* next;
Node(int d){
data = d;
next = NULL;
}
~Node(){
delete next;
...
-1
votes
1
answer
55
views
Destructors but for functions
New to c++ and possibly a really stupid question, but is there something like a destructor but for functions? I making a sort of game and as you can see that the way I made this program is that theres ...
0
votes
2
answers
97
views
Why isn't the derived class destructor being called?
I was doing some practicing with pointers to derived classes and when I ran the code provided underneath,the output I get is
Constructor A
Constructor B
Destructor A
Could someone tell me why is B::~...
0
votes
1
answer
87
views
SpringBoot: Destroy bean after last use / initialization phase
I have a Closeable bean that I only use during initialization of the application, but not later. It opens a resource that is used during initialization. I would like the close method to be called as ...
0
votes
1
answer
44
views
C++ destructor called timing for returned value
Consider C++ code as below:
struct V {
int s;
V(int s): s(s) {}
~V() { cout << "Destructor\n"; }
};
V f() {
V x(2);
return x;
}
int main(){
V a = f();
cout << &...
0
votes
0
answers
42
views
Will a default Destructor in .cpp preserve auto generation of copy/move [duplicate]
Will the compiler auto generate copy/move functions if the Destructor of an otherwise trivial class is declared in the .hpp file and implemented as =default in the .cpp file?
//header
class ...
1
vote
2
answers
112
views
Is it possible to call a function outside of main()?
I guess my question is stupid, but nevertheless:
In my C++ code I use some legacy C library(XLib). In order to use this library a connection to X server has to be opened first:
::Display* const ...
2
votes
1
answer
1k
views
remove null value while destructoring the data in node js / javascript
I am trying to destructing the function to fetch the return data and store it in and Array
id: data.id,
title: data.title,
otherDetails:[
{
name:"dummy name",
fields: "...
1
vote
0
answers
58
views
c++ using constructor together with _exit() function
I have a question about the _exit() function regarding destructors.
I found that calling _exit() does not call destructors.
In this example of "main.cpp", the destructor of someThing is ...
0
votes
1
answer
49
views
Virtual destructor needed for class which is both derived and base?
Say we have the following:
#include <iostream>
struct A
{
virtual ~A() { std::cout << "destr A\n"; }
};
struct B : A
{
// no need to be virtual?
~B() { std::cout &...
2
votes
2
answers
55
views
Why having both default destructor and vector member prevents class to be "nothrow movable constructible"?
Given the following code:
#include <iostream>
#include <vector>
#include <type_traits>
class Test {
public:
~Test() = default;
std::vector<int> m_vector;
};
int main()...
0
votes
0
answers
34
views
The destructor is not called when pthread is terminated
I have a pthread which I wanted to use to create a class interface
void* DmaInterfaceThread(void* args)
{
sem_wait(&DmaInterfaceSemaphore);
printf("IceNET 2 ---> Dma Interface ...
0
votes
1
answer
38
views
Call destructor upon manual termination of the program [closed]
Suppose I have an instance of a class. Will the destructor be called automatically if I terminate the program manually (say, by pressing the cross icon in the CLI window)? If not, is there any option ...
1
vote
1
answer
22
views
QAC Warning regarding destructor
I have a class defined like somewhat below where copy constructor and assignment operator are deleted. QAC is throwing a warning that "This class has declared default or deleted special members ...
1
vote
1
answer
54
views
How to call destructor of C++ class safely from a Python wrapper class using ctypes?
I built a C++ shared library, that exports functions for constructing, destructing and interacting with an implemented class. I want to write a wrapper class in Python, which loads the compiled .dll ...
1
vote
1
answer
105
views
C++ how to add destructor to anonymous class? [duplicate]
how do you add a destructor to an anonymous class in C++? like in PHP if i want to run something when my class go out of scope it'd be
$foo = new class() {
public $i=0;
public function ...
4
votes
4
answers
298
views
Will destructor delete built-in types and pointer objects?
I'm a c++ beginner and now reading the C++ Primer. I have some problem about the destrucor:
in chapter 13.1.3: "In a destructor, there is nothing akin to the constructor initializer list to ...
0
votes
1
answer
87
views
Pointer being freed was not allocated - destructor
Slowly learning about copy/move constructors, rule of 5 etc. Mixed with not-so-well understanding of usage of pointers/reference, I cannot understand why my destructor throws the error below. I know ...
0
votes
4
answers
106
views
Order of destructing objects (c++)
I want to understand the order that objects are destructed in the end of a scope.
I understand the right order when dealing with inheritance, but I dont understand it when dealing with containment.
...
0
votes
1
answer
64
views
Using Destructor in inheritance
In this inheritance program I create 2 classes which A is parent and B is child class . and i crate cons of both classes and also use
Destructor, and both classes have tow objects . @ MY question is ...
0
votes
0
answers
63
views
Destructor can not find member variable, can not delete memory, leading to memory leaks
I implement a class RedBlackTree like this. My questions are at the end.
template < class T,
class Node = ft::treeNode<T>,
class Type_Alloc = std::allocator<T>,
...
-1
votes
1
answer
125
views
pybind11: segfault on process exit with static py::object
I am using pybind11 to create a module in C++ and then importing it into a Python program. This is running through a normal script in CPython, not an embedded interpreter.
In my module, I have a ...
0
votes
0
answers
21
views
Heap error in delete statement when object is destroyed in C++ [duplicate]
I created a simple StringBuilder class to practice. A similar class is available in other programming languages.
When the destructor of this class is called, I try to delete the memory allocated to ...
2
votes
1
answer
63
views
How to handle object destruction in a custom container?
I'm writing a custom Stack container that stores its elements in a fixed-size array:
template<typename T, uint32 TCapacity>
class Stack {
// Member functions omitted
T mData[TCapacity];
...