#Memory leak issue

116 messages · Page 1 of 1 (latest)

dense meteorBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

forest kestrel
#

do not put a destructor in the node, that's a recipe for agony
write your destructor in the list iteslf, you have done that already

#

yes

#

show me

#

show me the full log

#

send me your cpp file

#

everything

swift ospreyBOT
#
Compiler Output
<source>: In function 'int main()':
<source>:24:17: error: friend declaration 'std::ostream& operator<<(std::ostream&, const main()::Location&)' in local class without prior local declaration
   24 | friend ostream &operator<<(ostream &os, const Location &loc);
      |                 ^~~~~~~~
<source>:26:17: error: friend declaration 'std::istream& operator>>(std::istream&, main()::Location&)' in local class without prior local declaration
   26 | friend istream &operator>>(istream &is, Location &loc);
      |                 ^~~~~~~~
<source>:40:19: error: cannot call constructor 'main()::Location::Location' directly [-fpermissive]
   40 | Location::Location()
      | ~~~~~~~~~~~~~~~~~~^~
<source>:40:19: note: for a function-style cast, remove the redundant '::Location'
<source>:40:21: error: expected ';' before '{' token
   40 | Location::Location()
      |                     ^
      |                     ;
   41 | {
      | ~                    
<source>:50:30: error: qualified
forest kestrel
#

the memory leak is in the void LocationStack::push(const Location &loc) function

#

you are redirecting the top pointer to a new node, thus completely abandoning the old node which the pointer top was pointing to

#

so now no one can access that old node from a pointer

#

so no one cleans that up

#

so you get a leak

#

write an if statement for the case when you have an empty list,
and another if for a non-empty list

#

if I do

int main()
{
  int* p = new int(42);
  p = new int(56);
  delete p;
}

how exactly are you gonna clean up the heap allocated value 42 ?

#

this is exactly what you wrote

#

if a stack is empty, what is the value of the pointer top?

#

yes, and when you call push to an empty stack, a new node is added, for this new node, what is its next pointer?

#

that is not enough

#

yes

#

this is the deciding way to know "I am the last node at the bottom of the stack"

#

and what if the stack already has top pointing to the node at the start of the stack?

#

for eaxample, just one node

#

and you call push

#

what is supposed to happen?

#

correct

#

now fix your code

#

yes

#

do you keep a counter somewhere?

#

I guess not

#

not required, but yes

#

operator new returns a pointer to a heap allocated object

#

calling the constructor just returns an object, not a pointer

#

the variable top is a pointer, you need to pass a pointer on the right to make it compile, operator new returns a pointer

#

I am testing it now

dense meteorBOT
#

@fierce saddle

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

#

Thank you and let us know if you have any more questions!

forest kestrel
#

it's not solved

#

whoops, I was running old code

#

same

#

don't think about how valgrind works - think about your nodes

deep crown
#

the problem with putting a recursive destructor in nodes is you can quickly trash the stack with large lists

deep crown
#

always best to go with iterative approaches

forest kestrel
#

your ostream &operator<<(ostream &os, const LocationStack &s) function is corrupting the lists

#

thus destroying the links

#

thus losing the connection to nodes from the bottom of the stack or whatever

#

and all nodes except the node at the top get lost in the abyss

#

100 nodes leaked out of 101 nodes created

#

curr->setNextNode(prev);
this shit

#

is bad

#

you should do

#

gurr = curr->getNextNode

#

or something

#

"hey I have a list , let's chop it in half, oh noooo, why did it break?"

#

hmmmmmmmmmmmmmm

#

you are editing the next node

#

so you are redirecting the pointer of some node from its correct neithbour, to somewhere else

#

thus you are losing nodes

#

can you explain to me what the operator<< si supposed to do here?

#

print what exactly?

#

in what order

#

so in reverse order

#

but your stack is singly linked

#

you can't go in reverse

#

so you need a copy stack

#

to copy all the elements in reverse

#

then print them

#

?

#

why do you believe this lie

#

what are you not allowed to do

#

is this stack supposed to be implemented as a doubly linked list?

#

your teachers are evil pieces of shit

#

not gonna lie

#

the job of the operator is to print, not to reverse the elements

#

so, to solve this shit

#

you need to make a helper global function

#

void reverseStack

#

that does the reversing in-place

#

don't tell me this is required by your teachers again

deep crown
#

you should be writing helper functions for this

#

the more places you deal with this stuff by hand

#

the more chances there are for you to mess up

forest kestrel
#

oh wait, theres EVEN MORE

#

you know how the operator<< takes a const Stack ?

#

you know how fields of constants cannot be edited?

#

you know how they want you to reverse a const Stack here?

#

right, so

#

fuck the authors

#

fuck the requirements

#

make a stack copy, with the elements reversed easy peasy

#

print the created copy, from the first element to the last

#

and submit the assignment

#

they can grade this however they like

#

let me write that for you

#

nah

#

use the Stack directly

#

oh and remove my macro for printing line numbers

#

oh and also

#

to run the memory checking tool

#

go here

#

enter code

#
clang++ -std=c++17 -Wall -Wextra -Wpedantic -fsanitize=address -g3 -O0 main.cpp -o main && ./main
#

and type this command on the right

#

it builds the program and runs it

#

if a leak is found, you get crashes immideately

#

!asan

dense meteorBOT
# forest kestrel !asan
How To Use Sanitizers

Sanitizers are tools which generate additional code in your program that can catch many common programming mistakes,
such as:

General Advice

Not all sanitizers can be combined, but when they can, use e.g.:
-fsanitize=address,undefined to combine them.
Always compile with debug info to get line numbers, variable names, etc.

MSVC 19.27+ and VS 2019 16.9+
Sample Program
int main(void) {
    int x;
    return x;
}
`-fsanitize=memory -g` Output

SUMMARY: MemorySanitizer: use-of-uninitialized-value /tmp/test.cpp:3:5 in main
Exiting
(3:5 is line and column of return)

forest kestrel
#

!solved

dense meteorBOT
#

Message is already solved

forest kestrel
#

nevermind