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 8587835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:39:55+00:00 2026-06-11T22:39:55+00:00

My MEX file is written in C++/CLI and calls a DLL written in C#.

  • 0

My MEX file is written in C++/CLI and calls a DLL written in C#.

When gcnew’ing an object, shouldn’t it be garbage collected when the mexFunction returns?
Its references should be lost but nothing seems to be garbage collected… each call to the mex function increases MATLAB‘s memory allocation (and no, the memory is not used for MATLAB variables).

I’ve experimented with creating a large dummy value with narrow scope and when stepping through the MEX file I can see the memory allocated and released. But not so with the main object created in the mexFunction =(

I’ve tried to delete it in the destructor and finalizer, but I can’t get it to garbage collect. How can I free the managed memory when returning to MATLAB?

I don’t think external DLL filers are the problem. To illustrate, I created this silly mexFunction:

public ref class Foo
{
    public:
        Foo()
        {
            Dictionary<int,String^>^ bar = gcnew Dictionary<int,String^>;
            for(int i=0;i<10000000;i++)
            {
                bar->Add(i, "abcdefghijklmnopqrstuvxyz");
            }
        }
};

void mexFunction(int nlhs, mxArray* plhs[], int nrhs, mxArray* prhs[])
{
    Foo^ test = gcnew Foo();
}

This bumps MATLAB’s memory by around 300 MB, although subsequent calls don’t increase the memory further like in my real MEX file.

EDIT:

I answered my own question, the culprit was mxArrayToString

  • 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-06-11T22:39:57+00:00Added an answer on June 11, 2026 at 10:39 pm

    I found the problem, turns out it wasn’t .net related after all… sorry for that red herring

    Since I wasn’t using new, malloc or mxMalloc I wrongly assumed that all my unmanaged memory would be in the stack and cleaned up when the mexFunction ended.

    However mxArrayToString doesn’t return a pointer to the MATLAB data as mxGetData and other mx* functions do. It copies the data onto the heap and one has to call mxFree to release it. I used mxArrayToString as input to create a System::String^, the only change needed was to save a temporay char pointer, use that for the String^ constructor and then mxFree it.

    So once again for SEO: The pointer from mxArrayToString needs to be mxFree’d!

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

Sidebar

Related Questions

How to create a 2d sparse matrix in a MEX-file written in C. After
I'm currently writing a MEX file in C to speed up MATLAB code that
How can I define endpoints with MEX endpoint in app.config file and what I
I have a mex module called p.mexw64 which uses another dll called p.dll The
I have a mex file (compiled in VS2010, Matlab 2010b) which accepts a variable,
Is there a way to get the mex file for a built-in MATLAB m-
I have a C++ file that: starts the matlab engine calls matlab_optimize() (a compiled
I am trying to create a mex file that interfaces MATLAB with an external
I'm currently coding a MEX file that needs to run as quickly as possible
I'm currently writing a MEX file in MATLAB that uses the CPLEX API in

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.