Solved

Creating memory leak in a C# application. How to do?

Posted on 2011-09-26
18
524 Views
Last Modified: 2012-05-12
Hi there;

I need to create memory leak in a C++ application for test purposes.

How can I achieve that? Moreover, could you provide me a sample code for this?

Kind regards.
0
Comment
Question by:jazzIIIlove
  • 5
  • 5
  • 4
  • +2
18 Comments
 
LVL 17

Assisted Solution

by:sonawanekiran
sonawanekiran earned 100 total points
ID: 36597573
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36597594
>>Creating memory leak in a C# application.
>>I need to create memory leak in a C++ application

So which do you want?  
In C# it is possible but not so obvious how.  In C++ it is trivial (assign memory with new but don't delete it afterwards - eg.  char* p = new char;)
0
 
LVL 10

Expert Comment

by:oxyoo
ID: 36597596
I am not too experience in this but as far as I know you can't get memory leaks in .Net as in C++ apps, but what you can get in .Net are objects that are not disposed of because they are still being referenced somewhere. So called long lived objects. An (somewhat silly) example of this could be as in the example below where the objects are referenced(leaked) into the list and will not be disposed of.

HTH


public static class FooFactory {
    private static List<Foo> foos = new List<Foo>();

    public static Something CreateSomething() {
        var foo = new Foo();
        foos.Add(foo);
        return foo;
    }
}

Open in new window

0
 
LVL 10

Expert Comment

by:oxyoo
ID: 36597600
Oh sorry, read that backwards. Thought you asked for an example in C#, sorry about that.

*runs off and hides*
0
 
LVL 10

Expert Comment

by:oxyoo
ID: 36597625
In case it is a C# example you need.. here it is reposted, with some errors removed. Typed too fast.
public static class FooFactory {
    private static List<Foo> foos = new List<Foo>();

    public static Foo CreateMyFoo() {
        var foo = new Foo();
        foos.Add(foo);
        return foo;
    }
}

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36597638
@oxyoo
I don't see how that creates a memory leak.  As far as I understand it:- it creates objects that will be later released by the GC mechanism.  No memory leaks anywhere.
0
 
LVL 10

Expert Comment

by:oxyoo
ID: 36597652
@AndyAinscow:
Yup, agree with you and that was what I said in the post. It does not create any leaks as such, but it creates long lived objects.

Even though you create a new object using CreateMyFoo, if you only use it for a split second it will not be disposed of since it is still being referenced by the static list.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36597677
That is what I thought.
It depends on the asker of the question - do they really mean a memory leak or do they mean code that just fills available memory up.

And the question of C++ or C# is still open.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36598374
Ah, sorry, I meant C/C++ but I mistype as I am currently also dealing with another C# application.

But let's go for both then :D

The issue is that I didn't mean to fill up the whole memory but a leak that cause a runtime error which i need.

Regards.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 total points
ID: 36598408
>>but a leak that cause a runtime error

As far as I know a memory leak will never create a runtime error.

For a leak in C++ you can do the following:
char* p = new char;
//delete p;  /*this will free the assigned memory, comment it out and the memory will leak == never released */
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36598422
Ok, then, how can I crash my program?

I mean does crash mean a runtime error? or Can the outcome of a memory leak be considered as a crash?

Kind regards.
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 total points
ID: 36598764
     int *p = 0;
      (*p) = 42;  //or any other number should do the trick
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36599202
42...
0
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 200 total points
ID: 36600062
you could crash your prog by creating more leaks than memory available:

while (true)
{
   p = new unsigned int[1024*1024];
   p[rand()] = rand();
}

Open in new window


the above code either will throw a memory exception because of too many memory requested (all leaks of 4mb) and crash or will crash because of array access when p finally gets a NULL assigned.

Sara
0
 
LVL 33

Expert Comment

by:sarabande
ID: 36600087
of course Andy's code snippet also works. i only wanted to have both the leaks and a crash :)

Sara
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36600570
Thanks. One final question os that is memory leak only way to crash a program? What other ways are there to reproduce a crash for test purpose for C and C# application?

Kind regards.
0
 
LVL 33

Accepted Solution

by:
sarabande earned 200 total points
ID: 36708927
no, as Andy told memory leaks don't lead to crash. they only reserve free memory where there is no access to from a variabel or pointer. the crash with leaks i produced happens in new operator when no free memory could be found because of too many storage was not freed. some compilers would not crash on new when memory was out but return a NULL pointer. to handle that i added a statement which tries to use a null pointer as array. that would crash in any case. so the crash simulates a situation where a program runs for days or weeks and because of leaks it finally cannot allocate memory.

so crashes can be produced by
- assigning values to a null pointer
- accessing null pointers as arrays
- dividing by zero
- making a memcpy or strcpy where the target pointer is NULL or invalid
- and more ...

higher level functions also may throw exceptions for other reasons. that leads to crash if you don't catch the exception.

Sara

0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36708991
Good!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ConsoleSql 1 34
Sum Column in GridView 3 44
Why Does This Page Not Show Up? 4 24
rest webservice call over https via c# 6 25
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now