首页 \ 问答 \ 格式和指针/十六进制值(内存覆盖)(Format and Pointers/Hex Values (Memory Overwrite))

格式和指针/十六进制值(内存覆盖)(Format and Pointers/Hex Values (Memory Overwrite))

在我的Delphi XE2 32位应用程序(Update 4 Hotfix 1 Version 16.0.4504.48759)中,我使用Format()例程来记录指针值。

例如:

Format('MyObject (%p)', [Pointer(MyObject)]);

但是,结果字符串有时包含垃圾字符(例如,在这种情况下,'?'或'|'代替十六进制数字):

MyObject (4E?|2010)

将'%p'替换为'%x'时,我也得到相同的结果:

Format('MyObject (%x)', [Integer(MyObject)]);

但是,使用整数值始终有效:

Format('MyObject (%d)', [Integer(MyObject)]);

MyObject (1291453120)

是否存在我不知道的错误,或者这与此处遇到的问题有关?

除了“%s”以外的任何内容与Variant一起使用时,为什么Format会崩溃?

UPDATE

我已经接受了Jeroen的回答,因为它引导我通过消除过程来解决问题。 在通过F7启动应用程序的情况之后(根据评论),我认为在此过程中必须出现问题。 在预感,我禁用madExcept从其IDE菜单,重建应用程序,问题消失了。 显然,无论代码madExcept链接到我的应用程序是什么导致在SysUtils常量TwoHexLookup中覆盖。 重新启用madExcept和重建(我没有任何其他更改)也有效,因此在链接阶段必定存在一些损坏。

Jeroen用于检测内存损坏的策略是一项有用的练习,如果我遇到类似的情况,它应该证明是有价值的。


In my Delphi XE2 32-bit application (Update 4 Hotfix 1 Version 16.0.4504.48759) , I'm using the Format() routine to log pointer values.

For example:

Format('MyObject (%p)', [Pointer(MyObject)]);

However, the resulting string sometimes contains garbage characters (e.g., in this case '?' or '|' in place of hex digits):

MyObject (4E?|2010)

I also get the same result when replacing '%p' with '%x' like so:

Format('MyObject (%x)', [Integer(MyObject)]);

However, using an integer value always works:

Format('MyObject (%d)', [Integer(MyObject)]);

MyObject (1291453120)

Is there a bug that I'm unaware of or can this be related to the problem experienced here?

Why does Format crash when anything but "%s" is used with a Variant?

UPDATE

I've accepted Jeroen's answer as it led me to the solution by process of elimination. After the situation with starting the app via F7 (as per the comment), I figured that something must be going wrong much earlier in the process. On a hunch, I disabled madExcept from its IDE menu, rebuilt the app, and the problem disappeared. Evidently, whatever code madExcept was linking into my application was causing an overwrite in the SysUtils constant TwoHexLookup. Re-enabling madExcept and rebuilding (without any other changes on my part) also worked, so there must have been some corruption during the linking phase.

The strategy Jeroen outlined for detecting memory corruption was a useful exercise and should prove valuable if I encounter a similar situation.


原文:https://stackoverflow.com/questions/14860985
更新时间:2022-06-23 20:06

最满意答案

您应该使用Jdbc连接池。 然后从代码中需要连接到数据库的任何地方,只需从池中获取与数据库的连接(连接池将负责维护与数据库的所需连接数)

例如,如果您使用BoneCP:

import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * <dependency>
 * <groupId>com.jolbox</groupId>
 * <artifactId>bonecp</artifactId>
 * <version>0.8.0.RELEASE</version>
 * </dependency>
 */
public class BoneCPexample {

    public static final int TOTAL_CONNECTIONS_TO_DATABASE = 20;

    public static void main(String[] args) throws SQLException {
        BoneCPexample boneCPexample = new BoneCPexample();
        boneCPexample.doTheWork();
    }

    private void doTheWork() throws SQLException {


        String jdbcUrlString = "jdbc:postgresql://localhost/test_database";  // jdbc:postgresql://host:port/database
        BoneCPConfig bcpConfig = new BoneCPConfig();
        bcpConfig.setJdbcUrl(jdbcUrlString);
        bcpConfig.setUsername("postgres");
        bcpConfig.setPassword("mi-password");
        bcpConfig.setPartitionCount(1);
        bcpConfig.setMinConnectionsPerPartition(TOTAL_CONNECTIONS_TO_DATABASE);
        bcpConfig.setMaxConnectionsPerPartition(TOTAL_CONNECTIONS_TO_DATABASE);
        bcpConfig.setConnectionTimeoutInMs(1 * 1000);
        bcpConfig.setDefaultAutoCommit(false);
        bcpConfig.setConnectionTestStatement("select now()");
        bcpConfig.setIdleConnectionTestPeriodInMinutes(5);


        BoneCP boneCP = new BoneCP(bcpConfig);
        Connection connection = boneCP.getConnection();

        Statement statement = connection.createStatement();
        statement.execute("select * from mytable");

    }
}

you should use a Jdbc Connection Pool. then from anywhere in the code that you need a connection to the database, just get a connection to the database from the pool (the Connection Pool will be in charge of maintaining the desired number of connections to your database)

for example, if you use BoneCP:

import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * <dependency>
 * <groupId>com.jolbox</groupId>
 * <artifactId>bonecp</artifactId>
 * <version>0.8.0.RELEASE</version>
 * </dependency>
 */
public class BoneCPexample {

    public static final int TOTAL_CONNECTIONS_TO_DATABASE = 20;

    public static void main(String[] args) throws SQLException {
        BoneCPexample boneCPexample = new BoneCPexample();
        boneCPexample.doTheWork();
    }

    private void doTheWork() throws SQLException {


        String jdbcUrlString = "jdbc:postgresql://localhost/test_database";  // jdbc:postgresql://host:port/database
        BoneCPConfig bcpConfig = new BoneCPConfig();
        bcpConfig.setJdbcUrl(jdbcUrlString);
        bcpConfig.setUsername("postgres");
        bcpConfig.setPassword("mi-password");
        bcpConfig.setPartitionCount(1);
        bcpConfig.setMinConnectionsPerPartition(TOTAL_CONNECTIONS_TO_DATABASE);
        bcpConfig.setMaxConnectionsPerPartition(TOTAL_CONNECTIONS_TO_DATABASE);
        bcpConfig.setConnectionTimeoutInMs(1 * 1000);
        bcpConfig.setDefaultAutoCommit(false);
        bcpConfig.setConnectionTestStatement("select now()");
        bcpConfig.setIdleConnectionTestPeriodInMinutes(5);


        BoneCP boneCP = new BoneCP(bcpConfig);
        Connection connection = boneCP.getConnection();

        Statement statement = connection.createStatement();
        statement.execute("select * from mytable");

    }
}

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)