×

Discussion Board

Page 1 of 3 123 LastLast
Results 1 to 15 of 33
  1. #1
    Regular Contributor
    Join Date
    Oct 2013
    Location
    Pennsauken, NJ, USA
    Posts
    107

    Creating CustomEffect in C++, NullReferenceException

    This might be something very stupid and it might be a longshot... but maybe someone here can help.

    I'm attempting to convert one of my CustomEffects that I've written in C# to C++. I'll start out by saying that the effect works just fine in C#, just a little slow but I'm hoping to speed it up.

    I've followed SB Dev's excellent wiki article on this and I've pretty much copied his template code for implementing the effect in C++. I'm not going to post it unless there's no choice (it's fairly large).

    However, to implement the effect in my managed code, all I did was switch one line.... from using the C# effect to the C++ effect like so:

    Code:
    //this is the c++ effect
    using (var edgeEffect = new DelegatingEffect(preFilterEffect, new CustomEffects.FindEdgesEffect((float)LowThresholdDetection, (float)HighThresholdDetection, 2f, 16, false, true)))
    //this is the c# effect
    //using (var edgeEffect = new EdgeDetectionFilter(preFilterEffect, (float)LowThresholdDetection, (float)HighThresholdDetection, 2f, 16, false,
    (FYI, I know I named my C# managed class incorrectly)

    With the C# version, the effect works fine. When I comment it out and replace it with the C++ effect, I get a NullReferenceException. I can't figure out where this is coming from. The effect initialization goes ok. I can step through it using native debugging and see that each variable gets put in the right place. The exception is being thrown when the RenderAsync is called. Putting a breakpoint at the beginning of the effect's Process method does nothing.

    Does anyone have any idea what I might be doing wrong?

  2. #2
    Regular Contributor
    Join Date
    Oct 2013
    Location
    Pennsauken, NJ, USA
    Posts
    107

    Re: Creating CustomEffect in C++, NullReferenceException

    Ok, this is strange. I've tried to run SB Dev's included project source code. I get the same exception and crash, NullReferenceException, on my Nokia Lumia 920.

    (running OS 8.0.10521.155)

  3. #3
    Regular Contributor
    Join Date
    Oct 2013
    Location
    Pennsauken, NJ, USA
    Posts
    107

    Re: Creating CustomEffect in C++, NullReferenceException

    I couldn't make SB Dev's project run on an emulator, so I switched the Nokia SDK references to x86 on my own project. Then I ran the app in an emulator and the custom C++ effect I wrote seems to work. (At least, it runs the code inside the Process method... it's not bug free yet.)

    I'm going to try my wife's Lumia 820 to see if it's just my device, but for the moment, there is something wrong with using DelegatingEffect in ARM.

  4. #4
    Regular Contributor
    Join Date
    Oct 2013
    Location
    Pennsauken, NJ, USA
    Posts
    107

    Re: Creating CustomEffect in C++, NullReferenceException

    SB Dev's solution is not working on my Lumia 820 either (OS 8.0.10517.150)

    I'm stumped. I can't tell if I'm doing something wrong or what.

  5. #5
    Regular Contributor
    Join Date
    Oct 2013
    Location
    Pennsauken, NJ, USA
    Posts
    107

    Re: Creating CustomEffect in C++, NullReferenceException

    Ok, I'm seeing some inconsistencies. I'm using SB Dev's solution from the wiki article to test this odd behavior.

    When I load a relatively small image first (720x960), it works fine. The custom effect runs and the output is displayed. I can then gradually move up to larger images (3552x2000) and the effect runs fine.

    However, if I just go for one of the pictures inside my camera roll (3552x2000) immediately from a cold start of the app, it doesn't work. The effect crashes with a NullReferenceException.

    It seems something needs to load in the background but is not quite finished before it is referenced...

  6. #6
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    511

    Re: Creating CustomEffect in C++, NullReferenceException

    What version of my (native) effect are you running exactlly - the solution contains several. Also could you provide the image you're using for reference in testing? I'll try to find the issue then.

  7. #7
    Regular Contributor
    Join Date
    Oct 2013
    Location
    Pennsauken, NJ, USA
    Posts
    107

    Re: Creating CustomEffect in C++, NullReferenceException

    I'm using the basic native one, MultiplyEffect, since that is as far as I will take my own effect.

    http://1drv.ms/1iRq7VZ
    This picture will crash your sample on my Lumia 920 using a Debug compile both using the debugger and just launching it from the app list.



    http://1drv.ms/1iRqIHa
    This picture will NOT crash your app. I can open it just fine.

  8. #8
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    511

    Re: Creating CustomEffect in C++, NullReferenceException

    I'll definitely give it a look and hope to have an answer soon. Can't guarantee that I'll get to it today though (busy day).

  9. #9
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    511

    Re: Creating CustomEffect in C++, NullReferenceException

    I can verify that the issue occurs for me as well on the 1020. Curiously enough I have no issues running a custom effect in one of my other projects that is based around pretty much the same template. Am I correct that you have put your effect into the framework provided by my sample for testing?

    It's possible that something the sample does is the cause although so far I don't really have the slightest idea what it might be. I'll keep looking though. The really curious part is that you said it works for you if you first use smaller images and only then use the one in full resolution.

  10. #10
    Regular Contributor
    Join Date
    Oct 2013
    Location
    Pennsauken, NJ, USA
    Posts
    107

    Re: Creating CustomEffect in C++, NullReferenceException

    I actually have the effect running in my own code completely independent of your sample. When it constantly threw that exception, I tried to run your unmodified sample with the same images to see if something was wrong with the way I was implementing the effect in C# (DelegatingEffect was very new to me).

    When I run my code on the emulator, I get no errors at all. I was curious if there was some internal timing problems since the emulator runs much more quickly than the phone does. Or maybe there is something wrong with some ARM-optimized code inside the SDK.

    The phenomenon in which I could finally open a large image after opening a few smaller images was strange to me, too. It didn't always work, but I usually had to open at least two smaller images (like the one above) before a large one would no longer crash the app.

    In any case, thanks for looking into this.

  11. #11
    Nokia Developer Champion
    Join Date
    Nov 2011
    Location
    la bouexiere, france
    Posts
    473

    Re: Creating CustomEffect in C++, NullReferenceException

    have you a simple sample with the problem?
    Windows 8, Windows phone 8 or Nokia Asha developer? Go to DVLUP

  12. #12
    Regular Contributor
    Join Date
    Oct 2013
    Location
    Pennsauken, NJ, USA
    Posts
    107

    Re: Creating CustomEffect in C++, NullReferenceException

    SB Dev's sample solution from the wiki also has the problem: http://developer.nokia.com/community...fectSample.zip

    Try opening a large image on an actual device. I posted an image above that crashed my 920.

  13. #13
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    511

    Re: Creating CustomEffect in C++, NullReferenceException

    I believe I have found the reason for the issue - at least for my sample.

    My BitmapComparer class is provided with the DelegatingEffect externally, then sets the ImageSource and tries to render it. Rendering fails. If I manually make a call to PreloadAsync it crashes at that point as well.

    This is the affected code from BitmapComparer.cs:

    Code:
    public async void Compare(Bitmap image, CustomEffectBase referenceEffect, DelegatingEffect comparisonEffect)
            {
                Bitmap refBmp = new Bitmap(image.Dimensions, ColorMode.Bgra8888);
                Bitmap compBmp = new Bitmap(image.Dimensions, ColorMode.Bgra8888);
                DateTime start = DateTime.Now;
                DateTime end = DateTime.Now;
    
                using (BitmapImageSource bis = new BitmapImageSource(image))
                {
                    referenceEffect.Source = bis;
                    using(referenceEffect)
                    using (BitmapRenderer renderer = new BitmapRenderer(referenceEffect, refBmp))
                    {
                        start = DateTime.Now;
                        await renderer.RenderAsync();
                        end = DateTime.Now;
    
                        ReferenceDuration = (end - start).TotalMilliseconds;
                    }
                }
    
                using (BitmapImageSource bis = new BitmapImageSource(image))
                {
                    
                    comparisonEffect.Source = bis;
                    await comparisonEffect.PreloadAsync();//crashes here
                    using (BitmapRenderer renderer = new BitmapRenderer(comparisonEffect, compBmp))
                    {
                        start = DateTime.Now;
                        await renderer.RenderAsync();//if Preload isn't called it crashes here
                        end = DateTime.Now;
    
                        ComparisonDuration = (end.Ticks - start.Ticks) / 10000.0;
                    }
                }
                CompareAndRender(refBmp, compBmp);
            }
    So just for testing purposes I now tried creating the DelegatingEffect along with my CustomEffect directly inside the method while setting the ImageSource as part of the constructor. This works with bigger images also (like your Demo image).

    Code:
    public async void Compare(Bitmap image, CustomEffectBase referenceEffect, DelegatingEffect comparisonEffect)
            {
                Bitmap refBmp = new Bitmap(image.Dimensions, ColorMode.Bgra8888);
                Bitmap compBmp = new Bitmap(image.Dimensions, ColorMode.Bgra8888);
                DateTime start = DateTime.Now;
                DateTime end = DateTime.Now;
    
                using (BitmapImageSource bis = new BitmapImageSource(image))
                {
                    referenceEffect.Source = bis;
                    using(referenceEffect)
                    using (BitmapRenderer renderer = new BitmapRenderer(referenceEffect, refBmp))
                    {
                        start = DateTime.Now;
                        await renderer.RenderAsync();
                        end = DateTime.Now;
    
                        ReferenceDuration = (end - start).TotalMilliseconds;
                    }
                }
    
                using (BitmapImageSource bis = new BitmapImageSource(image))
                {
                    
                    //comparisonEffect.Source = bis;
                    using(DelegatingEffect de = new DelegatingEffect(bis, new CustomEffectNative.MultiplyEffect(2)))
                    using (BitmapRenderer renderer = new BitmapRenderer(de, compBmp))
                    {
                        start = DateTime.Now;
                        await renderer.RenderAsync();
                        end = DateTime.Now;
    
                        ComparisonDuration = (end.Ticks - start.Ticks) / 10000.0;
                    }
                }
                CompareAndRender(refBmp, compBmp);
            }
    So to me it looks like changing the ImageSource outside the constructor might be the issue. If you want to reproduce it for yourself - the method I changed is the on that starts on line 149 of the BitmapComparer (I need to have 4 overloads of the method as there is no common BaseInterface I could use here). I will try and rework the sample and run some more tests.

  14. #14
    Regular Contributor
    Join Date
    Oct 2013
    Location
    Pennsauken, NJ, USA
    Posts
    107

    Re: Creating CustomEffect in C++, NullReferenceException

    That's really strange. I was already specifying the ImageSource in the constructor. I tried the changes to your project and it seems to be working fine. Mine is still inconsistent.

    I tried to put your MultiplyEffect (native) effect into my code at exactly the same place I was trying to run my own custom effect. It's not going to look right, but I just wanted to see if I could throw that error.

    Using your effect within my rendering chain:
    1. I start my app within the debugger:
    2. With the large image I posted already (my son playing with trains), I could render it using the effect. OK
    3. I try a landscape image with the same dimensions (just rotated), it renders. OK

    4. Restart the app within the debugger.
    5. Try the landscape image that just worked immediately. Render FAILS with a NullReferenceException.

    I don't know if it matters, but my rendering chain is fairly long. There are three different layers with multiple effects/filters being blended into one final image. If I substitute the DelegatingEffect with my original C# effect, it works fine. Also, the DelegatingEffect doesn't give me any problems in the emulator.



    One of my rendering chains that throws the exception during RenderAsync:

    Code:
    wBitmap = new WriteableBitmap(_bitmapImage.PixelWidth, _bitmapImage.PixelHeight);
    _sceneryImageSource = new ColorImageSource(new Windows.Foundation.Size(_wBitmap.PixelWidth, _wBitmap.PixelHeight), Windows.UI.Color.FromArgb(255, 255, 255, 255));
    
    //original image
    using (var source = new BufferImageSource(iBuffer))
    using (var preFilterEffect = new FilterEffect(source))
    
    // this is what I'm trying to make work
    //using (var edgeEffect = new DelegatingEffect(preFilterEffect, new CustomEffects.FindEdgesEffect((float)LowThresholdDetection, (float)HighThresholdDetection, 2.0f, 16, false, true)))
    
    // this is your custom effect... just testing
    using (var edgeEffect = new DelegatingEffect(preFilterEffect, new CustomEffects.MultiplyEffect(2)))
    
    // this is the original C# effect I wrote that works 100% of the time
    //using (var edgeEffect = new EdgeDetectionFilter(preFilterEffect, (float)LowThresholdDetection, (float)HighThresholdDetection, 2f, 16, false, true))
    
    // the following 3 effects are also working C# effects I wrote
    using (var fattenEffect = new FattenLineFilter(edgeEffect, (int)Math.Round(LineThickness, MidpointRounding.AwayFromZero), true))
    using (var mlaaEffect = new MLAAEffect(fattenEffect))
    using (var whiteToAlphaEffect = new WhiteToAlphaFilter(mlaaEffect))
    {
         if ((int)PreBlurAmount > 0)
         {
              var preBlurEffect = new BlurFilter((int)PreBlurAmount);
              preFilterEffect.Filters = new IFilter[] { preBlurEffect };
         }
         //now have black lines on transparent background
         using (var secondFilterEffect = new FilterEffect(source))
         using (var finalRenderer = new WriteableBitmapRenderer(secondFilterEffect, _wBitmap))
         {
              if ((int)BlurAmount > 0)
              {
                   var blurFilter = new BlurFilter((int)BlurAmount);
                   var cartoonFilter = new CartoonFilter();
                   var secondBlend = new BlendFilter(whiteToAlphaEffect, BlendFunction.Normal, 1.0);
                   secondFilterEffect.Filters = new IFilter[] { blurFilter, cartoonFilter, secondBlend };
              }
              else
              {
                   var cartoonFilter = new CartoonFilter();
                   var secondBlend = new BlendFilter(whiteToAlphaEffect, BlendFunction.Normal, 1.0);
                   secondFilterEffect.Filters = new IFilter[] { cartoonFilter, secondBlend };
              }
              await finalRenderer.RenderAsync();
              _wBitmap.Invalidate();
         }
    }
    ImageSource = _wBitmap;
    Last edited by leemcpherson; 2014-03-19 at 01:44. Reason: code readability

  15. #15
    Nokia Developer Champion
    Join Date
    Nov 2011
    Location
    la bouexiere, france
    Posts
    473

    Re: Creating CustomEffect in C++, NullReferenceException

    Have you try to increase memory limit with ID_FUNCCAP_EXTEND_MEM
    http://msdn.microsoft.com/en-us/libr...v=vs.105).aspx
    Windows 8, Windows phone 8 or Nokia Asha developer? Go to DVLUP

Similar Threads

  1. Replies: 4
    Last Post: 2009-11-24, 13:12
  2. Creating lacal SMS creating panic..
    By amol_benare604 in forum Symbian
    Replies: 2
    Last Post: 2009-11-02, 13:50
  3. Creating a Dialog and creating textboxes,checkboxes
    By tharunjavalimn in forum Symbian User Interface
    Replies: 3
    Last Post: 2008-10-26, 14:00
  4. Creating Themes on the Fly ( creating the SIS file )
    By bayugyug in forum Streaming and Video
    Replies: 0
    Last Post: 2005-10-20, 12:16
  5. Creating MTM HELP!!!!
    By pajak in forum Smart Messaging
    Replies: 0
    Last Post: 2004-01-23, 12:27

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×