From: Jules B. <ju...@je...> - 2008-03-14 08:34:57
|
Eric Kow wrote: >> So somehow, sometimes, we're getting an invalid pointer. Whether it's a >> pointer to a buffer which is too short, or a buffer which has been >> moved, or.... > > Another observation is that this fails much more easily with bigger buffers. > > A sort of silly question: do you think you could cook up an equivalent > demonstrator in C++ just using wxWidgets? Yes. But it doesn't segfault :( I have reduced the haskell code to the following simplified version: import qualified Graphics.UI.WX as WX import Graphics.UI.WXCore.Image import Graphics.UI.WXCore.Draw import Graphics.UI.WXCore.WxcClassesAL import Graphics.UI.WXCore.WxcClassesMZ import Foreign import Data.Word import Control.Monad main = do forM_ [0..500] (\i -> putStrLn ("Iteration : " ++ show i) >> createAndGetPixels) createAndGetPixels = do image <- imageCreateSized (WX.Size 256 256) pixels <- imageGetData image putStrLn $ "pixels : " ++ show pixels -- putStr $ "Reading byte.." -- forM_ [0..256*256*3] $ \b -> do -- byte <- peekByteOff (castPtr pixels) b :: IO Word8 -- putStr (show b ++ "..") bytes <- peekArray (256*256*3) (castPtr pixels) putStrLn $ "bytes : " ++ show (length (bytes :: [Word8])) (The commented out code was me experimenting to see if there was a pattern in "which" byte causes the segfault. There isn't, that I can see) And here is my attempt to replicate this exact set of calls from C++: #include <iostream> #include "wx/wx.h" #include "wx/image.h" void createAndGetPixels(); int main() { for (int i=0; i<500; i++) { std::cout << "Iteration " << i << std::endl; createAndGetPixels(); } } void createAndGetPixels() { wxImage *im = new wxImage(256,256); unsigned char* pixels = im->GetData(); std::cout << "pixels : " << ((void*)pixels) << std::endl; void *bytes = malloc(256*256*3); memcpy(bytes,pixels,256*256*3); //free(bytes); //delete im; } And it doesn't seem to segfault, ever. If you leave the "free" and "delete" in then the nice deterministic C malloc gives you back the same block of memory each time! that's why I commented them out. Jules |