Array of stuctures for repeated use in DLL

9 次查看(过去 30 天)
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