Array of stuctures for repeated use in DLL

조회 수: 6 (최근 30일)
Tyson Lawrence
Tyson Lawrence 2015년 10월 22일
편집: Philip Borghesani 2015년 10월 22일
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
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.
Philip Borghesani
Philip Borghesani 2015년 10월 22일
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일
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;
...

카테고리

Help CenterFile Exchange에서 LabVIEW에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by