==11==ERROR: LeakSanitizer: detected memory leaks Direct leak of 64 byte(s) in 1…
- User
==11==ERROR: LeakSanitizer: detected memory leaks Direct leak of 64 byte(s) in 1 object(s) allocated from: #0 0x7f370e7637f8 in operator new(unsigned long) (/usr/local/lib64/libasan.so.8+0xfa7f8) #1 0x4211ce in create_order(Order*&, int, char const*, Drink**&, int, SugarLevel) /vol/src/pa3.cpp:732 #2 0x430d32 in test19() /vol/src/pa3_test.cpp:579 #3 0x440312 in main /vol/src/pa3_test.cpp:1507 #4 0x7f370db20249 (/lib/x86_64-linux-gnu/libc.so.6+0x27249) (BuildId: 30f067a5eccdb3bdce0b9ec7638ebd86992b9c8b) SUMMARY: AddressSanitizer: 64 byte(s) leaked in 1 allocation(s).
==11==ERROR: LeakSanitizer: detected memory leaks Direct leak of 64 byte(s) in 1 object(s) allocated from: #0 0x7f370e7637f8 in operator new(unsigned long) (/usr/local/lib64/libasan.so.8+0xfa7f8) #1 0x4211ce in create_order(Order*&, int, char const*, Drink**&, int, SugarLevel) /vol/src/pa3.cpp:732 #2 0x430d32 in test19() /vol/src/pa3_test.cpp:579 #3 0x440312 in main /vol/src/pa3_test.cpp:1507 #4 0x7f370db20249 (/lib/x86_64-linux-gnu/libc.so.6+0x27249) (BuildId: 30f067a5eccdb3bdce0b9ec7638ebd86992b9c8b) SUMMARY: AddressSanitizer: 64 byte(s) leaked in 1 allocation(s).
==11==ERROR: LeakSanitizer: detected memory leaks Direct leak of 64 byte(s) in 1 object(s) allocated from: #0 0x7f370e7637f8 in operator new(unsigned long) (/usr/local/lib64/libasan.so.8+0xfa7f8) #1 0x4211ce in create_order(Order*&, int, char const*, Drink**&, int, SugarLevel) /vol/src/pa3.cpp:732 #2 0x430d32 in test19() /vol/src/pa3_test.cpp:579 #3 0x440312 in main /vol/src/pa3_test.cpp:1507 #4 0x7f370db20249 (/lib/x86_64-linux-gnu/libc.so.6+0x27249) (BuildId: 30f067a5eccdb3bdce0b9ec7638ebd86992b9c8b) SUMMARY: AddressSanitizer: 64 byte(s) leaked in 1 allocation(s).
- Assistant
The LeakSanitizer output indicates that you have a memory leak of exactly 64 bytes allocated once in the function
create_orderat line 732 ofpa3.cpp. This allocation is not freed before the program exits, which causes the leak.Key details:
- Leak size: 64 bytes
- Allocation site:
create_order(Order*&, int, char const*, Drink**&, int, SugarLevel) /vol/src/pa3.cpp:732 - The leak is triggered during the test
test19()
How to approach fixing this leak
-
Locate the allocation at line 732:
Check your filepa3.cppat line 732 where thenewoperator is called. This is where the leaked memory is allocated. -
Understand the ownership and lifecycle of that memory:
After the allocation, find where the pointer is stored or passed. You need to ensure that everynewis paired with a correspondingdeletein some part of the code. -
Verify destruction or deallocation:
This leaked memory is likely a heap object (could be yourOrderor something allocated insidecreate_order). Ensure that it is properlydeleted or freed when no longer needed. -
Check the data structures:
SinceDrink**&andOrder*&are passed in, make sure these pointers are correctly managed. If you're allocating a dynamic array or objects on the heap, make sure to properly destruct them.
Example: What you might be missing
Suppose your
create_order()function dynamically allocates anOrderobject or an array ofDrink*. You should have a matching destructor or cleanup function to delete these allocations.For example:
void create_order(Order*& order, int num_drinks, const char* customer_name, Drink**& drinks, int quantity, SugarLevel sugar) { order = new Order(customer_name, num_drinks, sugar); // This is likely line 732 (the leak) drinks = new Drink*[num_drinks]; for (int i = 0; i < num_drinks; ++i) { drinks[i] = new Drink(...); } //... } // Elsewhere you should clean up: void delete_order(Order* order, Drink** drinks, int num_drinks) { for (int i = 0; i < num_drinks; ++i) { delete drinks[i]; } delete[] drinks; delete order; }If you do not call
delete_orderor equivalent cleanup after you create an order, then those allocated bytes will leak.
What you need to do next:
- Audit your test
test19()andmain()to verify the destructor or cleanup calls for the allocatedOrderandDrinkobjects. - If you have destructors for
OrderandDrink, make sure they delete any dynamically allocated resources inside them. - If resources are managed manually (raw pointers), add matching
deletecalls. - If possible, replace raw pointers with smart pointers like
std::unique_ptrorstd::shared_ptrto automate cleanup.
If you can share the relevant part of
create_orderor how the allocatedOrderis managed and destroyed, I can help craft a more specific fix!