A way to recover memory leak from dynamic allocation in C++?What is meant by Resource Acquisition is Initialization (RAII)?What strategies and tools are useful for finding memory leaks in .NET?Easiest way to convert int to string in C++C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Creating a memory leak with JavaDifferent ways of leaking memoryWhy is reading lines from stdin much slower in C++ than Python?Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionWhat is the difference between 'typedef' and 'using' in C++11?Why should I use a pointer rather than the object itself?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviations
How to search for Android apps without ads?
Is it a bad idea to have a pen name with only an initial for a surname?
Why can't we feel the Earth's revolution?
XML Query Question
How to sort human readable size
what is "dot" sign in the •NO?
How do I become a better writer when I hate reading?
How to ask if I can mow my neighbor's lawn
Should I email my professor to clear up a (possibly very irrelevant) awkward misunderstanding?
Why do you need to heat the pan before heating the olive oil?
Is it possible for underground bunkers on different continents to be connected?
How to make all magic-casting innate, but still rare?
What are the mechanical differences between Adapt and Monstrosity?
Is there a term for someone whose preferred policies are a mix of Left and Right?
How did Avada Kedavra get its name?
Using roof rails to set up hammock
How did the European Union reach the figure of 3% as a maximum allowed deficit?
What is the color associated with lukewarm?
What is the context for Napoleon's quote "[the Austrians] did not know the value of five minutes"?
How can I maintain game balance while allowing my player to craft genuinely useful items?
Redirecting output only on a successful command call
Digital signature that is only verifiable by one specific person
Can you cover a cube with copies of this shape?
What could be the physiological mechanism for a biological Geiger counter?
A way to recover memory leak from dynamic allocation in C++?
What is meant by Resource Acquisition is Initialization (RAII)?What strategies and tools are useful for finding memory leaks in .NET?Easiest way to convert int to string in C++C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Creating a memory leak with JavaDifferent ways of leaking memoryWhy is reading lines from stdin much slower in C++ than Python?Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionWhat is the difference between 'typedef' and 'using' in C++11?Why should I use a pointer rather than the object itself?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviations
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
If I write:
b = new int;
b = new int;
delete b;
I know that only the second b
gets deleted from memory. Since there is nothing pointing to the first b
(due to the deletion of 2nd b
), I have a memory leak.
In such cases, I know that there is a way to recover this memory leak in C# and Java. Is there a way to recover this memory leak in C++?
c++ database memory memory-leaks heap
|
show 1 more comment
If I write:
b = new int;
b = new int;
delete b;
I know that only the second b
gets deleted from memory. Since there is nothing pointing to the first b
(due to the deletion of 2nd b
), I have a memory leak.
In such cases, I know that there is a way to recover this memory leak in C# and Java. Is there a way to recover this memory leak in C++?
c++ database memory memory-leaks heap
Not while adhering to Standard C++. Once you lose the last reference to a dynamic allocation, you're hosed.
– user4581301
Mar 25 at 3:59
@user4581301 I see. Thanks.
– John Park
Mar 25 at 4:39
No worries. The take-away is avoid raw dynamic allocation. Use containers and smart pointers to handle the memory for you. If you can't, wrap the allocation with RAII and observe the Rule of Three or Five.
– user4581301
Mar 25 at 4:45
The deletion of the "secondb
" isn't what causes the "firstb
" to be leaked. The second assignment ofb = new int
is what does that, whether followed bydelete b
or not. Thedelete b
simply releases the result of the secondnew
expression.
– Peter
Mar 25 at 4:50
@user4581301 Right. I really need to get more comfortable with using smart pointers. I think one of my professors talked about it as well.
– John Park
Mar 25 at 5:21
|
show 1 more comment
If I write:
b = new int;
b = new int;
delete b;
I know that only the second b
gets deleted from memory. Since there is nothing pointing to the first b
(due to the deletion of 2nd b
), I have a memory leak.
In such cases, I know that there is a way to recover this memory leak in C# and Java. Is there a way to recover this memory leak in C++?
c++ database memory memory-leaks heap
If I write:
b = new int;
b = new int;
delete b;
I know that only the second b
gets deleted from memory. Since there is nothing pointing to the first b
(due to the deletion of 2nd b
), I have a memory leak.
In such cases, I know that there is a way to recover this memory leak in C# and Java. Is there a way to recover this memory leak in C++?
c++ database memory memory-leaks heap
c++ database memory memory-leaks heap
edited Mar 25 at 3:59
user4581301
22k52136
22k52136
asked Mar 25 at 3:55
John ParkJohn Park
66
66
Not while adhering to Standard C++. Once you lose the last reference to a dynamic allocation, you're hosed.
– user4581301
Mar 25 at 3:59
@user4581301 I see. Thanks.
– John Park
Mar 25 at 4:39
No worries. The take-away is avoid raw dynamic allocation. Use containers and smart pointers to handle the memory for you. If you can't, wrap the allocation with RAII and observe the Rule of Three or Five.
– user4581301
Mar 25 at 4:45
The deletion of the "secondb
" isn't what causes the "firstb
" to be leaked. The second assignment ofb = new int
is what does that, whether followed bydelete b
or not. Thedelete b
simply releases the result of the secondnew
expression.
– Peter
Mar 25 at 4:50
@user4581301 Right. I really need to get more comfortable with using smart pointers. I think one of my professors talked about it as well.
– John Park
Mar 25 at 5:21
|
show 1 more comment
Not while adhering to Standard C++. Once you lose the last reference to a dynamic allocation, you're hosed.
– user4581301
Mar 25 at 3:59
@user4581301 I see. Thanks.
– John Park
Mar 25 at 4:39
No worries. The take-away is avoid raw dynamic allocation. Use containers and smart pointers to handle the memory for you. If you can't, wrap the allocation with RAII and observe the Rule of Three or Five.
– user4581301
Mar 25 at 4:45
The deletion of the "secondb
" isn't what causes the "firstb
" to be leaked. The second assignment ofb = new int
is what does that, whether followed bydelete b
or not. Thedelete b
simply releases the result of the secondnew
expression.
– Peter
Mar 25 at 4:50
@user4581301 Right. I really need to get more comfortable with using smart pointers. I think one of my professors talked about it as well.
– John Park
Mar 25 at 5:21
Not while adhering to Standard C++. Once you lose the last reference to a dynamic allocation, you're hosed.
– user4581301
Mar 25 at 3:59
Not while adhering to Standard C++. Once you lose the last reference to a dynamic allocation, you're hosed.
– user4581301
Mar 25 at 3:59
@user4581301 I see. Thanks.
– John Park
Mar 25 at 4:39
@user4581301 I see. Thanks.
– John Park
Mar 25 at 4:39
No worries. The take-away is avoid raw dynamic allocation. Use containers and smart pointers to handle the memory for you. If you can't, wrap the allocation with RAII and observe the Rule of Three or Five.
– user4581301
Mar 25 at 4:45
No worries. The take-away is avoid raw dynamic allocation. Use containers and smart pointers to handle the memory for you. If you can't, wrap the allocation with RAII and observe the Rule of Three or Five.
– user4581301
Mar 25 at 4:45
The deletion of the "second
b
" isn't what causes the "first b
" to be leaked. The second assignment of b = new int
is what does that, whether followed by delete b
or not. The delete b
simply releases the result of the second new
expression.– Peter
Mar 25 at 4:50
The deletion of the "second
b
" isn't what causes the "first b
" to be leaked. The second assignment of b = new int
is what does that, whether followed by delete b
or not. The delete b
simply releases the result of the second new
expression.– Peter
Mar 25 at 4:50
@user4581301 Right. I really need to get more comfortable with using smart pointers. I think one of my professors talked about it as well.
– John Park
Mar 25 at 5:21
@user4581301 Right. I really need to get more comfortable with using smart pointers. I think one of my professors talked about it as well.
– John Park
Mar 25 at 5:21
|
show 1 more comment
2 Answers
2
active
oldest
votes
Option 1.
The easiest way is to avoid new
and delete
altogether.
Instead of:
b = new int;
b = new int;
delete b;
Write:
std::unique_ptr<int> b;
...
b = std::make_unique<int>();
b = std::make_unique<int>();
When b
is overwritten, or gets out of scope, it frees the memory. No delete
is required or even allowed. Note quite like the Java garbage collector, but good enough most of the time.
Option 2.
Another way is to use an add-on garbage collector, but that solution is not advisable. First, the garbage collector is conservative meaning that there is a probability to have a memory leak despite the garbage collector. Second, these solutions tend to make existing problems worse, since the memory leaks will not be noticed and treated when introduced. Treating memory leaks a year after introduction is orders of magnitude more difficult than to treat them an hour after introduction.
NOTE: Using unique_ptr
is not as safe as in garbage-collected languages such as C#,Java, and Python. It fails when there is no clear ownership model and when there is a circular ownership. If element a
has a unique_ptr
to b
and b
has a unique_ptr
to a
then they will never be freed. They will never be freed since a unique_ptr
frees the object in the destructor, but nobody will call the destructor of neither a
nor b
. If nobody calls the destructor, then the unique_ptr
will never delete the object, and never call the destructor of the other object.
NOTE 2: Sometimes std::shared_ptr
should be used instead of unique_ptr
, but that does not solve the issue of circular references. This only solves the issue of multiple-owners of a single object.
NOTE 3: These smart pointers do not lower the possibility of stack-overflow due to deep recursion in destructors. This is likely to happen in recursive destruction of long linked-lists or in unbalanced binary-trees. Using unique_ptr
for this cases simply hides the fact that recursion is taking place.
Ok I see. So in C++, there seems to be no absolutely definitive alternative to adhering to the conventional practice of being mindful of how we dynamically allocate memory and how we can go about in safeguarding a way to clear the memory once we are done using the data at the end of our program. Thank you for your detailed explanation.
– John Park
Mar 25 at 5:17
@JohnPark yes, but there is a lot of automation. All you have to be mindful about, most of the time, is who owns what. You rarely have to think about who allocates and who frees stuff. All this as long as you avoid new/malloc
– Michael Veksler
Mar 25 at 5:32
add a comment |
I know that there is a way to recover this memory leak in C# and Java
Java and C# are both garbage collecting languages so they don't tend to have memory leaks often (its almost impossible), but when they do you can throw an exception,
Is there a way to recover this memory leak in C++?
in c++ memory leaks are more common because you are manually allocating memory every time you create a pointer, so you would always throw an exception to point out if a memory leak where to occur, where it is and, how to deal with it...so the answer to your question is...no you cant recover from a memory leak in c++, its a "Down To The Metal" language which is why its so vulnerable if not written correctly, but you can always throw an exception for when a memory leak does occur you can pin point it and see how to fix it.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331061%2fa-way-to-recover-memory-leak-from-dynamic-allocation-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Option 1.
The easiest way is to avoid new
and delete
altogether.
Instead of:
b = new int;
b = new int;
delete b;
Write:
std::unique_ptr<int> b;
...
b = std::make_unique<int>();
b = std::make_unique<int>();
When b
is overwritten, or gets out of scope, it frees the memory. No delete
is required or even allowed. Note quite like the Java garbage collector, but good enough most of the time.
Option 2.
Another way is to use an add-on garbage collector, but that solution is not advisable. First, the garbage collector is conservative meaning that there is a probability to have a memory leak despite the garbage collector. Second, these solutions tend to make existing problems worse, since the memory leaks will not be noticed and treated when introduced. Treating memory leaks a year after introduction is orders of magnitude more difficult than to treat them an hour after introduction.
NOTE: Using unique_ptr
is not as safe as in garbage-collected languages such as C#,Java, and Python. It fails when there is no clear ownership model and when there is a circular ownership. If element a
has a unique_ptr
to b
and b
has a unique_ptr
to a
then they will never be freed. They will never be freed since a unique_ptr
frees the object in the destructor, but nobody will call the destructor of neither a
nor b
. If nobody calls the destructor, then the unique_ptr
will never delete the object, and never call the destructor of the other object.
NOTE 2: Sometimes std::shared_ptr
should be used instead of unique_ptr
, but that does not solve the issue of circular references. This only solves the issue of multiple-owners of a single object.
NOTE 3: These smart pointers do not lower the possibility of stack-overflow due to deep recursion in destructors. This is likely to happen in recursive destruction of long linked-lists or in unbalanced binary-trees. Using unique_ptr
for this cases simply hides the fact that recursion is taking place.
Ok I see. So in C++, there seems to be no absolutely definitive alternative to adhering to the conventional practice of being mindful of how we dynamically allocate memory and how we can go about in safeguarding a way to clear the memory once we are done using the data at the end of our program. Thank you for your detailed explanation.
– John Park
Mar 25 at 5:17
@JohnPark yes, but there is a lot of automation. All you have to be mindful about, most of the time, is who owns what. You rarely have to think about who allocates and who frees stuff. All this as long as you avoid new/malloc
– Michael Veksler
Mar 25 at 5:32
add a comment |
Option 1.
The easiest way is to avoid new
and delete
altogether.
Instead of:
b = new int;
b = new int;
delete b;
Write:
std::unique_ptr<int> b;
...
b = std::make_unique<int>();
b = std::make_unique<int>();
When b
is overwritten, or gets out of scope, it frees the memory. No delete
is required or even allowed. Note quite like the Java garbage collector, but good enough most of the time.
Option 2.
Another way is to use an add-on garbage collector, but that solution is not advisable. First, the garbage collector is conservative meaning that there is a probability to have a memory leak despite the garbage collector. Second, these solutions tend to make existing problems worse, since the memory leaks will not be noticed and treated when introduced. Treating memory leaks a year after introduction is orders of magnitude more difficult than to treat them an hour after introduction.
NOTE: Using unique_ptr
is not as safe as in garbage-collected languages such as C#,Java, and Python. It fails when there is no clear ownership model and when there is a circular ownership. If element a
has a unique_ptr
to b
and b
has a unique_ptr
to a
then they will never be freed. They will never be freed since a unique_ptr
frees the object in the destructor, but nobody will call the destructor of neither a
nor b
. If nobody calls the destructor, then the unique_ptr
will never delete the object, and never call the destructor of the other object.
NOTE 2: Sometimes std::shared_ptr
should be used instead of unique_ptr
, but that does not solve the issue of circular references. This only solves the issue of multiple-owners of a single object.
NOTE 3: These smart pointers do not lower the possibility of stack-overflow due to deep recursion in destructors. This is likely to happen in recursive destruction of long linked-lists or in unbalanced binary-trees. Using unique_ptr
for this cases simply hides the fact that recursion is taking place.
Ok I see. So in C++, there seems to be no absolutely definitive alternative to adhering to the conventional practice of being mindful of how we dynamically allocate memory and how we can go about in safeguarding a way to clear the memory once we are done using the data at the end of our program. Thank you for your detailed explanation.
– John Park
Mar 25 at 5:17
@JohnPark yes, but there is a lot of automation. All you have to be mindful about, most of the time, is who owns what. You rarely have to think about who allocates and who frees stuff. All this as long as you avoid new/malloc
– Michael Veksler
Mar 25 at 5:32
add a comment |
Option 1.
The easiest way is to avoid new
and delete
altogether.
Instead of:
b = new int;
b = new int;
delete b;
Write:
std::unique_ptr<int> b;
...
b = std::make_unique<int>();
b = std::make_unique<int>();
When b
is overwritten, or gets out of scope, it frees the memory. No delete
is required or even allowed. Note quite like the Java garbage collector, but good enough most of the time.
Option 2.
Another way is to use an add-on garbage collector, but that solution is not advisable. First, the garbage collector is conservative meaning that there is a probability to have a memory leak despite the garbage collector. Second, these solutions tend to make existing problems worse, since the memory leaks will not be noticed and treated when introduced. Treating memory leaks a year after introduction is orders of magnitude more difficult than to treat them an hour after introduction.
NOTE: Using unique_ptr
is not as safe as in garbage-collected languages such as C#,Java, and Python. It fails when there is no clear ownership model and when there is a circular ownership. If element a
has a unique_ptr
to b
and b
has a unique_ptr
to a
then they will never be freed. They will never be freed since a unique_ptr
frees the object in the destructor, but nobody will call the destructor of neither a
nor b
. If nobody calls the destructor, then the unique_ptr
will never delete the object, and never call the destructor of the other object.
NOTE 2: Sometimes std::shared_ptr
should be used instead of unique_ptr
, but that does not solve the issue of circular references. This only solves the issue of multiple-owners of a single object.
NOTE 3: These smart pointers do not lower the possibility of stack-overflow due to deep recursion in destructors. This is likely to happen in recursive destruction of long linked-lists or in unbalanced binary-trees. Using unique_ptr
for this cases simply hides the fact that recursion is taking place.
Option 1.
The easiest way is to avoid new
and delete
altogether.
Instead of:
b = new int;
b = new int;
delete b;
Write:
std::unique_ptr<int> b;
...
b = std::make_unique<int>();
b = std::make_unique<int>();
When b
is overwritten, or gets out of scope, it frees the memory. No delete
is required or even allowed. Note quite like the Java garbage collector, but good enough most of the time.
Option 2.
Another way is to use an add-on garbage collector, but that solution is not advisable. First, the garbage collector is conservative meaning that there is a probability to have a memory leak despite the garbage collector. Second, these solutions tend to make existing problems worse, since the memory leaks will not be noticed and treated when introduced. Treating memory leaks a year after introduction is orders of magnitude more difficult than to treat them an hour after introduction.
NOTE: Using unique_ptr
is not as safe as in garbage-collected languages such as C#,Java, and Python. It fails when there is no clear ownership model and when there is a circular ownership. If element a
has a unique_ptr
to b
and b
has a unique_ptr
to a
then they will never be freed. They will never be freed since a unique_ptr
frees the object in the destructor, but nobody will call the destructor of neither a
nor b
. If nobody calls the destructor, then the unique_ptr
will never delete the object, and never call the destructor of the other object.
NOTE 2: Sometimes std::shared_ptr
should be used instead of unique_ptr
, but that does not solve the issue of circular references. This only solves the issue of multiple-owners of a single object.
NOTE 3: These smart pointers do not lower the possibility of stack-overflow due to deep recursion in destructors. This is likely to happen in recursive destruction of long linked-lists or in unbalanced binary-trees. Using unique_ptr
for this cases simply hides the fact that recursion is taking place.
edited Mar 25 at 5:04
answered Mar 25 at 4:44
Michael VekslerMichael Veksler
5,9241824
5,9241824
Ok I see. So in C++, there seems to be no absolutely definitive alternative to adhering to the conventional practice of being mindful of how we dynamically allocate memory and how we can go about in safeguarding a way to clear the memory once we are done using the data at the end of our program. Thank you for your detailed explanation.
– John Park
Mar 25 at 5:17
@JohnPark yes, but there is a lot of automation. All you have to be mindful about, most of the time, is who owns what. You rarely have to think about who allocates and who frees stuff. All this as long as you avoid new/malloc
– Michael Veksler
Mar 25 at 5:32
add a comment |
Ok I see. So in C++, there seems to be no absolutely definitive alternative to adhering to the conventional practice of being mindful of how we dynamically allocate memory and how we can go about in safeguarding a way to clear the memory once we are done using the data at the end of our program. Thank you for your detailed explanation.
– John Park
Mar 25 at 5:17
@JohnPark yes, but there is a lot of automation. All you have to be mindful about, most of the time, is who owns what. You rarely have to think about who allocates and who frees stuff. All this as long as you avoid new/malloc
– Michael Veksler
Mar 25 at 5:32
Ok I see. So in C++, there seems to be no absolutely definitive alternative to adhering to the conventional practice of being mindful of how we dynamically allocate memory and how we can go about in safeguarding a way to clear the memory once we are done using the data at the end of our program. Thank you for your detailed explanation.
– John Park
Mar 25 at 5:17
Ok I see. So in C++, there seems to be no absolutely definitive alternative to adhering to the conventional practice of being mindful of how we dynamically allocate memory and how we can go about in safeguarding a way to clear the memory once we are done using the data at the end of our program. Thank you for your detailed explanation.
– John Park
Mar 25 at 5:17
@JohnPark yes, but there is a lot of automation. All you have to be mindful about, most of the time, is who owns what. You rarely have to think about who allocates and who frees stuff. All this as long as you avoid new/malloc
– Michael Veksler
Mar 25 at 5:32
@JohnPark yes, but there is a lot of automation. All you have to be mindful about, most of the time, is who owns what. You rarely have to think about who allocates and who frees stuff. All this as long as you avoid new/malloc
– Michael Veksler
Mar 25 at 5:32
add a comment |
I know that there is a way to recover this memory leak in C# and Java
Java and C# are both garbage collecting languages so they don't tend to have memory leaks often (its almost impossible), but when they do you can throw an exception,
Is there a way to recover this memory leak in C++?
in c++ memory leaks are more common because you are manually allocating memory every time you create a pointer, so you would always throw an exception to point out if a memory leak where to occur, where it is and, how to deal with it...so the answer to your question is...no you cant recover from a memory leak in c++, its a "Down To The Metal" language which is why its so vulnerable if not written correctly, but you can always throw an exception for when a memory leak does occur you can pin point it and see how to fix it.
add a comment |
I know that there is a way to recover this memory leak in C# and Java
Java and C# are both garbage collecting languages so they don't tend to have memory leaks often (its almost impossible), but when they do you can throw an exception,
Is there a way to recover this memory leak in C++?
in c++ memory leaks are more common because you are manually allocating memory every time you create a pointer, so you would always throw an exception to point out if a memory leak where to occur, where it is and, how to deal with it...so the answer to your question is...no you cant recover from a memory leak in c++, its a "Down To The Metal" language which is why its so vulnerable if not written correctly, but you can always throw an exception for when a memory leak does occur you can pin point it and see how to fix it.
add a comment |
I know that there is a way to recover this memory leak in C# and Java
Java and C# are both garbage collecting languages so they don't tend to have memory leaks often (its almost impossible), but when they do you can throw an exception,
Is there a way to recover this memory leak in C++?
in c++ memory leaks are more common because you are manually allocating memory every time you create a pointer, so you would always throw an exception to point out if a memory leak where to occur, where it is and, how to deal with it...so the answer to your question is...no you cant recover from a memory leak in c++, its a "Down To The Metal" language which is why its so vulnerable if not written correctly, but you can always throw an exception for when a memory leak does occur you can pin point it and see how to fix it.
I know that there is a way to recover this memory leak in C# and Java
Java and C# are both garbage collecting languages so they don't tend to have memory leaks often (its almost impossible), but when they do you can throw an exception,
Is there a way to recover this memory leak in C++?
in c++ memory leaks are more common because you are manually allocating memory every time you create a pointer, so you would always throw an exception to point out if a memory leak where to occur, where it is and, how to deal with it...so the answer to your question is...no you cant recover from a memory leak in c++, its a "Down To The Metal" language which is why its so vulnerable if not written correctly, but you can always throw an exception for when a memory leak does occur you can pin point it and see how to fix it.
answered Mar 25 at 4:09
Prinzeono KeyPrinzeono Key
13
13
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331061%2fa-way-to-recover-memory-leak-from-dynamic-allocation-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Not while adhering to Standard C++. Once you lose the last reference to a dynamic allocation, you're hosed.
– user4581301
Mar 25 at 3:59
@user4581301 I see. Thanks.
– John Park
Mar 25 at 4:39
No worries. The take-away is avoid raw dynamic allocation. Use containers and smart pointers to handle the memory for you. If you can't, wrap the allocation with RAII and observe the Rule of Three or Five.
– user4581301
Mar 25 at 4:45
The deletion of the "second
b
" isn't what causes the "firstb
" to be leaked. The second assignment ofb = new int
is what does that, whether followed bydelete b
or not. Thedelete b
simply releases the result of the secondnew
expression.– Peter
Mar 25 at 4:50
@user4581301 Right. I really need to get more comfortable with using smart pointers. I think one of my professors talked about it as well.
– John Park
Mar 25 at 5:21