The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Segmentation fault when accessing function in a DLL
Replies: 5   Last Post: Apr 4, 2014 9:54 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Erik Benkler

Posts: 3
Registered: 4/16/09
Re: Segmentation fault when accessing function in a DLL
Posted: Apr 23, 2009 11:55 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Dear Phil,
thanks for the suggestion. My problem is now that I do not exactly know how to initialize the structure. I tried several ways, two of which I will show here (none of them worked):
First approach:
%%%beginning of code%%%%%%%%%%%
if not(libisloaded('USMCDLL'))
% first, define a Matlab structure which will be used for initialization of
% the library structure. Use cellstr, because this seems to be the correct
% type for the 'Serial' and 'Version' fields, but I'm not sure. In the C
% header file, the structure is defined by:
% typedef struct USMC_Devices_st{
% DWORD NOD; // Number of the devices ready to work
% char **Serial; // Array of 16 byte ASCII strings
% char **Version; // Array of 4 byte ASCII strings
%} USMC_Devices;
Devices_st_ml.Serial=cellstr(['0000000000000001';'0000000000000002';'0000000000000003';'0000000000000004']); % for 4 connected devices, there should be 4 x 16 byte strings in the Serial field.
Devices_st_ml.Version=cellstr(['0000';'0000';'0000';'0000']) %for 4 connected devices, there should be 4 x 4 byte strings in the Version field
%%%end of code%%%%%%%%%%%

Matlab crashes when the last line is executed.
In the second approach, I do the following:

%%%beginning of code%%%%%%%%%%%
if not(libisloaded('USMCDLL'))
calllib('USMCDLL', 'USMC_Init', Devices);
%%%end of code%%%%%%%%%%%

Running this code does NOT crash Matlab, although the calllib statement is executed. So it seems to be a bit better. The Devices.Value statement before calllib shows that the pointer to the structure is almost correctly initialized. However, the 'Serial' and 'Version' fields of the structure are both empty cell arrays of size {0x1 cell}. If I try to access the structure to which the pointer 'Devices' points, Matlab crashes, e.g. by typing:
after the calllib statement.

If I try to initialize these fields 'Serial' and 'Value' as well before the libpointer statement, Matlab crashes. So, I have the impression that my problem is related to the initialization of these two fields.
May the problem be connected to the statements
char **Serial; // Array of 16 byte ASCII strings
char **Version; // Array of 4 byte ASCII strings
in the definition of the structure in the header file? Or may there be some other things in the header file which could be misinterpreted by the Matlab C compiler, e.g., because the header file is written in C++? My problem is that I do not understand much of C or C++ and even less of programming DLLs. There is a hint in the first comments in the header file, which I do not understand, but which might be important for this issue:
// Note::C++
// Use functions with reference arguments as in sample (there is a sample C++ file how to use the DLL functions)
// Note.C
// Use functions with address of operator in invoke expression: func(var1, &struct);

What might this mean? What is the address of operator??? Sorry if I am asking stupid questions...

Best regards,

"Philip Borghesani" <philip_borghesani@mathworks.spam> wrote in message <gsi0ek$9st$>...
> Erik
> You are passing a NULL pointer to the function 'USMC_Init' is that what the function expects or does it expect a pointer to a valid
> location? To pass a valid pointer initialize at least one field of the structure before passing it to USMC_Init.
> Phil
> "Erik Benkler" <> wrote in message news:gs7f7m$8k9$

> > Hi everybody,
> > I am trying to use functions in a windows DLL for controlling stepper motor hardware. The stepper motor manual says that the DLL
> > should work with Matlab. So, I read carefully the external interfacing section in Matlab help and related articles in several
> > newsgroups. I think I am doing everything right, but Matlab always produces a segmentation fault when I make the calllib statement
> > to the function USMC_Init in the DLL. Before the callib statement, the library seems to be loaded correctly and the correct data
> > structures seem to be generated and initialized (see line 18 in the m-file). The files necessary to reproduce this behaviour are
> > available at Before the USMCDLL.dll can be used, the MicroSMC driver
> > must be installed on the system and using the DLL requires administrative rights. For more information see the comments in the
> > USMCDLL_test.m file and in the Matlab error message in segmentation_errormessage.txt. Am I doing something wrong in the m-file or
> > is there a problem in Matlab or in the DLL? For people who don't want to download the files, I post the contents of the m-file
> > below.
> > Thanks for any help in advance,
> > Erik
> >
> > % OS is Windows XP SP2, Matlab version is (R14) SP2
> > % ATTENTION: The user must be logged in with administrative rights,
> > % otherwise the USMCDLL library does not work
> > % Before the USMCDLL library can be used, it must be installed with
> > % setup.exe in the MicroSMC folder.
> > % Calls to the USMCDLL library first invoke MicroSMC.exe. Access to the
> > % library functions without MicroSMC properly installed does not work and
> > % produces an error message box, independet of Matlab).
> > % The USMCDLL.h header file must be located on the current Matlab path.
> >
> > clear all
> > if not(libisloaded('USMCDLL'))
> > loadlibrary('USMCDLL','USMCDLL.h')
> > end
> > %libfunctionsview('USMCDLL')
> >
> > Devices=libpointer('USMC_Devices_stPtr');
> > %Devices.Value %this correctly shows %initialized values in the data structure USMC_Devices_st as
> > %defined in USMCDLL.h header file
> > calllib('USMCDLL', 'USMC_Init', Devices); % here, a reproducible %segmentation violation occurs


Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.