Search code examples
c++stack-overflowunmanagedmanaged

Stackoverflow exception raised when instanciating an unmanaged class from a clr gui


I'm writing a c++/clr gui to wrap around some unmanaged c++ code I am writing for testing purposes.

I use a proxy class called HolderOfUnmanagedStuff to instanciate the few unmanaged objects I need in my clr gui.

#pragma once
#include "foo.h"
namespace fooCore
{
    public class HolderOfUnmanagedStuff
    {
    public:
        HolderOfUnmanagedStuff()
        {
            foos = std::vector<fooCore::foo>();
        }
        ~HolderOfUnmanagedStuff() { }
    public:
        std::vector<fooCore::foo> foos;
        void addFoo(std::string & fileName)
        {
            whatever code on the first line    // <-- System.StackOverflowException

            foo myfoo(FileName);  // <--- when I comment those 2 lines no exception
            foos.push_back(foo);  //      but exception when I comment only this one
        }
        otherUnmanagedMembers ...
    };
}

My foo class uses a constructor that reads the object from a file:

#pragma once
namespace fooCore
{    
    class foo
    {
    public:
        foo();
        foo(std::string & fileName);
        ~foo();

         fooMembers...
    };
}

Whenever i try to instanciate the foo class, i get a Stack OverFlow exception with a break on the first line of the function.

The call stack only shows the logical call hierarchy: a button event, then a call to the addFoo function, and that's all

I've tried multiple solutions, rewrote every constructor, checked for unmanaged/managed conflict, pointers, addresses, references.

I've learned a lot (obviously, since i hadn't written unmanaged code for 15 years) but I can't find the issue. Any idea?

EDIT

I tried a few things :

  1. added a foo member to my Holder : OK

  2. trying to define that member in the constructor : exception in the constructor

  3. pushing the foo member to the foo vector in the addFoo function : OK

  4. defining the foo member in the addFoo : exception

The following code shows the 4 tests i made :

public class HolderOfUnmanagedStuff
{
    public:
        HolderOfUnmanagedStuff()
        {
            ...
            memberFoo = foo(); // TEST 2 : SOF exception on the first line of the constructor
        }
        ~HolderOfUnmanagedStuff() { }
    public:
        foo memberFoo;    // TEST 1 : No exception with that line 
        std::vector<fooCore::foo> foos;
        void addFoo(std::string & fileName)
        {
            ...               
            foos.push_back(memberFoo);  // TEST 3 No exception with that line
            memberFoo = foo();  // TEST 4 SOF on the first line of the function
        }
        otherUnmanagedMembers ...
    };

Solution

  • The exception was due to the fact that my foo class was too big for the stack.

    I modified my code so that every foo object is declared in the data segment (on the heap) by the default constructor. No more stack overflow, and the data structure is more logical.