Class destructors and pointer deallocation - c++

I am writing a class (virtual_flight_runtime_environment), and it is mostly non-static, with the exception of one static function for the purposes of a Win32 thread using it as its function. The class declares struct simaircraftdata* aircraftdata (a data struct), and calls 'aircraftdata = new aircraftdata;' in the constuctor (public: virtual_flight_runtime_environment()).
My question is about destructors and memory deallocation. I have written the destructor as such:
~virtual_flight_runtime_environment(void) {
/*..Other code, i.e. closing win32 handles, etc.*/
delete aircraftdata;
}
Now, the class is declared in another function (the DoWork function of a .Net background worker) like so:
virtual_flight_runtime_environment* this_environment = new virtual_flight_runtime_environment;
And just before the end of the function, I call 'delete this_environment;'. Immediately after, 'this_environment' would have gone out of scope, and the desturctor should have been called.
Is this correct? I do notice continued increases in memory usage over time, and I'm wondering if I've done something wrong. Does calling delete on a pointer just make it a null pointer, or does it deallocate the data at the end of it?
Any advice would be appreciated,
Collin Biedenkapp

There is no direct connection between a delete in your program and whether it will directly be visible in say the task manager because the OS tries to optimize memory utilization. When you look in the task manager you will typically see the working set size of your application, this is a measure of how much memory your app has requested but not necessarily how much it is currently using.
And to your question, yes deleting the memory as you did it is the WTG although as others have pointed out using smart pointers is generally much better to handle memory to avoid later headaches.

You're almost correct.
delete this_environment calls the destructor of virtual_flight_runtime_environment. The destructor executes delete aircraftdata.
Right after that, the memory occupied by the instance of virtual_flight_runtime_environment is released.
Please be aware that delete statement doesn't set the pointer to NULL.
So I see no problem in your code given the information in the question.

This looks correct.
Calling delete on your this_environment will cause the destructor of that class to be called before it's memory is deallocated. The destructor deletes the aircraft data. Looks correct.
You might consider instead of having a member variable containing a raw pointer to aircraftdata instead using an auto_ptr or in c++11 unique_ptr which will ensure that it gets deleted automatically when it's containing class is constructed. Look it up, this isn't the place to teach it and it's just a suggestion, not a necessity.
Edit: And I also agree with Pete Becker's comment on the question, which is to question if this needs a pointer at all.

You should indeed explicitely call "delete this_environment". Otherwise, it's only the pointer itself (which exists on the stack) which would be destroyed. The data pointed, on the heap, would still exist.
The other solution is to simply get rid of the pointer and declare your variable as such :
virtual_flight_runtime_environment this_environment;
That way, your object will directly exist on the stack, and its lifetime will depend on the scope in which it is declared - and the destructor will be automatically called at this point, calling in turn the delete for your internal data.

Related

When should I provide a destructor for my class?

