Presentation is loading. Please wait.

Presentation is loading. Please wait.

Defect Analysis: Memory Leaks

Similar presentations


Presentation on theme: "Defect Analysis: Memory Leaks"— Presentation transcript:

1 Defect Analysis: Memory Leaks
CPRE 556: Lecture 5 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

2 What will we cover today?
Last time we discussed an example of code and made several observations regarding memory leaks. We will first review those observations and then study a few other examples. 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

3 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved
Scenario Memory is allocated in the function f() by calling a dynamic memory allocation function (e.g. getbuf() in our sample code). We want to check if the memory is subsequently deallocated. We will use the term handle to refer to a pointer that stores the address of the dynamically allocated memory. Memory must be deallocated before destroying all the handles to the memory. 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

4 Who may deallocate memory?
There are three possibilities – the memory may be deallocated by: The same function (f()) that allocated the memory. The caller of the function f(). One or more functions called by f(). 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

5 Should we analyze the caller?
We need to analyze the caller of f() only if the caller can see a handle to the allocated memory. Typical cases would be: There is a handle which is a global pointer. A handle is assigned to a function parameter and communicated to the caller. 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

6 Should we analyze the callee?
We need to analyze a function g() called (directly or indirectly through a chain of calls) by f() only if g() can see a handle to the allocated memory. Typical cases would be: A handle is assigned to a function parameter and communicated to g(). 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

7 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved
Escape Analysis We will say an handle escapes a function f() if the handle is communicated to other functions through a global variable or a parameter. Based on the previous observations, we need to check all those functions to where a handle escapes. We will call this an escape analysis. The purpose is to determine the functions that need to be looked at. 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

8 Execution Path Analysis
The escape analysis leads us to functions that need to be analyzed. We need to analyze all the relevant execution paths within the identified functions. We have to relate the paths with respect to the conditions governing the allocation. What is a governing condition in the code sample for the homework? 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

9 Challenge: Asynchronous Processing
Memory allocation is done in f() and deallocation is done g(). The function g() is not a caller of f() and it is also not called by f() (directly or indirectly). Typical cases are: g() is an interrupt-driven function. g() is executed by a concurrent thread. 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

10 How is the handle communicated in asynchronous processing?
Suppose the allocation is done in f() and deallocation is done g() that works asynchronously with f(). How is a handle communicated to g()?. Typically it is done by : Inserting the handle in a shared linked list or some other data structure that is accessible to both f() and g(). Does it happen in the homework code? Where? How do we find the g()? 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

11 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved
Homework Reminder The first homework is posted on the web. Type the answer and send the answer to me by next Thursday (1/26), 10 pm. Mail a PDF or MS Word document to: 9/17/2018 Lecture Notes – Copyright © 2006 S. C. Kothari, All rights reserved

12 C++ Example Mark Roulo is a technical coordinator for Java World magazine who has programmed professionally since 1989 and has screened candidates for C++ projects. As part of his standard interview for C++ candidates, he asks them to write a small class with the intention of evaluating their command of the language. We are going to examine the code most candidates write as a reference point for our discussion.

13 C++ Example Mark Roulo is a technical coordinator for Java World magazine who has programmed professionally since 1989 and has screened candidates for C++ projects. As part of his standard interview for C++ candidates, he asks them to write a small class with the intention of evaluating their command of the language. We are going to examine the code most candidates write as a reference point for our discussion.

14 C++ Example “Most of the candidates I interview have already made it to the top of the resume pool -- usually by claiming at least 3 years professional experience with C++ plus large systems experience, degree from a good school, personal recommendation, etc.” “The candidates then must survive a phone screen interview whose job is to weed out candidates that can't, for example, describe any of their projects coherently.”

15 C++ Example Mark Roulo is a technical coordinator for Java World magazine who has programmed professionally since 1989 and has screened candidates for C++ projects. As part of his standard interview for C++ candidates, he asks them to write a small class with the intention of evaluating their command of the language. We are going to examine the code most candidates write as a reference point for our discussion.

16 C++ Example “Most of the candidates I interview have already made it to the top of the resume pool -- usually by claiming at least 3 years professional experience with C++ plus large systems experience, degree from a good school, personal recommendation, etc.” “The candidates then must survive a phone screen interview whose job is to weed out candidates that can't, for example, describe any of their projects coherently.”

17 C++ Example The assignment:
Write a Named Point class with three members: two floating point values for the coordinates on an X-Y plane, and a name represented as a 'char *'. Assume that this class will be used for some sort of wargame or simulation program that treats the world as flat and that these named points will be used to represent things like cities, battlefields, etc. The handout has the submissions from the interview candidates.

18 C++ Example Mark Roulo is a technical coordinator for Java World magazine who has programmed professionally since 1989 and has screened candidates for C++ projects. As part of his standard interview for C++ candidates, he asks them to write a small class with the intention of evaluating their command of the language. We are going to examine the code most candidates write as a reference point for our discussion.

19 C++ Example “Most of the candidates I interview have already made it to the top of the resume pool -- usually by claiming at least 3 years professional experience with C++ plus large systems experience, degree from a good school, personal recommendation, etc.” “The candidates then must survive a phone screen interview whose job is to weed out candidates that can't, for example, describe any of their projects coherently.”

20 C++ Example The assignment:
Write a Named Point class with three members: two floating point values for the coordinates on an X-Y plane, and a name represented as a 'char *'. Assume that this class will be used for some sort of wargame or simulation program that treats the world as flat and that these named points will be used to represent things like cities, battlefields, etc. The handout has the submissions from the interview candidates.

21 C++ Example This example shows us that:
Even interview candidates with 3 years of experience are proficient at writing buggy code in C++. These defects occur across functions. For a tool to catch these errors it needs to take into account how functions interact. We might think that a bug is fixed, but in C++ we are probably trading it for an even more subtle bug. We need to be able to recheck the code often.


Download ppt "Defect Analysis: Memory Leaks"

Similar presentations


Ads by Google