Can anyone please help me in finding out whether the below code causes memory leak or not. If it doesnt cause leak wat modification i can do to make it leak.

Thank You

Here is the code in FORM1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace custom
{
    public partial class Form1 : Form
    {
       
        
        public Form1()
        {
            InitializeComponent();
            
        }

        public static void call()
        {
            Form2 f3 = new Form2();
            f3.Show();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            call();
           
        }
    }
}

This is the code in FORM2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace custom
{

    public class mycalss
    {

            
        public void disp()
       {
           Form2 f2w = new Form2();
           f2w.even+=new Form2.del(f2w.f2_even);
           f2w.raiseeven();
             
        }
}
        
  public partial class Form2 : Form
    {

       
        
       public Form2()
        {
            InitializeComponent();
            
            
        }
        

        public delegate void del();
        public event del even;
        public void raiseeven()
        {
            even();
          
            //objc.Add(f2);
        }
        //public ArrayList objc = new ArrayList();
        public static void call1()
        {

            Form1 f1 = new Form1();
            f1.Show();
   
        }
        
        public void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            mycalss my = new mycalss();
            my.disp();
           
            f2.even+=new del(f2_even);
            f2.raiseeven();
            
            
            //objc.Add(f2);
            //call1();
        }
        public void f2_even()
        {
            call1();
           
        }
    }
}

The above code is nothing but simple button click on which it displays form1 or form2

Recommended Answers

All 5 Replies

First of all, use code tags if you post code.
Second, why would you want your program to leak? It will not.

Thx for your fast reply .... i need to have memory leak because i am currently working on a academic project which deals with memory leaks. Since i am new to c# i am finding it bit difficult to make memory leak in programs . If you provide a simple example that will be a great help. Thank you

Any simple code about memory leak will be more helpful . I am struggling a lot.

for (int i = 0; i < 10000000; i++) {
    Font f = new Font(label1.Font.Name, 8, label1.Font.Style, label1.Font.Unit);
}

Fonts need to be disposed of when you aren't using them as they consume unmanaged resources. This should provide a small but growing memory leak. Code assumes you have a label named label1.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.