Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8397931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:54:18+00:00 2026-06-09T20:54:18+00:00

I am new to Haskell programming, Foreign Function Interface and Stackoverflow. I am trying

  • 0

I am new to Haskell programming, Foreign Function Interface and Stackoverflow. I am trying to build a Haskell FFI binding for a C based library. Please find below a hypothetical example which is very similar to my current problem:

Consider I have a C struct and a function like this:

typedef struct {
      int someInt;
      void *someInternalData;
   } opaque_t;

int bar (opaque_t *aPtr, int anArg);

The opaque C structure is the out parameter here. I should pass on the same to other APIs. The caller need not de-reference the opaque struct.

Find below myFFI.hsc file with FFI imports:

{-# LANGUAGE CPP, ForeignFunctionInterface #-}
module MyFFI where
import Foreign
import Foreign.Ptr
import Foreign.ForeignPtr
import Foreign.C.Types
import Foreign.C
import System.IO.Unsafe
import Foreign.Marshal
import qualified Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
import qualified System.IO (putStrLn)

#include "myclib.h"

newtype OpaquePtr = OpaquePtr (ForeignPtr OpaquePtr)

#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__) 
instance Storable OpaquePtr where
    sizeOf _ = #{size opaque_t}
    alignment _ = #{alignment opaque_t}
    peek _ = error "Cant peek"

foreign import ccall unsafe "myclib.h bar"
    c_bar :: Ptr OpaquePtr
                -> CInt
                -> CInt

barWrapper :: Int -> (Int, ForeignPtr OpaquePtr)
barWrapper anArg = System.IO.Unsafe.unsafePerformIO $ do
    o <- mallocForeignPtr
    let res = c_bar (fromIntegral anArg) (Foreign.ForeignPtr.Unsafe.unsafeForeignPtrToPtr o)
    return ((fromIntegral res), o)

In my actual code, similar implementation of the above seems to work. But when I pass around the opaque struct reference, I am getting weird output and some times the ghci crases.

I am not sure about the usage of mallocForeignPtr and ForeignPtr in FFI call. For a long living reference we should use ForeignPtr + mallocForeignPtr, but we cannot pass a ForeignPtr in a ccall. How to do it then? Is my above logic correct?
Any kind of help would be really great. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:54:19+00:00Added an answer on June 9, 2026 at 8:54 pm

    Well, I think I found a solution with proper usage of ForeignPtr and mallocForeignPtr:

    barWrapper :: Int -> (Int, Either error (ForeignPtr OpaquePtr))
    barWrapper anArg = System.IO.Unsafe.unsafePerformIO $ do
        o <- mallocForeignPtr
        withForeignPtr o $ \opaque_ptr -> do
            let res = c_bar (fromIntegral anArg) opaque_ptr
            if res /= (-1)
                then return ((fromIntegral res), Right o)
            else
                return ((fromIntegral res), Left $ error "some problem")
    

    The issues were:

    1. I overlook the lazy evaluation thingy of Haskell : only on accessing the ‘res’ the foreign call will be executed. So, I had to use a if / else block to make it call the ccall
    2. I should be using withForeignPtr instead of unsafeForeignPtrtoPtr
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to both Haskell and programming. My question about binding in pattern-matched,
I am new to Haskell, and programming in general. I am trying to define
I'm new in Haskell programming. Now i'm learn lambda function and lambda abstractions. And
Please bear with me as I am very new to functional programming and Haskell.
I'm very new to Haskell and I'm just trying to find the sum of
I am new to Haskell and trying to fiddle with some test cases I
Hello I am new at Haskell and i'm having problems trying to get this
I'm new to Haskell and, in general, to functional programming, and I'm a bit
i'm new in Haskell. I have to write function that takes list of Integers
I'm new in Haskell and start to write simple XML generator library. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.