Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7517797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:33:54+00:00 2026-05-30T01:33:54+00:00

I created a neat little function to delete an ienumerable and it’s contents (to

  • 0

I created a neat little function to delete an ienumerable and it’s contents (to plug a memory leak I recently discovered):

generic<typename T>
void CollectionHelpers::DeleteEnumerable(IEnumerable<T>^% enumerable)
{
      if(enumerable != nullptr)
      {
            for each( T obj in enumerable)
            {
                  delete obj;
            }

            delete enumerable;
            enumerable = nullptr;
      }
}

…but for some reason, when I trace through with a debugger, the list still appears to point to some memory when I return from the DeleteEnumerable function.

I thought that by passing in as a tracking reference it should modify the handle I’m passing in? What have I missed here?


Edit: A more thorough test example…

This is a slightly updated disposer:

using namespace GenericCollections;
using namespace System::Collections::Generic;
using namespace System;

generic<typename T>
void CollectionHelpers::DeleteEnumerable(IEnumerable<T>^% enumerable)
{
    if(enumerable != nullptr)
    {
        for each( T obj in enumerable )
        {
            Console::WriteLine("Disposing of object");
            delete obj;
        }

        Console::WriteLine("Disposing of enumerable");
        delete enumerable;
        enumerable = nullptr;
    }

    if( enumerable == nullptr )
    {
        Console::WriteLine("enumerable tracking reference is nullptr");
    }
    else
    {
        Console::WriteLine("enumerable tracking reference is NOT nullptr");
    }
}

This is a cut down example of the code which calls the disposer:

using namespace System;
using namespace GenericCollections;
using namespace System::Collections::Generic;

ref class MyClass
{
private:
    int* m_myBuf;

public:
    MyClass()
    {
        m_myBuf = new int[100];
        Console::WriteLine("MyClass::MyClass()");
    }

    ~MyClass()
    {
        delete [] m_myBuf;
        m_myBuf = NULL;
        Console::WriteLine("MyClass::~MyClass()");
    }
};

int main(array<System::String ^> ^args)
{

    List<MyClass^>^ myList = gcnew List<MyClass^>;

    myList->Add(gcnew MyClass());
    myList->Add(gcnew MyClass());
    myList->Add(gcnew MyClass());

    CollectionHelpers::DeleteEnumerable(myList);

    if(myList == nullptr)
    {
        Console::WriteLine("Original list is disposed of");
    }
    else
    {
        Console::WriteLine(String::Format("Original list still referenced: {0}", myList));
    }

    return 0;
}

…and this is the output:

MyClass::MyClass()
MyClass::MyClass()
MyClass::MyClass()
Disposing of object
MyClass::~MyClass()
Disposing of object
MyClass::~MyClass()
Disposing of object
MyClass::~MyClass()
Disposing of enumerable
enumerable tracking reference is nullptr
Original list still referenced: System.Collections.Generic.List`1[MyClass]

To be honest, I’m not so worried whether deleting the enumerable works it’s the objects which are contained inside which needed to be killed off. We use these lists all over the place and our system was running out of memory because they weren’t being cleaned up quickly enough.

Worse still, we were relying on the DeleteEnumerable function to set our list tracking references to nullptr, but when they’re passed back out the tracking reference doesn’t appear to have been updated. It’s not just a debugger issue either as you can see from the console output.

What I don’t understand is why the

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:33:57+00:00Added an answer on May 30, 2026 at 1:33 am

    Okay, this behavior is indeed weird. And this is what actually happens:

    You have a local variable of type List<MyClass^>^ and you are calling a method with a parameter of type IEnumerable<T>^%. This is a problem. The method could set its parameter, say, to array<T>^. In C#, code like this would not be allowed, but for some reason, it is allowed in C++/CLI. And it compiles down to something like:

    List<MyClass^>^ myList = …;
    IEnumerable<MyClass^>^ enumerable = myList;
    CollectionHelpers::DeleteEnumerable(enumerable);
    

    Do you see the problem? If you set the reference to nullptr in DeleteEnumerable(), the local variable enumerable changes, but not myList.

    To verify, change your code to this:

    IEnumerable<MyClass^>^ myEnumerable = myList;
    
    CollectionHelpers::DeleteEnumerable(myEnumerable);
    
    if(myEnumerable == nullptr)
    {
        Console::WriteLine("Original list is disposed of");
    }
    else
    {
        Console::WriteLine(String::Format("Original list still referenced: {0}", myList));
    }
    

    And it correctly prints “Original list is disposed of”.

    If you want to know why does C++/CLI behave in this way, maybe it’s so that tracking references behave similarly to normal C++ references?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In facebook, when you share a link, a neat little preview is created with
Ok, so I had this neat little idea the other night to create a
Django as a framework is a neat little package. There are very few files
I created quite a neat application for backups. It just copies the directories you
I have a function that fills a part of the bitmap created by imagecreatetruecolor($h,$w)
So I've set up my framework in a neat little system to wrap SDL,
Given a multimap<A,B> M what's a neat way to create a vector<B> of all
Created .NET WCF service, tested it - works. Generated schemas from Data and service
I created a program using dev-cpp and wxwidgets which solves a puzzle. The user
I created a few mediawiki custom tags, using the guide found here http://www.mediawiki.org/wiki/Manual:Tag_extensions I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.