<div>Sorry, forgot to post it to the mailing list.</div>
<div>The call stack is given below. IpOptWrapper is my wrapper dll which communicates with the c# interface.</div>
<div> </div>
<div> IpOptWrapper.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign() + 0xa6 bytes C++<br> IpOptWrapper.dll!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::operator=() + 0x22 bytes C++<br>
IpOptWrapper.dll!std::_Locinfo::_Locinfo_ctor() + 0x39 bytes <br> IpOptWrapper.dll!std::_Locinfo::_Locinfo() + 0xb1 bytes C++<br> IpOptWrapper.dll!std::ctype<char>::ctype<char>() + 0x68 bytes C++<br> IpOptWrapper.dll!std::ctype<char>::_Getcat() + 0x55 bytes C++<br>
IpOptWrapper.dll!std::use_facet<std::ctype<char> >() + 0xc6 bytes C++<br> IpOptWrapper.dll!std::basic_ios<char,std::char_traits<char> >::widen() + 0x3e bytes C++<br> IpOptWrapper.dll!std::basic_ifstream<char,std::char_traits<char> >::basic_ifstream<char,std::char_traits<char> >() + 0x81 bytes C++<br>
IpOptWrapper.dll!Ipopt::IpoptApplication::Initialize() + 0x143 bytes C++<br></div>
<div>Thanks,</div>
<div>Nandini<br><br></div>
<div class="gmail_quote">On Wed, Dec 10, 2008 at 12:48 PM, Nandini Chakravorti <span dir="ltr"><<a href="mailto:nchakravorti@gmail.com">nchakravorti@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">
<div>Damien/Ali,</div>
<div> </div>
<div> Giving more information about the code.</div>
<div class="Ih2E3d">
<div> </div><font color="#0000ff" size="2">
<p>class</p></font><font size="2">Wrapper
<p>{ </p>
<p></p></font><font color="#0000ff" size="2">private</font><font size="2">:
<p>SmartPtr<IpoptApplication> Impl;</p>
<p>SmartPtr<TNLP> mynlp;</p>
<div></div></font>public:</div><font size="2">
<p>Wrapper::Wrapper(</p></font><font color="#0000ff" size="2">bool</font><font size="2"> create_console_out ,</font><font color="#0000ff" size="2">bool</font><font size="2"> create_empty )
<p>{</p>
<p>mynlp = </p></font><font color="#0000ff" size="2">new</font><font size="2"> IpOptNLP(</font><font color="#0000ff" size="2">this</font><font size="2">);
<p>Impl = </p></font><font color="#0000ff" size="2">new</font><font size="2"> IpoptApplication();
<div></div></font>}
<div><font size="2">
<p>ApplicationReturnStatus Wrapper::Initialize(</p></font><font color="#0000ff" size="2">char</font><font size="2">* paramsFile)
<p>{ </p>
<p>ApplicationReturnStatus ret; </p>
<p>ret = Impl->Initialize(paramsFile);</p>
<p></p></font><font color="#0000ff" size="2">return</font><font size="2"> ret;
<p>}</p></font></div>
<div>};</div>
<div> </div>
<div><strong>And my NLP is :</strong></div>
<div>
<div class="Ih2E3d"><font color="#0000ff" size="2">
<p>class</p></font><font size="2">IpOptNLP: </font><font color="#0000ff" size="2">public</font><font size="2"> TNLP
<p>{ </p>
<p></p></font><font color="#0000ff" size="2">private</font><font size="2">:
<p>Wrapper* m_pcUnManagedWrapper;</p>
<p></p>
<p></p></font><font color="#0000ff" size="2">public</font><font size="2">:
<p>IpOptNLP();</p>
<p></p></font><font color="#0000ff" size="2">virtual</font></div><font size="2">~IpOptNLP();<font size="2">
<p>IpOptNLP::IpOptNLP(Wrapper* pcWrapper)</p>
<p>{ </p>
<p>m_pcUnManagedWrapper = pcWrapper;</p>
<p>}</p>
<p></p></font></font>
<div class="Ih2E3d"><font color="#0000ff" size="2">virtual</font><font size="2"> </font><font color="#0000ff" size="2">bool</font><font size="2"> get_nlp_info(</font><font color="#0000ff" size="2">int</font><font size="2">& n, </font><font color="#0000ff" size="2">int</font><font size="2">& m, </font><font color="#0000ff" size="2">int</font><font size="2">& nnz_jac_g, </font><font color="#0000ff" size="2">int</font><font size="2">& nnz_h_lag, IndexStyleEnum& index_style);
<p></p></font></div><font color="#0000ff" size="2">virtual</font><font size="2"> </font><font color="#0000ff" size="2">bool</font><font size="2"> get_bounds_info(</font><font color="#0000ff" size="2">int</font><font size="2"> n, </font><font color="#0000ff" size="2">double</font><font size="2">* x_l, </font><font color="#0000ff" size="2">double</font><font size="2">* x_u, </font><font color="#0000ff" size="2">int</font><font size="2"> m, </font><font color="#0000ff" size="2">double</font><font size="2">* g_l, </font><font color="#0000ff" size="2">double</font><font size="2">* g_u);
<p></p></font><font color="#0000ff" size="2">virtual</font><font size="2"> </font><font color="#0000ff" size="2">bool</font><font size="2"> get_starting_point(</font><font color="#0000ff" size="2">int</font><font size="2"> n, </font><font color="#0000ff" size="2">bool</font><font size="2"> init_x, </font><font color="#0000ff" size="2">double</font><font size="2">* x, </font><font color="#0000ff" size="2">bool</font><font size="2"> init_z, </font><font color="#0000ff" size="2">double</font><font size="2">* z_L, </font><font color="#0000ff" size="2">double</font><font size="2">* z_U,
<p></p></font><font color="#0000ff" size="2">int</font><font size="2"> m, </font><font color="#0000ff" size="2">bool</font><font size="2"> init_lambda, </font><font color="#0000ff" size="2">double</font><font size="2">* lambda);
<p></p></font><font color="#0000ff" size="2">virtual</font><font size="2"> </font><font color="#0000ff" size="2">bool</font><font size="2"> eval_f(</font><font color="#0000ff" size="2">int</font><font size="2"> n, </font><font color="#0000ff" size="2">const</font><font size="2"> </font><font color="#0000ff" size="2">double</font><font size="2">* x, </font><font color="#0000ff" size="2">bool</font><font size="2"> new_x, </font><font color="#0000ff" size="2">double</font><font size="2">& obj_value);
<p></p></font><font color="#0000ff" size="2">virtual</font><font size="2"> </font><font color="#0000ff" size="2">bool</font><font size="2"> eval_grad_f(</font><font color="#0000ff" size="2">int</font><font size="2"> n, </font><font color="#0000ff" size="2">const</font><font size="2"> Number* x, </font><font color="#0000ff" size="2">bool</font><font size="2"> new_x, </font><font color="#0000ff" size="2">double</font><font size="2">* grad_f);
<p></p></font><font color="#0000ff" size="2">virtual</font><font size="2"> </font><font color="#0000ff" size="2">bool</font><font size="2"> eval_g(</font><font color="#0000ff" size="2">int</font><font size="2"> n, </font><font color="#0000ff" size="2">const</font><font size="2"> </font><font color="#0000ff" size="2">double</font><font size="2">* x, </font><font color="#0000ff" size="2">bool</font><font size="2"> new_x, </font><font color="#0000ff" size="2">int</font><font size="2"> m, </font><font color="#0000ff" size="2">double</font><font size="2">* g);
<p></p></font><font color="#0000ff" size="2">virtual</font><font size="2"> </font><font color="#0000ff" size="2">bool</font><font size="2"> eval_jac_g(</font><font color="#0000ff" size="2">int</font><font size="2"> n, </font><font color="#0000ff" size="2">const</font><font size="2"> </font><font color="#0000ff" size="2">double</font><font size="2">* x, </font><font color="#0000ff" size="2">bool</font><font size="2"> new_x,
<p></p></font><font color="#0000ff" size="2">int</font><font size="2"> m, </font><font color="#0000ff" size="2">int</font><font size="2"> nele_jac, </font><font color="#0000ff" size="2">int</font><font size="2">* iRow, </font><font color="#0000ff" size="2">int</font><font size="2"> *jCol,
<p></p></font><font color="#0000ff" size="2">double</font><font size="2">* values);
<p></p></font><font color="#0000ff" size="2">virtual</font><font size="2"> </font><font color="#0000ff" size="2">bool</font><font size="2"> eval_h(</font><font color="#0000ff" size="2">int</font><font size="2"> n, </font><font color="#0000ff" size="2">const</font><font size="2"> </font><font color="#0000ff" size="2">double</font><font size="2">* x, </font><font color="#0000ff" size="2">bool</font><font size="2"> new_x,
<p></p></font><font color="#0000ff" size="2">double</font><font size="2"> obj_factor, </font><font color="#0000ff" size="2">int</font><font size="2"> m, </font><font color="#0000ff" size="2">const</font><font size="2"> </font><font color="#0000ff" size="2">double</font><font size="2">* lambda,
<p></p></font><font color="#0000ff" size="2">bool</font><font size="2"> new_lambda, </font><font color="#0000ff" size="2">int</font><font size="2"> nele_hess, </font><font color="#0000ff" size="2">int</font><font size="2">* iRow,
<p></p></font><font color="#0000ff" size="2">int</font><font size="2">* jCol, </font><font color="#0000ff" size="2">double</font><font size="2">* values);
<p></p></font><font color="#0000ff" size="2">virtual</font><font size="2"> </font><font color="#0000ff" size="2">void</font><font size="2"> finalize_solution(SolverReturn status, </font><font color="#0000ff" size="2">int</font><font size="2"> n, </font><font color="#0000ff" size="2">const</font><font size="2"> </font><font color="#0000ff" size="2">double</font><font size="2">* x,
<p></p></font><font color="#0000ff" size="2">const</font><font size="2"> </font><font color="#0000ff" size="2">double</font><font size="2">* z_L, </font><font color="#0000ff" size="2">const</font><font size="2"> </font><font color="#0000ff" size="2">double</font><font size="2">* z_U,
<p></p></font><font color="#0000ff" size="2">int</font><font size="2"> m, </font><font color="#0000ff" size="2">const</font><font size="2"> </font><font color="#0000ff" size="2">double</font><font size="2">* g, </font><font color="#0000ff" size="2">const</font><font size="2"> </font><font color="#0000ff" size="2">double</font><font size="2">* lambda,
<p></p></font><font color="#0000ff" size="2">double</font><font size="2"> obj_value,
<p></p></font><font color="#0000ff" size="2">const</font><font size="2"> IpoptData* ip_data,
<p>IpoptCalculatedQuantities* ip_cq);</p>
<p></p></font><font color="#0000ff" size="2">virtual</font><font size="2"> </font><font color="#0000ff" size="2">bool</font><font size="2"> intermediate_callback(AlgorithmMode mode,
<p></p></font><font color="#0000ff" size="2">int</font><font size="2"> iter, </font><font color="#0000ff" size="2">double</font><font size="2"> obj_value,
<p></p></font><font color="#0000ff" size="2">double</font><font size="2"> inf_pr, </font><font color="#0000ff" size="2">double</font><font size="2"> inf_du,
<p></p></font><font color="#0000ff" size="2">double</font><font size="2"> mu, </font><font color="#0000ff" size="2">double</font><font size="2"> d_norm,
<p></p></font><font color="#0000ff" size="2">double</font><font size="2"> regularization_size,
<p></p></font><font color="#0000ff" size="2">double</font><font size="2"> alpha_du, </font><font color="#0000ff" size="2">double</font><font size="2"> alpha_pr,
<p></p></font><font color="#0000ff" size="2">int</font><font size="2"> ls_trials,
<p></p></font><font color="#0000ff" size="2">const</font><font size="2"> IpoptData* ip_data,
<p>IpoptCalculatedQuantities* ip_cq);</p>
<p>};</p></font></div>
<div><br><strong>And I have c export function like</strong></div>
<div> </div>
<div><font color="#0000ff" size="2">
<p>#define</p></font><font size="2">IPOPT_API </font><font color="#0000ff" size="2">__declspec</font><font size="2">( </font><font color="#0000ff" size="2">dllexport</font><font size="2"> ) </font><font color="#0000ff" size="2">
<p>#define</p></font><font size="2">PLSHANDLE32(name) </font><font color="#0000ff" size="2">struct</font><font size="2"> name</font><font color="#0000ff" size="2">#</font><font size="2">#__ { </font><font color="#0000ff" size="2">int</font><font size="2"> unused; }; \
<p></p></font><font color="#0000ff" size="2">typedef</font><font size="2"> </font><font color="#0000ff" size="2">const</font><font size="2"> </font><font color="#0000ff" size="2">struct</font><font size="2"> name</font><font color="#0000ff" size="2">#</font><font size="2">#__ * name;</font>
<p><font size="2"> </font></p></div><font size="2">
<p>Wrapper*HToPtrDialogFilters(HIPOPTHANDLE h) {assert(h); </p></font><font color="#0000ff" size="2">return</font><font size="2"> (Wrapper* )h;};
<p>HIPOPTHANDLE PtrToHIPOPTHANDLE(Wrapper*p) {assert(p); </p></font><font color="#0000ff" size="2">return</font><font size="2"> (HIPOPTHANDLE )p;};</font><font color="#0000ff" size="2">
<p>void</p></font><font size="2">IPOPT_API IpOptCreate(HIPOPTHANDLE *hIpoptHandle)
<p>{</p>
<p>Wrapper* pFilters = </p></font><font color="#0000ff" size="2">new</font><font size="2"> Wrapper(</font><font color="#0000ff" size="2">false</font><font size="2">, </font><font color="#0000ff" size="2">false</font><font size="2">);
<p>*hIpoptHandle = PtrToHIPOPTHANDLE(pFilters);</p>
<p>}</p></font><font size="2">
<p>ApplicationReturnStatus IPOPT_API IpOptInitializeNLP(HIPOPTHANDLE hIpoptHandle</p></font><font size="2">)
<div dir="ltr">{</div>
<blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
<div></div></blockquote></font><font color="#0000ff" size="2">return</font><font size="2"> HToPtrDialogFilters(hIpoptHandle)->Initialize(NULL</font><font size="2">);</font><font size="2">
<p>}</p></font>
<div><strong>At the c# end</strong></div>
<div>I import these 2 functions like</div><font color="#0000ff" size="2">
<p>class</p></font><font size="2"></font><font color="#2b91af" size="2">Helper</font><font size="2">
<p>{</p>
<p>[</p></font><font color="#2b91af" size="2">DllImport</font><font size="2">(</font><font color="#a31515" size="2">"IPOPTWrapper.dll"</font><font size="2">, CallingConvention = </font><font color="#2b91af" size="2">CallingConvention</font><font size="2">.Cdecl, EntryPoint = </font><font color="#a31515" size="2">"IpOptCreate"</font><font size="2">)]
<p></p></font><font color="#0000ff" size="2">unsafe</font><font size="2"> </font><font color="#0000ff" size="2">public</font><font size="2"> </font><font color="#0000ff" size="2">static</font><font size="2"> </font><font color="#0000ff" size="2">extern</font><font size="2"> </font><font color="#0000ff" size="2">void</font><font size="2"> IpOptCreate(</font><font color="#0000ff" size="2">ref</font><font size="2"> </font><font color="#2b91af" size="2">IntPtr</font><font size="2"> handle);</font><font size="2">
<p>[</p></font><font color="#2b91af" size="2">DllImport</font><font size="2">(</font><font color="#a31515" size="2">"IPOPTWrapper.dll"</font><font size="2">, CallingConvention = </font><font color="#2b91af" size="2">CallingConvention</font><font size="2">.Cdecl, EntryPoint = </font><font color="#a31515" size="2">"IpOptInitializeNLP"</font><font size="2">)]
<p></p></font><font color="#0000ff" size="2">unsafe</font><font size="2"> </font><font color="#0000ff" size="2">public</font><font size="2"> </font><font color="#0000ff" size="2">static</font><font size="2"> </font><font color="#0000ff" size="2">extern</font><font size="2"> </font><font color="#0000ff" size="2">void</font><font size="2"> IpOptInitializeNLP(</font><font color="#2b91af" size="2">IntPtr</font><font size="2"> handle</font><font size="2">);</font>
<p><font size="2">}</font></p>
<p><font size="2"><strong>And in my main C# class</strong></font></p><font size="2"><font color="#2b91af" size="2"><font size="2">
<p>returnStatus = </p></font><font color="#2b91af" size="2">IPOPTReturnStatus</font><font size="2">.Solve_Succeeded; //<font color="#000000">IPOPTReturnStatus is an enum defined in the above mentioned class - ie <strong>Helper</strong> class</font></font>
<p>IntPtr</p></font><font size="2">handle = </font><font color="#2b91af" size="2">IntPtr</font><font size="2">.Zero; </font><font color="#008000" size="2">// Handle to the IPOPT helper</font><font size="2">
<p></p></font><font color="#2b91af" size="2">Helper</font><font size="2">.IpOptCreate(</font><font color="#0000ff" size="2">ref</font><font size="2"> handle);</font><font size="2"><font size="2">
<p>returnStatus = </p></font><font color="#2b91af" size="2">Helper</font><font size="2">.IpOptInitializeNLP(handle);
<div> </div>
<div>Now, my code throws an exception at the Initialize. But, if I try to Invoke the Impl->Initialize(paramsFile) from within the constructor of the <strong>Wrapper</strong> class, then Ipopt Initializes. But, then again the next call(involving the Impl pointer) to the dll from the C# end throws up an exception.</div>
<div> </div>
<div>Thanks,</div>
<div>Nandini</div></font></font></font>
<div>
<div></div>
<div class="Wj3C7c">
<div> </div>
<div><br></div>
<div class="gmail_quote">On Tue, Dec 9, 2008 at 5:52 PM, Damien Hocking <span dir="ltr"><<a href="mailto:damien@khubla.com" target="_blank">damien@khubla.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">
<div>Nandini,<br><br>Is that your own Wrapper class? We'd need to see that before offering hints on why your ref counts are going to zero. Whenever I want to wrap a C++ solver in C# I always use IntPtrs and manage memory myself, otherwise you can fall foul of aggressive memory cleanups on the C# side.<br>
<br>private IntPtr IPOPTSolver_; //etc etc<br><br><br>Damien<br>
<blockquote style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: rgb(0,0,255) 2px solid; MARGIN-RIGHT: 0px">
<hr>
<b>From:</b> Nandini Chakravorti [mailto:<a href="mailto:nchakravorti@gmail.com" target="_blank">nchakravorti@gmail.com</a>]<br><b>To:</b> <a href="mailto:ipopt@list.coin-or.org" target="_blank">ipopt@list.coin-or.org</a><br>
<b>Sent:</b> Tue, 09 Dec 2008 08:18:39 -0700<br><b>Subject:</b> [Ipopt] Ipopt Initialize<br><br>
<div>Hi,</div>
<div> </div>
<div> I am using the Ipopt lib(Release version) from C#. So, I have a dll wrapper for the ipopt.lib. So, after I run the Initialize method I get an exception saying ""Attempted to read or write protected memory. This is often an indication that other memory is corrupt." </div>
<div> </div>
<div> Giving a C++ snippet of my code below:</div>
<div> </div>
<div> <font color="#0000ff" size="2"></font></div>
<blockquote style="MARGIN-RIGHT: 0px">
<p>class<font size="2"> Wrapper</font></p>
<blockquote style="MARGIN-RIGHT: 0px">
<div>{</div>
<blockquote style="MARGIN-RIGHT: 0px">
<div><font color="#0000ff" size="2">private</font><font size="2">:</font></div>
<div>SmartPtr<IpoptApplication> Impl;</div>
<div>SmartPtr<TNLP> mynlp; </div>
<div> </div>
<div>public:</div>
<div><font color="#0000ff" size="2"><font size="2">
<p>ApplicationReturnStatus </p></font></font><font size="2">Initialize()<br>{ </font><font size="2">
<p>return Impl->Initialize(</p></font><font size="2">);</font><font size="2">
<p>}</p></font></div></blockquote><font size="2">
<p>};</p><font color="#0000ff" size="2">
<p>class</p></font><font size="2">IpOptNLP: </font><font color="#0000ff" size="2">public</font><font size="2"> TNLP
<p>{ </p>
<p></p></font><font color="#0000ff" size="2">private</font><font size="2">:
<p>Wrapper* m_pcUnManagedWrapper;</p>
<p></p>
<p></p></font><font color="#0000ff" size="2">public</font><font size="2">:
<p>IpOptNLP();</p>
<p></p></font><font color="#0000ff" size="2">virtual</font><font size="2"> ~IpOptNLP();
<p>IpOptNLP(Wrapper* pcWrapper) { <font size="2">m_pcUnManagedWrapper = pcWrapper;}</font></p><font color="#0000ff" size="2">
<p>virtual</p></font><font size="2"></font><font color="#0000ff" size="2">bool</font><font size="2"> get_nlp_info(</font><font color="#0000ff" size="2">int</font><font size="2">& n, </font><font color="#0000ff" size="2">int</font><font size="2">& m, </font><font color="#0000ff" size="2">int</font><font size="2">& nnz_jac_g, </font><font color="#0000ff" size="2">int</font><font size="2">& nnz_h_lag, IndexStyleEnum& index_style); //And so on</font>
<p><font size="2">}</font></p>
<p> </p>
<p>Now when I invoke the Initialize function from C#(after exporting the dll) I get an exception when <em>Impl->Initialize(</em><font size="2"><em>)</em> is getting invoked. And while debugging I see the reference count for both Impl and mynlp are zero. </font></p>
<p>Can someone please point out where I might be going wrong?</p>
<p>Thanks,</p>
<p>Nandini</p>
<p> </p>
<p><font size="2"> </font></p></font></font></blockquote></blockquote>
<div> </div>
<div> </div></blockquote></div></blockquote></div><br></div></div></blockquote></div><br>