Nevron Chart
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » C# Language » PInvoke Error for FFTW
       
Author Reply
Chris Foley
posted 19 posts
since Jul 31, 2010 
from Pearland, TX

PInvoke Error for FFTW

  Posted on: 10 Sep 2010       
Major Problem....This code consistently generates an unbalanced stack error.  I have even tried to change the Wrapper class argument to an IntPtr and that does not work....I am stumped....the "suggestion" is that my parameters do not match the number of arguments expected by the dll.  Anyone used this wrapper class before?  Any Problems?


ANSI C base function in the dll is:
      void *fftwf_malloc(size_t n);
Wrapper Implementation for PInvoke is:
      IntPtr malloc(int length);                     //in a wrapper class called fftw
My code simply calls:
      fftw.malloc(1000);                                //hardcoded test to see why this is failing...
   fftw.malloc(FFTSampleSize*sizeof(float)*2);       //where FFTSampleSize is a int 



Bechir Bejaoui
posted  1450 posts
since  Dec 06, 2007 
from 

 Re: PInvoke Error for FFTW
  Posted on: 10 Sep 2010        0  

Hi
Are you using 3.5 or 4.0, because the 4.0 resolves you problem by using optional parameters
Every one among us was a beginner once.
http://dotnetuniver.blogspot.com/
Chris Foley
posted  19 posts
since  Jul 31, 2010 
from  Pearland, TX

 Re: PInvoke Error for FFTW
  Posted on: 13 Sep 2010        0  
Target Framework: .NET Framework 4 Client Profile.

I have tried reworking the wrapper declaration as uint was well...same issue.
Bechir Bejaoui
posted  1450 posts
since  Dec 06, 2007 
from 

 Re: PInvoke Error for FFTW
  Posted on: 13 Sep 2010        0  
Please send the detail error or exception that happens when you run the code
Every one among us was a beginner once.
http://dotnetuniver.blogspot.com/
Chris Foley
posted  19 posts
since  Jul 31, 2010 
from  Pearland, TX

 Re: PInvoke Error for FFTW
  Posted on: 13 Sep 2010        0  
PInvokeIssue.gif
Bechir Bejaoui
posted  1450 posts
since  Dec 06, 2007 
from 

 Re: PInvoke Error for FFTW
  Posted on: 13 Sep 2010        0  
Sorry but I didn't see any screenshot, Now I see it OK
Every one among us was a beginner once.
http://dotnetuniver.blogspot.com/
Chris Foley
posted  19 posts
since  Jul 31, 2010 
from  Pearland, TX

 Re: PInvoke Error for FFTW
  Posted on: 13 Sep 2010        0  
I had to go back a second time and load the image...the above post should have the unbalanced stack message now.  I also went back and verified that I do have the 32-bit version of the dll from FFTW installed.
Bechir Bejaoui
posted  1450 posts
since  Dec 06, 2007 
from 

 Re: PInvoke Error for FFTW
  Posted on: 13 Sep 2010        0  
The parameter issue couldn't only be the unique raison (Of Corse you verify the parameter number,order and size) It could be also a bug witihin the unmanaged code, I mean the invoked native function unbalances the  stack because of a given  bug.

I remember  I have read a good artile for simila issue, I thing this one
http://msdn.microsoft.com/en-us/magazine/cc163606.aspx

Meanwhile, I give you and alternative solution, if you have the unmanaged library then you could wrap up it and use it, I invite you to take a look on my articles in  this  context
http://www.c-sharpcorner.com/UploadFile/yougerthen/510262008060749AM/5.aspx?ArticleID=d2da1ab3-b92d-499d-94cd-bf54d91e14c3



Every one among us was a beginner once.
http://dotnetuniver.blogspot.com/
Chris Foley
posted  19 posts
since  Jul 31, 2010 
from  Pearland, TX

 Re: PInvoke Error for FFTW
  Posted on: 13 Sep 2010        0  
nope, this is all 32-bit...PC, OS (Windows XP Pro 2002, SP 3) with the 32-bit dll.

I have access to the unmanaged code BUT I do not have the tools to compile and link it....

I have tried the modifying the PInvoke Signature for all of the following...

IntPtr
int
unit
long

All of these give the same stack imbalance error.  The original code is based on size_t which can only be a 32-bit or 64-bit integer...right?