This seems like a rather trivial or at least common question, but I couldn't find a satisfying answer on google or on SO.
I'm not sure when I should implement a destructor for my class.
An obvious case is when the class wraps a connection to a file, and I want to make sure the connection is closed so I close it in the destructor.
But I want to know in general, how can I know if I should define a destructor. What guidelines are there that I can check to see if I should have a destructor in this class?
One such guideline I can think of, is if the class contains any member pointers. The default destructor would destory the pointers on deletion, but not the objects they're pointing at. So that should be the work of a user-defined destructor. E.g: (I'm a C++ newbie, so this code might not compile).
class MyContainer {
public:
MyContainer(int size) : data(new int[size]) { }
~MyContainer(){
delete [] data;
}
// .. stuff omitted
private:
int* data;
}
If I hadn't supplied that destructor, than destroying a MyContainer object would mean creating a leak, since all the data previously referenced by data wouldn't have been deleted.
But I have two questions:
1- Is this the only 'guideline'? I.e. define a destructor if the class has member pointers or if it's managing a resource? Or is there anything else?
2- Are there cases when I should not delete member pointers? What about references?
You need to define a destructor if the default destruction does not suffice. Of course, this just punts the question: what does the default destructor do? Well, it calls the destructors of each of the member variables, and that's it. If this is enough for you, you're good to go. If it's not, then you need to write a destructor.
The most common example is the case of allocating a pointer with new. A pointer (to any type) is a primitive, and the destructor just makes the pointer itself go away, without touching the pointed to memory. So the default destructor of a pointer does not have the right behavior for us (it will leak memory), hence we need a delete call in the destructor. Imagine now we change the raw pointer to a smart pointer. When the smart pointer is destroyed, it also calls the destructor of whatever its pointing to, and then frees the memory. So a smart pointer's destructor is sufficient.
By understanding the underlying reason behind the most common case, you can reason about less common cases. It's true that very often, if you're using smart pointers and std library containers, their destructors do the right thing and you don't need to write a destructor at all. But there are still exceptions.
Suppose you have a Logger class. This logger class is smart though, it buffers up a bunch of messages to Log, and then writes them out to a file only when the buffer reaches a certain size (it "flushes" the buffer). This can be more performant than just dumping everything to a file immediately. When the Logger is destroyed, you need to flush everything from the buffer regardless of whether it's full, so you'll probably want to write a destructor for it, even though its easy enough to implement Logger in terms of std::vector and std::string so that nothing leaks when its destroyed.
Edit: I didn't see question 2. The answer to question 2 is that you should not call delete if it is a non-owning pointer. In other words, if some other class or scope is solely responsible for cleaning up after this object, and you have the pointer "just to look", then do not call delete. The reason why is if you call delete and somebody else owns it, the pointer gets delete called on it twice:
struct A {
A(SomeObj * obj) : m_obj(obj){};
SomeObj * m_obj;
~A(){delete m_obj;};
}
SomeObj * obj = new SomeObj();
A a(obj);
delete obj; // bad!
In fact, arguably the guideline in c++11 is to NEVER call delete on a pointer. Why? Well, if you call delete on a pointer, it means you own it. And if you own it, there's no reason not to use a smart pointer, in particular unique_ptr is virtually the same speed and does this automatically, and is far more likely to be thread safe.
Further, furthermore (forgive me I'm getting really into this now), it's generally a bad idea to make non-owning views of objects (raw pointers or references) members of other objects. Why? Because, the object with the raw pointer may not have to worry about destroying the other object since it doesn't own it, but it has no way of knowing when it will be destroyed. The pointed to object could be destroyed while the object with the pointer is still alive:
struct A {
SomeObj * m_obj;
void func(){m_obj->doStuff();};
}
A a;
if(blah) {
SomeObj b;
a.m_obj = &b;
}
a.func() // bad!
Note that this only applies to member fields of objects. Passing a view of an object into a function (member or not) is safe, because the function is called in the enclosing scope of the object itself, so this is not an issue.
The harsh conclusion of all this is that unless you know what you're doing, you just shouldn't ever have raw pointers or references as member fields of objects.
Edit 2: I guess the overall conclusion (which is really nice!) is that in general, your classes should be written in such a way that they don't need destructors unless the destructors do something semantically meaningful. In my Logger example, the Logger has to be flushed, something important has to happen before destruction. You should not write (generally) classes that need to do trivial clean-up after their members, member variables should clean up after themselves.
A class needs a destructor when it "owns" a resource and is responsible for cleaning it up. The purpose of the destructor is not simply to make the class itself work properly, but to make the program as a whole work properly: If a resource needs to be cleaned up, something needs to do it, and so some object should take responsibility for the cleanup.
For instance, memory might need to be freed. A file handle might need to be closed. A network socket might need to be shut down. A graphics device might need to be released. These things will stay around if not explicitly destroyed, and so something needs to destroy them.
The purpose of a destructor is to tie a resource's lifetime to an object's, so that the resource goes away when the object goes away.
A Destructor is useful for when your classes contain Dynamically Allocated Memory. If your classes are simple and don't have 'DAM', then it's safe to not use a Destructor. In addition, read about the Rule Of Three. You should also add a copy constructor and an overloaded = operator if your class is going to have 'DAM'.
2) Do not worry about References. They work in a different way such as that it "Refers" to another variable (Which means they don't point to the memory).

Object deletion from heap space

In main function I create an object using new and don't delete it.I hope the heap space would be cleared once the process exits .The below is a sample code where object of class A is a member variable of class B. Class B also has a multimap as a member variable.
Class A
{
Public:
A(); //have definition in cpp file
~A();//have definition in cpp file
Private:
Int a;
};
Class B{
Private:
Std::multimap<string,string> map_test;
Public:
A a;
B(); //have definition inn cpp file
~B();//does not have any definition in cpp file
};
int main()
{
B *b = new B();
/* code section where it fills some 1000 key value pairs in the multimap
for some purpose */
return 0;
}
My understanding:
Even if i do not delete the object here, it won't create any issue as the heap space would be cleaned once the process exits.As my scope of the programme is limited as above and nobody else is going to reuse this.So is it good or bad not to use delete? What's your suggestion on this?
It should call the object's default destructor which then calls the implicit multimap destructor. So its not needed to explicitly clear the multimap.Please correct me if i am wrong.
In parent class it just declares the destructor and does not have any definition.So will it call implicit destructor or it will ignore calling it?(There is no reason of not defining it, just asking for better understanding. )
If it calls implicit destructor in case of parent class, should it call the child class destructor which is defined here?
As the parent class object is instantiated using new, it would be created in heap.Then where exactly the member variables of this parent object would be stored.For example object "a" is a member variable and by looking into the declaration of this member object, seems like it would be created in stack. I am just confused here how the parent object and its member child object exact memory creation happens. Can you please help me to understand this?
Yes, as long as your object is created in main.
However, if you ever want to alter this and for example create multiple instances of B, or use it inside another class, etc etc etc, that's a different story. Also, memory checking tools like valgrind are going to give you false positives on a new w/o delete which you will be tempted to ignore. But then, you may ignore a true memory leak if it becomes a habit.
Correct, now if it was a map<string, string*> then you would likely need to clean up
It will call default destructor
Yes it will
I guess, you are asking where base class member variables are stored ? They are also stored on the heap. They precede derived class fields in memory.
You have asked many question.
First its best practice to alwsys clear your memory even if the process exits and clears all the memory ( as it does) . Always handle it..its easy to do usining shared_ptr...
Destructors always get called in the right order however you multimap is a dangeorus as you should clear the elements in the multimap as ifyou store pointers it can cause a serious leak
Most OSs will clean up a process's heap space when the process exits. I wouldn't be surprised if there are a bunch of embedded OSs out there where that isn't the case, plus it is still bad practise as you do have a memory leak.
As you're leaking the memory, no destructors will be called. That said, if you're not implementing a destructor and rely on the compiler-generated default destructor, you should not declare it either - in fact I'm surprised that you're not getting a linker error. And yes, the default destructor for the multimap will delete the contents of the multimap as long as you observe the container's requirements for value semantics (ie, no raw pointers).
It is bad practise and as a programmer you should really make sure that you always manage your resources properly. Also, I don't see any reason for heap allocating b, you can just create the object on the stack and you won't have to think about resource management.
If you do declare it you'll have to provide an implementation as the declaration of the destructor will implicitly disable the compiler generated destructor. That's why I said above that I'm surprised you're not getting a link error.
The compiler will take care of the destruction of the base classes by walking the hierarchy and calling the destructors in reverse order of construction.
No, the whole object containing both A and B will be constructed on the heap - that's why you can alias a pointer to B as a pointer to A and interact with the derived class as if it was the base class.
Fact : there's no native (under the table) garbage collection in C++, though there are many things that can enforce some sort of garbage collection.
Thus, in your code, you have a memory leak. At the end of the scope in which you allocate a B, the memory you allocated _is not free'd.
Going through your list of questions :
No, if you don't delete the pointer, the memory won't be free'd. There are a few things that you can do regarding this topic :
Use the RAII idiom (Resource acquisition is initialization), i.e. use objects to manage memory :
Use smart pointers : these constructs enforce RAII (the most common in my opinion being std::shared_ptr and std::unique_ptr) and make sure the memory they are responsible for is correctly free'd.
It depends. If you allocated objects using the new operator, then inserted them in the map, but they aren't referenced anywhere else, then you should delete manually every entry of the map. It doesn't apply in this case because the map types aren't pointers.
The class could even omit a destructor declaration. If they are omitted, destructors (but also copy assignement operator, copy constructor and a default constructor) are generated by the compiler.
Edited: It depends, if you declared the member A a;, you don't have to explicitly delete it, its destructor will be called when the class that declares it as a member has its destructor called. but if it's a pointer that you allocated (e.g. in the constructor), then you have to delete it in the destructor.
Once you use dynamic memory allocation for an object, the whole object is on the heap, no matter how his members are declared.

Leaving "zombie" flag upon object destruction

I want to explicitly destroy an object (call the destructor on it and all its fields), but it may happen that I still hold some pointers to the object in question. Thus, I don't want to yet free the memory; instead I would like to leave a sort of a flag "I am a destroyed object".
I came with an idea of the following approach:
class BaseClass { //all objects in question derive from this class
public:
BaseClass() : destroyed(false) {}
virtual ~BaseClass() {}
private:
bool destroyed;
public:
bool isDestroyed() { return destroyed; }
void destroy() {
this->~BaseClass(); //this will call the virtual destructor of a derivative class
new(this) BaseClass();
destroyed=true;
}
};
When destroy is called, I basically destroy the whatever object I had (perhaps a derivative one) and create a new "zombie" one in that very same place. As a result I hope to achieve:
Any other pointer ptr previously pointing to this object can still call ptr->isDestroyed() to verify its existence.
I am aware that if I don't check the flag of the zombie and try to access fields belonging to any derived object, bad things may happen
I am aware that the zombie object still consumes as much memory as the destroyed object (as it may be a derivative of BaseClass)
I still have to free memory of the destroyed object. I hope however, that calling delete is still correct?
Questions:
Are there any other problems which I should consider when using the above pattern?
Will calling delete on the zombie object correctly free whole memory consumed by the previous (normal) object?
While I appreciate your input on how to do it differently, and I may be inclined to do it your way - I would still like to understand all the risks that the above code poses.
You got some nasty comments to your question. Now I don't think they are deserved although there may be better ways to do what you want. I understand where you are coming from but actually you are using the destructor the same way you would use the reset function you refuse to write. Actually you gain nothing from calling a destructor since calling a distructor has nothing to do with actually deleting or resetting anything unless you actually write the code to do it within the destructor.
As to your question about the placement new:
As you may know already the placement new doesn't allocate any memory so calling it will just create the object in the same place. I understand that is exactly what you want but it's just not ncessary. Since you don't call delete on your object just destroy, you can set destroyed to true without initializing the class.
To sum it up:
If you use the destructor as a regular virtual function you gain nothing. Don't do it since you can get into trouble if a destructor is called twice
A call to a placement new will not allocate memory and just perform needless initialization. You can just set destroyed to true.
To do what you want to do correctly and gain the benefits of destructors, you should overload the new and delete operators of your classes and use the normal destruction mechanism. You can then opt not to release the memory but mark it as invalid or maybe release most of the memory but leave the pointer pointing to some flags.
EDIT
Following the comments I decided to sum up all the risks I see and the risks that others have pointed out:
Accessing invalid pointer in a multi-threaded environment: Using your method a class may be accessed after the destructor has run but before the destroyed flag is set (As to your question in one of the comments - shared_ptr is for most purposes thread safe)
Relaying on a behavior you don't totally control: Your method relies on the way destructors auto call the destructors of other members which are not dynamically allocated: This means that you still have to release dynamically allocates memory specifically, You have no control on how exactly this is implemented, You have no control on the order in which other destructors are called.
Relaying on a behavior you don't totally control (Point 2): You are relaying on the way a compiler implements the part of the destructor which calls other destructors you have no way in telling whether your code will be portable or even how will it handle calling it twice.
Destructors may be called twice: Depending on your implementation this may cause memory leaks or heap corruption unless you guard against releasing the same memory twice. You claim you guard against that case by calling the placement new - However in a multi-threading environment this is not guaranteed further more you assume that all memory allocations are done by the default constructor - depending on your specific implementation this may or may not be true.
You are going against the better judgment of everyone that answered your question or commented on it - You may be onto something genius but most probably you are just shooting yourself in the leg by limiting your implementation to a small subset of situations where it will work correctly. It is like when you use the wrong screwdriver you will eventually end up damaging the screw. In the same way using a language construct in a way it was not intended to be used may end up with a buggy program - Destructors are intended to be called from delete and from the code generated by the compiler to clear the stack. Using it directly is not wise.
And I repeat my suggestion - overload delete and new for what you want
There is a suggestion of using smart pointers. In fact - I am doing that, but my references are circular. I could use some fully-fledged garbage collectors, but since I know myself where (and when!) circle chains can be broken, I want to take advantage of that myself.
Then you can explicitly null-ify (reset if you are using shared_ptr) one of the circular smart pointers and break the cycle.
Alternatively, if you know where the cycles will be in advance, you should also be able to avoid them in advance using weak_ptr in place of some of the shared_ptrs.
--- EDIT ---
If an object referenced by weak pointers only would merely get flagged as "invalid" and release control over all its contained pointers (is this sentence clear?), I would be happy.
Then weak_ptr::expired should make you happy :)
As with everyone else, I recommend you should just use weak_ptr. But you asked why your approach doesn't work as well. There are some issue of elegant implementation and separation of concerns that your code walks all over, but I won't argue those. Instead, I'll just point out that your code is horribly not thread-safe.
Consider the following execution sequence of two threads of control:
// Thread One
if ( ! ptr -> isDestroyed() ) { // Step One
// ... interruption ...
ptr -> something(); // Step Three
And the other:
// Thread Two
ptr -> destroy(); // Step Two
By the time step 3 comes around, the pointer is no longer valid. Now it's possible to fix this by implementing lock() or similar, but now you've incurred the possibility of defects about not releasing locks. The reason that everyone is recommending weak_ptr is that this whole class of problems has been worked out both in the interface of the class and its implementations.
One issue remains. You seem to want a facility where you can kill an object at will. This is the tantamount to requiring that the only pointers to an object are weak ones, that no strong ones are present that would break when the object was manually deleted. (I'll stipulate that this isn't a bad idea, though I must say I don't know why it's not in your case.) You can get this by building on top of weak_ptr and shared_ptr. These classes are generic, so if you want to disallow shared_ptr access to BaseClass, then you can write a specialization for shared_ptr<BaseClass> that behaves differently. Hide one instance of shared_ptr<BaseClass> to prevent deletion and provide such pointers through a factory method under your control.
In this model the semantics of destroy() need attention. The first choice is whether you want synchronous or asynchronous operation. A synchronous destroy() would block until all external pointers are released and not allow the issuing of new ones. (I'll assume that copy constructors are already disabled on the pointer.) There are two kinds of asynchronous destroy(). The simpler of the two fails if there still exist external references. Calling unique() on the hidden shared_ptr() makes this an easy implementation. The more complicated one acts like an asynchronous I/O call, scheduling the destruction to happen at some point in the future, presumably as soon as all external references are gone. This function might be called mark_for_death() to reflect the semantics, since the object might or might not be destroyed at return time.
I would consider using one appropriate of the smart pointer patterns instead. Behavior of accessing a deleted object is still undefined and a 'zombie' flag won't really help. The memory that was associated with the object instance that was deleted could be immediately occupied by any other object created, thus accessing the zombie flag isn't an information you can trust.
IMHO the placement new operator
new(this) BaseClass();
used in your destroy() method won't really help. Depends a bit how this method is intended to be used. Instead of deleting derived objects or inside destructors of deleted objects. In the latter case the memory will be freed anyway.
UPDATE:
According to your edit, wouldn't it be better to use the shared pointer/weak pointer idiom to solve the occurrence of circular references. I would consider these as a design flaw otherwise.

Is "delete this" a bad idea? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is it safe to delete this?
I've been doing a little work on a class that's designed to act as a node in a linked list, and I figured I'd give the class its own deletion function as opposed to the managing class doing it. So basically it goes like this:
void Class::Delete() {
//Some cleanup code before deleting the object
delete this;
}
Now I've tested this and it appears to work fine, but I've had a problem in the past where objects have been in the middle of running code, been deleted, then obviously crashed the program by trying to use a no-longer-existing object.
Since "delete this" is right at the end of the function, it obviously exits the function and works fine, but is this sort of practice a bad idea at all? Could this ever blow up in my face if I'm not careful?
The FAQlite answers this quite well:
As long as you're careful, it's OK for
an object to commit suicide (delete
this).
Here's how I define "careful":
You must be absolutely 100% positive sure that this object was
allocated via new (not by new[], nor
by placement new, nor a local object
on the stack, nor a global, nor a
member of another object; but by plain
ordinary new).
You must be absolutely 100% positive sure that your member
function will be the last member
function invoked on this object.
You must be absolutely 100% positive sure that the rest of your
member function (after the delete this
line) doesn't touch any piece of this
object (including calling any other
member functions or touching any data
members).
You must be absolutely 100% positive sure that no one even touches
the this pointer itself after the
delete this line. In other words, you
must not examine it, compare it with
another pointer, compare it with NULL,
print it, cast it, do anything with
it.
Naturally the usual caveats apply in
cases where your this pointer is a
pointer to a base class when you don't
have a virtual destructor.
Basically, you need to take the same care as you do with deleteing any other pointer. However, there are more areas where things can go wrong with a member function committing suicide, compared with an explicitly-declared pointer.
Using delete this is a bad idea if one is not sure of the pitfalls & working around them.
Once you call delete this the object's destructor is going to be invoked and the dynamically allocated memory will be freed.
If the object was not allocated using new, it will be a Undefined behaviour.
If any of object's data members or virtual functions are accessed after delete this, the behaviour will be Undefined Behavior again.
Probably, It is best to avoid delete this given the above.
It's actually a frequent idiom, and about as safe as any delete. As
with all deletes, you have to ensure that no further code tries to
access the object, and you have to be sure that the object was
dynamically allocated. Typically, however, the latter is not a
problem, since the idiom is only relevant for objects which have a
lifetime determined by the semantics of the object, and such objects are
always allocated dynamically. Finding all of the pointers too the
object can be an issue (whether delete this is used or not); usually,
some form of the observer pattern will be used to notify all interested
parties that the object will cease to exist.
The idiomatic way of doing that in C++ is by putting the cleanup code in the destructor then let it be called automatically when you delete the object.
Class::~Class() {
do_cleanup();
}
void ManagingClass::deleteNode(Class* instance) {
delete instance; //here the destructor gets called and memory gets freed
}
There's a simple way of doing the same thing that doesn't involve undefined behavior:
void Class::Delete() {
//Some cleanup code before deleting the object
std::auto_ptr delete_me(this);
}

Deleting an object in C++

Here is a sample code that I have:
void test()
{
Object1 *obj = new Object1();
.
.
.
delete obj;
}
I run it in Visual Studio, and it crashes at the line with 'delete obj;'.
Isn't this the normal way to free the memory associated with an object?
I realized that it automatically invokes the destructor... is this normal?
Here is a code snippet:
if(node->isleaf())
{
vector<string> vec = node->L;
vec.push_back(node->code);
sort(vec.begin(), vec.end());
Mesh* msh = loadLeaves(vec, node->code);
Simplification smp(msh);
smp.simplifyErrorBased(errorThreshold);
int meshFaceCount = msh->faces.size();
saveLeaves(vec, msh);
delete msh;
}
loadleaves() is a function that reads a mesh from disk and creates a Mesh object and returns it.(think of vec and node->code are just information about the file to be opened)
Should I remove the delete msh; line?
Isn't this the normal way to free the memory associated with an object?
This is a common way of managing dynamically allocated memory, but it's not a good way to do so. This sort of code is brittle because it is not exception-safe: if an exception is thrown between when you create the object and when you delete it, you will leak that object.
It is far better to use a smart pointer container, which you can use to get scope-bound resource management (it's more commonly called resource acquisition is initialization, or RAII).
As an example of automatic resource management:
void test()
{
std::auto_ptr<Object1> obj1(new Object1);
} // The object is automatically deleted when the scope ends.
Depending on your use case, auto_ptr might not provide the semantics you need. In that case, you can consider using shared_ptr.
As for why your program crashes when you delete the object, you have not given sufficient code for anyone to be able to answer that question with any certainty.
Your code is indeed using the normal way to create and delete a dynamic object. Yes, it's perfectly normal (and indeed guaranteed by the language standard!) that delete will call the object's destructor, just like new has to invoke the constructor.
If you weren't instantiating Object1 directly but some subclass thereof, I'd remind you that any class intended to be inherited from must have a virtual destructor (so that the correct subclass's destructor can be invoked in cases analogous to this one) -- but if your sample code is indeed representative of your actual code, this cannot be your current problem -- must be something else, maybe in the destructor code you're not showing us, or some heap-corruption in the code you're not showing within that function or the ones it calls...?
BTW, if you're always going to delete the object just before you exit the function which instantiates it, there's no point in making that object dynamic -- just declare it as a local (storage class auto, as is the default) variable of said function!
Isn't this the normal way to free the memory associated with an object?
Yes, it is.
I realized that it automatically invokes the destructor... is this normal?
Yes
Make sure that you did not double delete your object.
if it crashes on the delete line then you have almost certainly somehow corrupted the heap. We would need to see more code to diagnose the problem since the example you presented has no errors.
Perhaps you have a buffer overflow on the heap which corrupted the heap structures or even something as simple as a "double free" (or in the c++ case "double delete").
Also, as The Fuzz noted, you may have an error in your destructor as well.
And yes, it is completely normal and expected for delete to invoke the destructor, that is in fact one of its two purposes (call destructor then free memory).
saveLeaves(vec,msh); I'm assuming takes the msh pointer and puts it inside of vec. Since msh is just a pointer to the memory, if you delete it, it will also get deleted inside of the vector.
Just an update of James' answer.
Isn't this the normal way to free the memory associated with an object?
Yes. It is the normal way to free memory. But new/delete operator always leads to memory leak problem.
Since c++17 already removed auto_ptr auto_ptr. I suggest shared_ptr or unique_ptr to handle the memory problems.
void test()
{
std::shared_ptr<Object1> obj1(new Object1);
} // The object is automatically deleted when the scope ends or reference counting reduces to 0.
The reason for removing auto_ptr is that auto_ptr is not stable in case of coping semantics
If you are sure about no coping happening during the scope, a unique_ptr is suggested.
If there is a circular reference between the pointers, I suggest having a look at weak_ptr.