Array of stuctures for repeated use in DLL

I am trying to call a DLL function inside a for loop, but Matlab crashes on the second loop iteration. I think the issue has to do with the arrays of structs the function requires, but I am not sure. The DLL function I am working with has the following arguments: a 1D array of data, the array size, and two 2 arrays of structures. I am trying to access this function via calllib inside for loop. My code looks like the following. Am I doing something wrong? Am I missing something? Is there a better way to go about this? (I feel like there should be!)
% Load library and setup
% Initialize array of structures
tmp(1).a = 0;
tmp(2).a = 0;
tmp(3).a = 0;
tmp(4).a = 0;
structs1 = libpointer('my_struct', tmp);
structs2 = libpointer('my_struct', tmp);
a1s = zeros(N,4);
a2s = zeros(N,4);
for n = 1:N
% get nth new_data
calllib(mylib, 'my_func', new_data, length(new_data), structs1, structs2);
for m = 1:4
tmp_libptr = structs1 + (m-1);
a1s(n,m) = tmp_libtr.Value.a;
tmp_libptr = structs2 + (m-1);
a2s(n,m) = tmp_libtr.Value.a;
end
end
% Unload library and clean up
Also, as a newly discovered oddity. If I run this script from the command window, Matlab crashes on the first call to calllib. If I copy and paste segments of the code individually, the code runs through the first iteration fine, but crashes on iteration 2. For n = 1, I can run calllib multiple times over by copying and pasting it into the Command Window.

댓글 수: 4

What you have given looks ok, More information would be helpful.
  • What does my_func do?
  • Is new_data only an input?
  • How does my_func know the lenght of structs1 and 2?
  • Does the stack trace show the crash is in mylib? If so can you debug where in mylib it is? If not attach the stack trace and we may be able to figure out where in MATLAB it is crashing.
Tyson Lawrence
Tyson Lawrence 2015년 10월 22일
my_func smooths/filters new_data and then calculates various characteristics of the data and stores then those characteristics in structs1 and structs2. I have tested the individual parts of my_func and they all seem to checkout. I am re-testing them again right now. The dll runs fine in LabVIEW and Python.
new_data is only an input. Temporary copies are made internally to the dll for various smoothing/filtering routines, but none of them are passed back out.
The lengths of structs1 and structs2 are (lazily and temporarily) hard coded to 4 in the library right now.
I am not getting an option to view the stack trace.
Tyson Lawrence
Tyson Lawrence 2015년 10월 22일
I did view debug info, but I don't see anything meaningful in the stack trace. The crash is due to heap corruption, which has me confused, because I can use the library without any issues in native C++ code and LabVIEW. My native test code can loop over that function millions of times.
The function does not allocate anything returned in the structs does it? There are no pointer members of my_struct correct?

댓글을 달려면 로그인하십시오.

답변 (1개)

Philip Borghesani
Philip Borghesani 2015년 10월 22일
편집: Philip Borghesani 2015년 10월 22일

0 개 추천

This should not make any difference but you might try this code instead.
...
structs1 = libstruct('my_struct', tmp);
structs2 = libstruct('my_struct', tmp);
structs1p=libpointer( 'my_struct', structs1) ;
structs2p=libpointer( 'my_struct', structs2) ;
a1s = zeros(N,4);
a2s = zeros(N,4);
for n = 1:N
% get nth new_data
calllib(mylib, 'my_func', new_data, length(new_data), structs1, structs2);
for m = 1:4
tmp_libptr = structs1p + (m-1);
a1s(n,m) = tmp_libtr.Value.a;
tmp_libptr = structs2p + (m-1);
a2s(n,m) = tmp_libtr.Value.a;
end
end
The only real improvement you could make to the code is to create an array of pointers ahead of time and use those when extracting data.
%setup code
for n=1:4
s1p(n)=structs1p+(n-1);
s2p(n)=structs2p+(n-1);
end
...
% to use
a1s(n,m) = s1p(m).Value.a;
...

카테고리

도움말 센터File Exchange에서 LabVIEW에 대해 자세히 알아보기

태그

질문:

2015년 10월 22일

편집:

2015년 10월 22일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by