Bechir Bejaoui
posted  1450 posts
since  Dec 06, 2007 
from 

 Re: PInvoke Error for FFTW
  Posted on: 13 Sep 2010        0  
Try to use this argument staff instead

IntPtr
int
UIntPtr  because size_t is an unsigned and Inptr change its size dynamically to fit the variable size
long

Or this one

IntPtr
int
IntPtr
long


Every one among us was a beginner once.
http://dotnetuniver.blogspot.com/
Chris Foley
posted  19 posts
since  Jul 31, 2010 
from  Pearland, TX

 Re: PInvoke Error for FFTW
  Posted on: 13 Sep 2010        0  
hmmm...same error.....
Bechir Bejaoui
posted  1450 posts
since  Dec 06, 2007 
from 

 Re: PInvoke Error for FFTW
  Posted on: 13 Sep 2010        0  
I m pretty sure that the problem comes from the unmanaged code. Verify if the given unmanaged libray has dependencies on other libraries, you know the function that you call could call anther code source outside the assembly
Every one among us was a beginner once.
http://dotnetuniver.blogspot.com/
Chris Foley
posted  19 posts
since  Jul 31, 2010 
from  Pearland, TX

 Re: PInvoke Error for FFTW
  Posted on: 13 Sep 2010        0  
I think I just solved this...it seems to be in the DllImport arguments.  I kept thinking that I have sent every datatype and still have the exact same issue so I tried to understand all the modifiers (still do not understand all of them).  However, I found an obscure reference that said something like "if you keep getting this error, try setting the CallingConvention = Cdecl.  That did it.  However, I have to change the entire wrapper class as this happens on each and every function exported.



