×

Discussion Board

Results 1 to 15 of 33

Hybrid View

  1. #1
    Regular Contributor
    Join Date
    Oct 2013
    Location
    Pennsauken, NJ, USA
    Posts
    120

    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
    120

    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
    120

    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
    120

    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
    120

    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
    526

    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
    Nokia Developer Moderator
    Join Date
    Oct 2006
    Location
    Finland
    Posts
    4,572

    Re: Creating CustomEffect in C++, NullReferenceException

    FYI, this thread has been featured and you can find details of it in the blog

  8. #8
    Registered User
    Join Date
    Mar 2014
    Posts
    1

    Re: Creating CustomEffect in C++, NullReferenceException

    thanks, it's very helpful

  9. #9
    Registered User
    Join Date
    May 2014
    Posts
    9

    Re: Creating CustomEffect in C++, NullReferenceException

    Here's the snag I think you've hit (OP at least):

    DelegatingEffect holds a weak reference to the passed ICustomEffect. Because ICustomEffect can be implemented by the class that also references the DelegatingEffect, had we made this reference strong it would have caused a reference cycle and therefore a memory leak.

    So the non-obvious fact is that the user must somehow keep a strong reference to the ICustomEffect, otherwise the weak pointer in DelegatingEffect will decay to null - nearly immediately.
    That's probably the source of the NullReferenceException seen.

    CustomEffectBase in C# is the current standard implementation of this pattern, and should be interesting for you to study in a reflector/decompiler.

    Since CustomEffectBase implements both IImageProvider and ICustomEffect, you have the necessary strong reference in the hands of anyone having a reference to the subclass.
    CustomEffectBase also has a strong reference to an internal DelegatingEffect, and passes along all IImageProvider property get/set and method calls to it.

    Finally, disposing DelegatingEffect/ICustomEffect should be no more complicated than disposing (or not disposing) any of the other objects in the SDK.

  10. #10
    Nokia Developer Champion
    Join Date
    Nov 2011
    Location
    la bouexiere, france
    Posts
    475

    Re: Creating CustomEffect in C++, NullReferenceException

    This explain a lit of thinks :/

    Quote Originally Posted by cadahl View Post
    DelegatingEffect holds a weak reference to the passed ICustomEffect. Because ICustomEffect can be implemented by the class that also references the DelegatingEffect, had we made this reference strong it would have caused a reference cycle and therefore a memory leak.
    Why set the reference to null in destructor (called with dispose) not resolve reference cycle?

    Personally, i thinks a weak reference is a bad solution here.

    It should be really interesting if you could explain SDK memory management.
    Windows 8, Windows phone 8 or Nokia Asha developer? Go to DVLUP

  11. #11
    Registered User
    Join Date
    May 2014
    Posts
    9

    Re: Creating CustomEffect in C++, NullReferenceException

    Quote Originally Posted by yan_ View Post
    Why set the reference to null in destructor (called with dispose) not resolve reference cycle?
    Simply because we can't require the user to remember to Dispose, this would lead to many more developers having problems. That being said, the documentation of the SDK is fairly C# centered so far.. I'll bring up adding more native sample code.

    We do recommend being vigilant about calling Dispose to ensure deterministic deallocation though, and because it's good practice.

    About memory management within the SDK, there's not much interesting to say. Whether code is in C++/CX, WRL or "standard" style C++ (we use them all) we follow the usual reference counting patterns, which work realiably thanks to smart pointers. The few times that memory issues have arisen during testing, they have involved references crossing the ABI between native and managed domains.
    Last edited by cadahl; 2014-05-30 at 20:51.

  12. #12
    Nokia Developer Champion
    Join Date
    Nov 2011
    Location
    la bouexiere, france
    Posts
    475

    Re: Creating CustomEffect in C++, NullReferenceException

    Quote Originally Posted by cadahl View Post
    Simply because we can't require the user to remember to Dispose, this would lead to many more developers having problems.
    For me,

    When the user allocate a class with IDsiposable, it should call dispose because garbage collector doesn't manage native memory. It's the role of Dispose function. It's the recommended practice and iIt's why we generally use using with native component. If user don't call dispose, deallocated of a lot of native memory will be delayed because garbadge collector doesn't use it in its algorithm. So, you can generate easily out of memory exception. And for me a weak reference is not logic because DelegatingXXX need a ICustomXXX to works. It's not a simple observator.

    If you don't thinks user should dispose, you need to explain who and how memory is used with SDK.

    Maybe i'm wrong, but it's what i've learn with my experience


    Maybe it could be interesting to have a boolean in DelegatingXXX constructor to say him it should manage the ICustomXXXX or not.
    Windows 8, Windows phone 8 or Nokia Asha developer? Go to DVLUP

  13. #13
    Registered User
    Join Date
    May 2014
    Posts
    9

    Re: Creating CustomEffect in C++, NullReferenceException

    Actually, not calling dispose on an IDisposable does not guarantee a leak of native resources, and this has never been a "norm" in .NET. Classes implementing IDisposable usually have a finalizer that is called when the GC eventually cleans up.

    This assumes that the GC can in fact do its job, which it can when the reference cycle involves only .NET objects. It then has the ability to walk the object graph and prove that there are no "root" references left - and can chuck away the whole cycle. However when you cross the border to WinRT so that one object is managed and one is native and they both refer to each other, you're stuck. The GC can no longer detect the cycle and therefore cannot run finalizers and clean up.

    So it's not a question of deallocation being delayed. It just won't happen. (!)

    And to reiterate, we do think users should dispose, to ensure deterministic deallocation. I fully agree, it's crucial in memory constrained scenarios. But that's slightly different from requiring a call to Dispose under the penalty of leaking memory forever.

    We basically chose the lesser of two evils.

    And again, in the wrapping pattern shown by CustomEffectBase, the "weakness" can be hidden rather naturally.

Similar Threads

  1. Replies: 4
    Last Post: 2009-11-24, 14:12
  2. Creating lacal SMS creating panic..
    By amol_benare604 in forum Symbian
    Replies: 2
    Last Post: 2009-11-02, 14:50
  3. Creating a Dialog and creating textboxes,checkboxes
    By tharunjavalimn in forum Symbian User Interface
    Replies: 3
    Last Post: 2008-10-26, 15: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, 13: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
  •  
×