System Access Violation using get

View: New views
4 Messages — Rating Filter:   Alert me  

System Access Violation using get

by dirk.brodbeck :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi there,
I tried to use lpsolve.read_LP from lpsolve55.cs to read a lp model in my own model structure. Reading and solving the model works just fine. However, if I use lpsolve.get_lp_name or lpsolve.get_row_name a system access violation occurs:

        public void ReadLP(string filename)
        {
            int lp;
            lp = lpsolve.read_LP(filename, 3,"");

            if (lp == 0)
            {
                throw new LPOptimizerException("couldn't construct a new model from file " + filename);
            }


            lpsolve.solve(lp);

            string modelname;

//here the crash occurs
            modelname = lpsolve.get_lp_name(lp);

            string colname;
            colname = lpsolve.get_row_name(lp, 1);

            lpsolve.delete_lp(lp);
        }

Thanks for your help...

Dirk


Re: System Access Violation using get

by Peter Notebaert-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

What version of lp_solve? Make sure you use the latest lpsolve55.cs from sourceforge. There have been some issues with this and with the latest version this should be ok.

Peter

--- In lp_solve@..., "dirk.brodbeck" <dirk.brodbeck@...> wrote:

>
> Hi there,
> I tried to use lpsolve.read_LP from lpsolve55.cs to read a lp model in my own model structure. Reading and solving the model works just fine. However, if I use lpsolve.get_lp_name or lpsolve.get_row_name a system access violation occurs:
>
>         public void ReadLP(string filename)
>         {
>             int lp;
>             lp = lpsolve.read_LP(filename, 3,"");
>
>             if (lp == 0)
>             {
>                 throw new LPOptimizerException("couldn't construct a new model from file " + filename);
>             }
>
>
>             lpsolve.solve(lp);
>
>             string modelname;
>
> //here the crash occurs
>             modelname = lpsolve.get_lp_name(lp);
>
>             string colname;
>             colname = lpsolve.get_row_name(lp, 1);
>
>             lpsolve.delete_lp(lp);
>         }
>
> Thanks for your help...
>
> Dirk
>



Re: System Access Violation using get

by dirk.brodbeck :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Peter,
thank you for your immediate answer.I worked already with lp_solve 5.5.0.15 but not with the actual version of lpsolve55.cs.