[DllImport("libfftw3f-3.dll",
EntryPoint = "fftwf_malloc",
ExactSpelling = true,
             CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr malloc(int length);
Bechir Bejaoui
posted  1450 posts
since  Dec 06, 2007 
from 

 Re: PInvoke Error for FFTW
  Posted on: 13 Sep 2010        0  
I think is not related with your case, you know this enumeration value specifying that the native function is using a varargs multi dimension variable like params but in your case I dont think so that this is the issue
Every one among us was a beginner once.
http://dotnetuniver.blogspot.com/
krishna v
posted  16 posts
since  Feb 08, 2006 
from 

 Re: PInvoke Error for FFTW
  Posted on: 14 Sep 2010        0  

try modifying the signature to unit16 / uint32
I remember size_t is typedef for unsinged int
IntPtr malloc(uint16 length);  
IntPtr malloc(uint32 length);  

 

Chris Foley
posted  19 posts
since  Jul 31, 2010 
from  Pearland, TX

 Re: PInvoke Error for FFTW
  Posted on: 15 Sep 2010        0  
I tried that and got the same error.  However, the calling convention change has worked....go figure.  As soon as I got the fftwf.malloc() function working, I got the exact same error on the other methods in the FFTWF class.  I have updated all of them with the change and they now all function as they should.
Bechir Bejaoui
posted  1450 posts
since  Dec 06, 2007 
from 

 Re: PInvoke Error for FFTW
  Posted on: 15 Sep 2010        0  
Hi Chris

What do you mean by  the calling convention change Chris


Every one among us was a beginner once.
http://dotnetuniver.blogspot.com/
Chris Foley
posted  19 posts
since  Jul 31, 2010 
from  Pearland, TX

 Re: PInvoke Error for FFTW
  Posted on: 15 Sep 2010        0  
look up several post and note the DLLImport arguments where I added the CallingConvention statement.
Jim Tallent
posted  5 posts
since  Dec 14, 2010 
from 

 Re: PInvoke Error for FFTW
  Posted on: 14 Dec 2010        0  
Hello Chris,

I'm been working down the same path over the past two weeks trying to use the following:

wrapper class is from http://www.sdss.jhu.edu/~tamas/bytes/fftwcsharp.html

I am hopping you can help me with a few issues I'm having in implementing this class/wrapper successfully as there is not much info I can find.  If so, I will post my question here.

Thanks
Jim

Chris Foley
posted  19 posts
since  Jul 31, 2010 
from  Pearland, TX

 Re: PInvoke Error for FFTW
  Posted on: 15 Dec 2010        0  
Jim,

I have had to put this project aside for the moment.  I did get a clean compile by changing the calling convention in the PInvoke signature.  HOWEVER, I have not been able to a clean FFT using the functions yet.  I am not sure that my input data is correct and have not had a chance to verify it.  

Chris
Jim Tallent
posted  5 posts
since  Dec 14, 2010 
from 

 Re: PInvoke Error for FFTW
  Posted on: 15 Dec 2010        0  
Hello Chris,

I was also able to get a clean compile and have been running the FFTs.  However my FFT results are questionable.  I can send you what I have if you plan to get back on the project. Maybe between the two of us we can resolve issues. One of my questions is regarding the data input and output requirements since I am using the fftw_complexarray. 

Maybe you can tell me this: 
When I input my real data to the fftw_complexarray, as per my understanding, the data must be formatted as alternating real and imaginary parts.  So, if I have an an array of 4 real numbers the input to the fftw_complexarray must be of size 8 and the data copied to the fftw_complexarray needs to look like: real_1,Imag_1, real_2,Imag_2, real_3,Imag_3, real_4,Imag_4. Where all Imaginary data is initially set to "0".

Following the FFT execution I perform addition calculations on the FFT output, and likewise my understanding is that the FFT output data is formatted in the array as:  real_1,Imag_1, real_2,Imag_2, real_3,Imag_3, real_4,Imag_4, ...

I am trying to execute a Phase_Correlation on two 1-D real data arrays using the example provided at:
http://nashruddin.com/phase-correlation-function-in-opencv.html
Once successful I'll go for the 2-D array (two images).

Thanks
James



Sam Hobbs
posted  5888 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: PInvoke Error for FFTW
  Posted on: 15 Dec 2010        0  

Chris: the error message you got about the stack does say very clearly that the cause might be the calling convention. Therefore it makes sense to me that specifying cdecl works.

The malloc function is part of C/C++. You can find the "signature" in the headers; it is:
void * __cdecl malloc(_In_ size_t _Size);
Using that, it is clear that calling convention is cdecl.
 
I doubt however that you you need to use malloc; the .Net Interop probably have safer ways to do what you need to do.
Thinking is a feeling; pleasant for some and unpleasant for others.
Sam Hobbs
posted  5888 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: PInvoke Error for FFTW
  Posted on: 15 Dec 2010        0  
James: it appears to me that your problem is totally different. I am concerned that if either of you try to use the other's solutions then one or both of you will get confused. Something that someone suggests for one of you is likely to confuse the ohter. I suggest that you create a new thread for your situation.
Thinking is a feeling; pleasant for some and unpleasant for others.
Chris Foley
posted  19 posts
since  Jul 31, 2010 
from  Pearland, TX

 Re: PInvoke Error for FFTW
  Posted on: 16 Dec 2010        0  
Sam, 

fftw.malloc is not the standard malloc in C.  It is a special one that aligns the memory in continuous memory blocks (I think).  It is specific to the fftw library and I have to use Interop to use it.

Chris
Sam Hobbs
posted  5888 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: PInvoke Error for FFTW
  Posted on: 16 Dec 2010        0  
Okay, so I made a mistake. So are you ignoring everything I said? There is a "signature" in a header someplace that specifies the calling convention and the parameters, so you don't have to guess.
Thinking is a feeling; pleasant for some and unpleasant for others.
Chris Foley
posted  19 posts
since  Jul 31, 2010 
from  Pearland, TX

 Re: PInvoke Error for FFTW
  Posted on: 16 Dec 2010        0  
oh lord no.  What you have said is accurate and I fully appreciate the input.  The problem now is that I cannot get sample data to give me a clean and accurate FFT.  I just need to find more time to get into it without interruptions.
Michael Lamming
posted  1 posts
since  Feb 03, 2011 
from 

 Re: PInvoke Error for FFTW
  Posted on: 03 Feb 2011        0  
Did anybody resolve this finally. I'm struggling to use the dll from C# too. Seems a shame to have to have to change the calling conventions on every signature.
Sam Hobbs
posted  5888 posts
since  Sep 07, 2009 
from  Los Angeles, California, USA

 Re: PInvoke Error for FFTW
  Posted on: 03 Feb 2011        0  
If the calling convention has been incorrectly specified on every signature then the mistake must be undone on every signature.
Thinking is a feeling; pleasant for some and unpleasant for others.
       
Mindcracker MVP Summit 2012
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Discover the top 5 tips for understanding .NET Interop
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved