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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:39:46+00:00 2026-06-10T13:39:46+00:00

I have a very simple program, listed below, which reads a value from a

  • 0

I have a very simple program, listed below, which reads a value from a .mat file (a data file from Matlab) and prints it. For some reason, I get a segfault error after exiting main() – I can run gdb my_program and step through the entire method, but as soon as main() finishes, I enter some method in a Matlab related library (libmwfl.so, a dependency of libmat.so) which throws a segfault.

I am completely new to C programming, but some reading up I suspect that I’m either somehow corrupting the stack or calling some destructor twice. However, I can’t see any of those in my code – and as I said, I can step through my code with the debugger without problems.

What am I doing wrong here?

#include <stdlib.h>
#include <stdio.h>
#include <mat.h>

int main(int argc, char *argv[]) {

    double value;
    MATFile *datafile;
    datafile = matOpen("test.mat", "r");

    mxArray *mxv;
    mxv = matGetVariable(datafile, "value");
    value = *mxGetPr(mxv);
    mxFree(mxv);
    matClose(datafile);

    printf("The value fetched from the .mat file was: %f", value);

    return 0;
}
  • 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-10T13:39:47+00:00Added an answer on June 10, 2026 at 1:39 pm

    The documentation recommends to use the function mxDestroyArray instead of mxFree to free an mxArray. By using mxFree you probably mess up the matlab’s heap. From the documentation

    Improperly Destroying an mxArray

    You cannot use mxFree to destroy an mxArray.

    Warning: You are attempting to call mxFree on a <class-id> array. The destructor for mxArrays is mxDestroyArray; please call
    this instead. MATLAB will attempt to fix the problem and continue,
    but this will result in memory faults in future releases.

    Example That Causes Warning

    In the following example, mxFree does not destroy the array object.
    This operation frees the structure header associated with the array,
    but MATLAB will still operate as if the array object needs to be
    destroyed. Thus MATLAB will try to destroy the array object, and in
    the process, attempt to free its structure header again.

    mxArray *temp = mxCreateDoubleMatrix(1,1,mxREAL);

      ...
    

    mxFree(temp); /* INCORRECT */

    Solution.

    Call mxDestroyArray instead.

    mxDestroyArray(temp); /* CORRECT */

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

Sidebar

Related Questions

So I have a very simple program that reads the 3 first bytes of
I have a very very simple program that parses a csv file that has
I have a very simple program in swi-prolog, which in I define all predicates
I have a very simple test program, running on Solaris 5.8: #include <stdio.h> #include
It's a very simple program. I have a function defined on the top and
I have a very simple question. I have a program that accepts an integer
I have a very simple table called Member , which consists of the following:
I have a very simple fxml file, with a checkbox: ... <AnchorPane id=AnchorPane xmlns:fx=http://javafx.com/fxml
I have a very simple program that I am trying to improve performance. One
I have a very simple print program called print.c: #include <stdio.h> int main(void){ printf(Random

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.