-
Notifications
You must be signed in to change notification settings - Fork 3
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory leak problem, help appreciated #22
Comments
Use |
I might make a pull request later. |
So I've been experimenting with adding a destructor for
I added a virtual destructor for
Weird. Someone on stackoverflow says:
|
Hm... |
"Successful" means that both the left and right pointers could be deleted without error. I think the error occurs on deleting of |
Even weirder findings. When I run the following spark code (with the same c++ code):
The segfault isn't even in the destructor for BinOp. Output:
Is the pointer for left and right used elsewhere, after the parent |
Where did you delete the BinOp node? |
Hmm... I didn't delete the BinOp node anywhere new. I don't think the "garbage collector" would delete it. Also, the BinOp node is deleted before the code is executed, so that's why there is a Segmentation fault. |
Ok, I've narrowed the error down to here: |
You are right, I'm pretty sure C++ does not have built-in garbage collection. |
What happens if it is of type |
If I change the minus in the test code
to a plus, the same error occurs: Segmentation fault while evaluating |
On a side note, are you using Visual Studio? |
Yes, I do use Visual Studio (Code). Please tell me how! |
Oh, I'm not sure it works on Visual Studio Code. |
I use kubuntu (linux), XD. I'm checking out the debug tab and the .vscode/launch.json file. |
Ah yes, Linux, the OS that all programmers use xD |
Yep, that's exactly how it works in Visual Studio. |
I see. |
It uses the same debugger actually, but I can't see the parameters through clicking. I already know the call stack from using print statements though. |
It doesn't show the variables either, unfortunately. |
According to the call stack, when running the default test.spark file (not the simplified one I made), |
|
Oops LOL I am really used to saying |
🥳 I've found the error! When I run the following code:
the |
BTW, I outputted the
|
Nice! |
BTW, don't use pointers unless you need to have the same copy of memory. Even if you do, you can use references instead. But if you REALLY need to use pointers, I would recommend smart pointers which automatically delete themselves. |
You are right, I forgot all about smart pointers. |
I'm a cpp noob so I don't know what a union is -_-. I read that the OS reclaims the memory when the process is done, so actually if you aren't creating pointers forever (example: a web server that runs for a long time) you don't necessarily need to delete your pointers. I'm still going to try to optimize memory, though. |
As you might have read in my pull request, I finally have my debugger fully working!
Ok, so the first time In other words, when visiting the |
Oh yeah, I forgot to tell you the test code I ran:
|
Another thing I forgot to tell you: the |
I did realize that, but if the code is very long memory will likely need to be released. |
True. |
True, I don't think the
It seems that the only memory we need to clean up is the objects generated by |
More progress! With the help of my dad, I figured out that the "copy constructor" was being called when using static_cast. That means we can have an attribute called is_copy_constructor and only delete left and right attributes if it is false. Explanation: copy constructors copy the data from one pointer to another. However, the parent addresses are different. Because it goes out of scope, the new pointer made using static_cast is deleted, and so is left and right. However, we don't want this behavior as we will need left and right later. Just check if this pointer was made not using copy constructor before deletion. 🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉 |
We could also change the implementation to just directly use pointers instead of static_cast. |
What would you think of using smart pointers? |
It just changes the way the pointers are created, the operators used are the same. |
It would definitely make for cleaner code 😄 |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Hey guys,
So if you've read my code you might know that there are a couple memory leaks. I've been using C++ for around 2 years now, but pointers are not my forte. If you are more experienced, I would appreciate a couple tips to fixing these leaks, and possibly a pull request. Thanks!
The text was updated successfully, but these errors were encountered: