<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Wed Feb 07 21:59:54 UTC 2024

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary append 'field=key&field=summary' to the URL of your request.
-->
<rss version="0.92" >
<channel>
    <title>MongoDB Jira</title>
    <link>https://jira.mongodb.org</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>9.7.1</version>
        <build-number>970001</build-number>
        <build-date>13-04-2023</build-date>
    </build-info>


<item>
            <title>[CXX-660] Pointer free early bug</title>
                <link>https://jira.mongodb.org/browse/CXX-660</link>
                <project id="11980" key="CXX">C++ Driver</project>
                    <description>&lt;p&gt;See this example below:&lt;br/&gt;
bsoncxx::document::view getfilter ()&lt;br/&gt;
{&lt;br/&gt;
  bsoncxx::builder::basic::document filter{};&lt;br/&gt;
  filter.append( bsoncxx::builder::basic::kvp(&quot;p_test&quot;, &quot;abcabc&quot;) );&lt;br/&gt;
  return filter.view();&lt;br/&gt;
}&lt;br/&gt;
main()&lt;/p&gt;
{
  bsoncxx::document::view filter=getfilter(); //error
}
&lt;p&gt;//the filter&apos;s construction contain &quot; *_data &quot; pointer. but this pointer&apos;s destination memory will free when getfilter() function returned.&lt;br/&gt;
so I got &quot;EXC_Bad_ACCESS&quot; error.&lt;/p&gt;

&lt;p&gt;This bug occasional occur, I suppose it depends c++ recycling the memory zone with &quot;_data&quot; pointer.&lt;/p&gt;

&lt;p&gt;Another source of evidence:&lt;br/&gt;
                    auto filter = th-&amp;gt;getfilter();&lt;br/&gt;
                    auto cursor = result.find(filter);&lt;br/&gt;
when I run directly, it told me  &quot;EXC_Bad_ACCESS&quot;.&lt;br/&gt;
But when I run into debug mode and &quot;STEP BY STEP&quot; before that statements, it seems normally, and got a right result.&lt;/p&gt;

&lt;p&gt;My program is single threaded.&lt;/p&gt;</description>
                <environment></environment>
        <key id="228483">CXX-660</key>
            <summary>Pointer free early bug</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</type>
                                            <priority id="3" iconUrl="https://jira.mongodb.org/images/icons/priorities/major.svg">Major - P3</priority>
                        <status id="6" iconUrl="https://jira.mongodb.org/images/icons/statuses/closed.png" description="The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.">Closed</status>
                    <statusCategory id="3" key="done" colorName="success"/>
                                    <resolution id="9">Done</resolution>
                                        <assignee username="andrew.morrow@mongodb.com">Andrew Morrow</assignee>
                                    <reporter username="vrqq">vrqq</reporter>
                        <labels>
                    </labels>
                <created>Fri, 4 Sep 2015 16:41:49 +0000</created>
                <updated>Thu, 24 Sep 2015 17:00:53 +0000</updated>
                            <resolved>Fri, 18 Sep 2015 21:16:37 +0000</resolved>
                                                                    <component>BSON</component>
                                        <votes>0</votes>
                                    <watches>2</watches>
                                                                                                                <comments>
                            <comment id="1042263" author="vrqq" created="Thu, 24 Sep 2015 17:00:53 +0000"  >&lt;p&gt;OK I think I see you point now.&lt;br/&gt;
When I want to copy any document::element variable, I should call get_value() to get an copyable version.&lt;br/&gt;
Thank you.&lt;br/&gt;
Maybe you can rewrite &quot;=&quot; overload method to copy it automatically.&lt;/p&gt;</comment>
                            <comment id="1042242" author="acm" created="Thu, 24 Sep 2015 16:48:16 +0000"  >&lt;p&gt;The problem is that document::element, which is what view::operator[] returns, is still just a view into the document. To get a value that you can take away, you need to call get_value on the element:&lt;/p&gt;

&lt;p/&gt;
&lt;div id=&quot;syntaxplugin&quot; class=&quot;syntaxplugin&quot; style=&quot;border: 1px dashed #bbb; border-radius: 5px !important; overflow: auto; max-height: 30em;&quot;&gt;
&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot; width=&quot;100%&quot; style=&quot;font-size: 1em; line-height: 1.4em !important; font-weight: normal; font-style: normal; color: black;&quot;&gt;
		&lt;tbody &gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;  margin-top: 10px;   margin-bottom: 10px;  width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;auto _id = tmp.value().view()[&quot;_id&quot;].get_value();&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
			&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p/&gt;

&lt;p&gt;Or if you already know that the _id field is of type oid, you could call get_oid directly and get a b_oid value:&lt;/p&gt;

&lt;p/&gt;
&lt;div id=&quot;syntaxplugin&quot; class=&quot;syntaxplugin&quot; style=&quot;border: 1px dashed #bbb; border-radius: 5px !important; overflow: auto; max-height: 30em;&quot;&gt;
&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot; width=&quot;100%&quot; style=&quot;font-size: 1em; line-height: 1.4em !important; font-weight: normal; font-style: normal; color: black;&quot;&gt;
		&lt;tbody &gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;  margin-top: 10px;   margin-bottom: 10px;  width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;auto _id = tmp.value().view()[&quot;_id&quot;].get_oid();&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
			&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p/&gt;</comment>
                            <comment id="1042219" author="vrqq" created="Thu, 24 Sep 2015 16:31:08 +0000"  >&lt;p&gt;But I still had my doubts.&lt;/p&gt;

&lt;p&gt;First I declare a variable _id:&lt;br/&gt;
  bsoncxx::document::element _id;&lt;/p&gt;

&lt;p&gt;Then I pass it a value from a temporary variable.&lt;br/&gt;
  _id = tmp.value().view()&lt;span class=&quot;error&quot;&gt;&amp;#91;&amp;quot;_id&amp;quot;&amp;#93;&lt;/span&gt;;&lt;/p&gt;

&lt;p&gt;When I execute the statement above, I think it should be &quot;copy&quot; method instead of &quot;reference&quot; method, a new variable &quot;_id&quot; should be CREATED, this two variable should not have any relations.&lt;br/&gt;
tmp.value() should be destroyed BUT &quot;_id&quot; not be affected.&lt;/p&gt;

&lt;p&gt;In other words, I use bsoncxx::document::element can&apos;t declare to a independent variable.&lt;br/&gt;
Is that really it? Thank you.&lt;/p&gt;</comment>
                            <comment id="1037315" author="acm" created="Fri, 18 Sep 2015 21:16:37 +0000"  >&lt;p&gt;I&apos;m closing this issue as I don&apos;t believe there is a bug here. Please feel free to re-open it if you need any further help with this issue.&lt;/p&gt;</comment>
                            <comment id="1033262" author="acm" created="Thu, 17 Sep 2015 17:05:43 +0000"  >&lt;p&gt;I believe the problem, in both instances, is that you are returning a view into an object that has gone out of scope. Taking your second example, you are saying:&lt;/p&gt;

&lt;p/&gt;
&lt;div id=&quot;syntaxplugin&quot; class=&quot;syntaxplugin&quot; style=&quot;border: 1px dashed #bbb; border-radius: 5px !important; overflow: auto; max-height: 30em;&quot;&gt;
&lt;table cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; border=&quot;0&quot; width=&quot;100%&quot; style=&quot;font-size: 1em; line-height: 1.4em !important; font-weight: normal; font-style: normal; color: black;&quot;&gt;
		&lt;tbody &gt;
				&lt;tr id=&quot;syntaxplugin_code_and_gutter&quot;&gt;
						&lt;td  style=&quot; line-height: 1.4em !important; padding: 0em; vertical-align: top;&quot;&gt;
					&lt;pre style=&quot;font-size: 1em; margin: 0 10px;  margin-top: 10px;   margin-bottom: 10px;  width: auto; padding: 0;&quot;&gt;&lt;span style=&quot;color: black; font-family: &apos;Consolas&apos;, &apos;Bitstream Vera Sans Mono&apos;, &apos;Courier New&apos;, Courier, monospace !important;&quot;&gt;tmp.value().view()&lt;/span&gt;&lt;/pre&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
			&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p/&gt;

&lt;p&gt;Note that the call to &apos;value()&apos; returns a temporary. Calling &apos;view&apos; on a temporary gets you a view into the temporary, and then the element returned by accessing _id also refers into a temporary. At the end of the statement, the temporary value is destroyed, and the views are now pointing to free&apos;d memory.&lt;/p&gt;

&lt;p&gt;In the first example in your getfilter example you are constructing a document scoped to the function, which owns memory, and then returning a view into it. However, when you leave the function, the document is destroyed, so the view into it is now invalid.&lt;/p&gt;</comment>
                            <comment id="1023082" author="vrqq" created="Sat, 5 Sep 2015 05:41:21 +0000"  >&lt;p&gt;Another evidence:&lt;br/&gt;
main ()&lt;br/&gt;
{&lt;br/&gt;
            bsoncxx::document::element _id;&lt;br/&gt;
            if (true) &lt;/p&gt;
{
                auto tmp = dbconn.find_one(filter);
                _id = tmp.value().view()[&quot;_id&quot;]; //debug to watch &quot;_id&quot; is right here.
            }
&lt;p&gt;            bsoncxx::to_json(_id); //It&apos;s wrong in here, because when &quot;if statement&quot; done, &quot;tmp&quot; variable is destory, but &quot;_id&quot; variable contain a pointer into &quot;tmp&quot; variable&apos;s one location. &lt;br/&gt;
}&lt;/p&gt;</comment>
                    </comments>
                    <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|hrenfj:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>9223372036854775807</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                    <customfield id="customfield_10557" key="com.pyxis.greenhopper.jira:gh-sprint">
                        <customfieldname>Sprint</customfieldname>
                        <customfieldvalues>
                                <customfieldvalue id="673">Platform 9 (09/18/15)</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </customfields>
    </item>
</channel>
</rss>