To avoid the problem I have described, obviously some functions returning pointers are marked in the new version as unsafe. When you use the new version of lpsolve55.cs, then you have to mark the whole Visual Studio solution to allow unsafe code. Good programming practise tells you, that you should also mark the class and methods using these functions as unsafe (see e.g.: http://www.c-sharpcorner.com/UploadFile/PatrickSmacchia/CSharp2UnsafeCode02162006063859AM/CSharp2UnsafeCode.aspx)

If you don't like this, you have to comment the unsafe functions of the API and use the rest "safely".

Regards, Dirk

P.S.: thanks a lot for lpsolve, it's gerat!

--- In lp_solve@..., "peter_notebaert" <_peno_@...> wrote:

>
> What version of lp_solve? Make sure you use the latest lpsolve55.cs from sourceforge. There have been some issues with this and with the latest version this should be ok.
>
> Peter
>
> --- In lp_solve@..., "dirk.brodbeck" <dirk.brodbeck@> wrote:
> >
> > Hi there,
> > I tried to use lpsolve.read_LP from lpsolve55.cs to read a lp model in my own model structure. Reading and solving the model works just fine. However, if I use lpsolve.get_lp_name or lpsolve.get_row_name a system access violation occurs:
> >
> >         public void ReadLP(string filename)
> >         {
> >             int lp;
> >             lp = lpsolve.read_LP(filename, 3,"");
> >
> >             if (lp == 0)
> >             {
> >                 throw new LPOptimizerException("couldn't construct a new model from file " + filename);
> >             }
> >
> >
> >             lpsolve.solve(lp);
> >
> >             string modelname;
> >
> > //here the crash occurs
> >             modelname = lpsolve.get_lp_name(lp);
> >
> >             string colname;
> >             colname = lpsolve.get_row_name(lp, 1);
> >
> >             lpsolve.delete_lp(lp);
> >         }
> >
> > Thanks for your help...
> >
> > Dirk
> >
>



RE: Re: System Access Violation using get

by William H. Patton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dirk,          Thanks for the explanation. Do you have a suggestion to fix
the issue?

That is , can we add an API helper to the lp_solve dll to give bak a managed
(or manageable string) for the 6 places

The current API gives back pointers to strings owned by the C dll heap?

        public static string get_col_name(int lp, int column)

        {

            return (Marshal.PtrToStringAnsi(get_col_name_c(lp, column)));

        }

 

Classically in C the caller could supply a buffer for the dll to fill

      Function GetName( enumname, pBuf, maxlen)

And the dll API would switch on enumname to fill the callers pBuf with the
desired string up to maxlen with safe strcpy.

Is this or something similar a way to do a  CLR "safe"  return of a string?

 

Maybe in C# the issue is to pass the API a function pointer
pGetstring(pChar,len) returns bool bsuceed that we should call from our new
wrappers.

So in the lpsolve API we add  interfaces like the following so that the CLR
fetches the bytes under its control.

 bool CLR_ get_col_name(int lp, int column, pGetString)

{

    char *   s = get_col_name_c(lp, column);

    bool     bret = FALSE;

    if ( s != NULL ) then

        bret =   (*pGetstring)(s,len(s));  

    return bret;

}

 

William

 

  _____  

From: lp_solve@... [mailto:lp_solve@...] On Behalf
Of Dirk
Sent: Friday, October 16, 2009 6:58 AM
To: lp_solve@...
Subject: [lp_solve] Re: System Access Violation using get

 

 

Hi Peter,
thank you for your immediate answer.I worked already with lp_solve 5.5.0.15
but not with the actual version of lpsolve55.cs.

To avoid the problem I have described, obviously some functions returning
pointers are marked in the new version as unsafe. When you use the new
version of lpsolve55.cs, then you have to mark the whole Visual Studio
solution to allow unsafe code. Good programming practise tells you, that you
should also mark the class and methods using these functions as unsafe (see
e.g.: http://www.c-
<http://www.c-sharpcorner.com/UploadFile/PatrickSmacchia/CSharp2UnsafeCode02
162006063859AM/CSharp2UnsafeCode.aspx>
sharpcorner.com/UploadFile/PatrickSmacchia/CSharp2UnsafeCode02162006063859AM
/CSharp2UnsafeCode.aspx)

If you don't like this, you have to comment the unsafe functions of the API
and use the rest "safely".

Regards, Dirk

P.S.: thanks a lot for lpsolve, it's gerat!

--- In lp_solve@yahoogroup <mailto:lp_solve%40yahoogroups.com> s.com,
"peter_notebaert" <_peno_@...> wrote:
>
> What version of lp_solve? Make sure you use the latest lpsolve55.cs from
sourceforge. There have been some issues with this and with the latest
version this should be ok.
>
> Peter
>
> --- In lp_solve@yahoogroup <mailto:lp_solve%40yahoogroups.com> s.com,
"dirk.brodbeck" <dirk.brodbeck@> wrote:
> >
> > Hi there,
> > I tried to use lpsolve.read_LP from lpsolve55.cs to read a lp model in
my own model structure. Reading and solving the model works just fine.
However, if I use lpsolve.get_lp_name or lpsolve.get_row_name a system
access violation occurs:
> >
> > public void ReadLP(string filename)
> > {
> > int lp;
> > lp = lpsolve.read_LP(filename, 3,"");
> >
> > if (lp == 0)
> > {
> > throw new LPOptimizerException("couldn't construct a new model from file
" + filename);

> > }
> >
> >
> > lpsolve.solve(lp);
> >
> > string modelname;
> >
> > //here the crash occurs
> > modelname = lpsolve.get_lp_name(lp);
> >
> > string colname;
> > colname = lpsolve.get_row_name(lp, 1);
> >
> > lpsolve.delete_lp(lp);
> > }
> >
> > Thanks for your help...
> >
> > Dirk
> >
>